Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in how no_loop() works with Live Coding #554

Open
hx2A opened this issue Nov 10, 2024 Discussed in #545 · 5 comments
Open

Inconsistency in how no_loop() works with Live Coding #554

hx2A opened this issue Nov 10, 2024 Discussed in #545 · 5 comments
Labels
bug Something isn't working

Comments

@hx2A
Copy link
Collaborator

hx2A commented Nov 10, 2024

From discussion #545

I tried converting @vsquared 's code to module mode and found it did not work:

# Reference for Turtle Graphics in Processing
# https://gist.githubusercontent.com/nataliefreed/8483050/raw/9e3f1d0f44bcb0c872762e4b984358d375e7b5fa/turtle.pde
import py5

end = 0.0
loc = 0.0
newLoc = 0.0
h = 600.0
w = (h / 1.325) / 1.325
orientation = py5.radians(90)
rotation = -45
showLines = False


def harriss(iteration):
    global w

    if iteration > 0:
        iteration -= 1
        fd(w, iteration)
        lt(py5.radians(90))
        w = w / 1.325
        harriss(iteration)


def setup():
    global loc
    global showLines

    py5.size(800, 750)
    surface = py5.get_surface()
    surface.set_title("Harriss Spiral Design")
    showLines = False
    loc = py5.Py5Vector(py5.width / 2 + 100, py5.height / 2 + 200)
    py5.no_loop()


def draw():
    global orientation
    global rotation
    global h
    global w

    # Base Spiral
    harriss(7)
    # Right Upper Spiral
    jumpTo(495, 230)
    h = 300.0
    w = (h / 1.325) / 1.325
    orientation = py5.radians(0)
    rotation = 45
    harriss(8)
    # Left Upper Spiral
    jumpTo(240, 238)
    h = 250
    w = (h / 1.325) / 1.325
    rotation = -45
    orientation = py5.radians(90)
    harriss(7)
    # Left Lower Spiral
    jumpTo(240, 425)
    h = 200
    w = (h / 1.325) / 1.325
    rotation = 225
    orientation = py5.radians(180)
    harriss(6)
    # Center Lower Spiral
    jumpTo(390, 425)
    h = 175
    w = (h / 1.325) / 1.325
    rotation = 135
    orientation = py5.radians(-90)
    harriss(5)


# ======= Turtle Graphics Functions =========


def fd(w, iteration):
    global loc
    global newLoc
    global end
    global rotation

    polar(w, orientation)
    end = loc + newLoc

    if showLines:
        py5.stroke(0)
        py5.stroke_weight(1)
        py5.line(loc.x, loc.y, end.x, end.y)

    py5.no_fill()
    py5.stroke(py5.random(255), 0, py5.random(255))
    py5.stroke_weight(12)
    if (iteration == 7) or (iteration == 3):
        py5.arc(
            loc.x + w / 2,
            loc.y - w / 2,
            w * 1.414,
            w * 1.414,
            py5.radians(rotation),
            py5.radians(rotation + 90),
        )
    if (iteration == 6) or (iteration == 2):
        py5.arc(
            loc.x - w / 2,
            loc.y - w / 2,
            w * 1.414,
            w * 1.414,
            py5.radians(rotation),
            py5.radians(rotation + 90),
        )
    if (iteration == 5) or (iteration == 1):
        py5.arc(
            loc.x - w / 2,
            loc.y + w / 2,
            w * 1.414,
            w * 1.414,
            py5.radians(rotation),
            py5.radians(rotation + 90),
        )
    if (iteration == 4) or (iteration == 0):
        py5.arc(
            loc.x + w / 2,
            loc.y + w / 2,
            w * 1.414,
            w * 1.414,
            py5.radians(rotation),
            py5.radians(rotation + 90),
        )

    loc = end
    rotation -= 90


def lt(theta):
    global orientation
    orientation += theta


def jumpTo(x, y):
    global loc
    loc = py5.Py5Vector(x, y)


def polar(r, theta):
    global newLoc
    newLoc = py5.Py5Vector(r * py5.cos(theta), r * py5.sin(-theta))


py5.run_sketch()

The call to no_loop() in setup() causes the draw() function to never execute. I think how module mode handles this is correct; imported mode execution should not execute the draw() function.

@villares
Copy link
Collaborator

villares commented Nov 10, 2024

Wait, let me check this... looks the same to me! could it be platform dependent?

Isn't this the expected behavior? I checked also on Processsing Python mode (I used to have a page about this).

image

image

image

My expectation was fulfilled that that no_loop() inside setup() would let draw() run once and then stop. And then, no_loop()' inside draw()` fill prevent it from running again.

image

@hx2A hx2A changed the title Inconsistency in how no_loop() works between imported mode and module mode Inconsistency in how no_loop() works with Live Coding Nov 10, 2024
@hx2A
Copy link
Collaborator Author

hx2A commented Nov 10, 2024

@villares , no you are right. I took another look and I see it works correctly for imported mode and module mode. I was running this with Live Coding, and the problem is how Live Coding handles no_loop(). Calling that in setup() means it does not call draw() a single time. My initial conclusion was based on my incorrect assumption that the Live Coding feature handled no_loop() correctly, and was the same as what I'd get with the python interpreter.

So this is a bug, it is just a bug with Live Coding, not module mode / imported mode.

And this means I found a bug in the release minutes after doing the release. 😞

@villares
Copy link
Collaborator

Oh, I see, but don't let this spoil the joy of a great new release my friend!

@hx2A
Copy link
Collaborator Author

hx2A commented Nov 10, 2024

Oh, I see, but don't let this spoil the joy of a great new release my friend!

Ha, these kinds of things come with the territory. It will probably be an easy fix. :)

@hx2A hx2A added the bug Something isn't working label Nov 20, 2024
@hx2A
Copy link
Collaborator Author

hx2A commented Nov 21, 2024

Ha, these kinds of things come with the territory. It will probably be an easy fix. :)

Nope. Not easy either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants