Changing Squares to Five Point Stars with JVR Code
-
Hi All,
Having a ball learning drawbot so thanks to everyone who has been involved in the development process to date. Recently I been playing around with the source code for one of Just Van Rossum's StackoSquares animations. That said, I have a question, in order to change the object from multiple squares to multiple five point stars, is the solution simply changing the square object to a star object. If so how would I write the star object code. Very new to coding so any help would be gratefully appreciated.
Best.
Robert
PS: The code is below this line of type.
CANVAS = 500 SQUARESIZE = 158 NSQUARES = 50 SQUAREDIST = 6 width = NSQUARES * SQUAREDIST NFRAMES = 50 for frame in range(NFRAMES): newPage(CANVAS, CANVAS) frameDuration(1/20) fill(0) rect(0, 0, CANVAS, CANVAS) phase = 2 * pi * frame / NFRAMES # angle in radians startAngle = 90 * sin(phase) endAngle = 90 * sin(phase + 0.5 * pi) translate(CANVAS/2 - width / 2, CANVAS/2) fill(1) stroke(0) for i in range(NSQUARES + 1): f = i / NSQUARES save() translate(i * SQUAREDIST, 0) scale(0.7, 1) rotate(startAngle + f * (endAngle - startAngle)) rect(-SQUARESIZE/2, -SQUARESIZE/2, SQUARESIZE, SQUARESIZE) restore() saveImage("StackOfSquares.gif")
-
hi. see this tweet by Just himself
-
@gferreira
Hi. That is exactly what I was hoping to achieve. Hopefully I can make this work.Best.
Robert
-
Hi All,
I am now totally stuck:
CANVAS = 1440 NFRAMES = 1 for frame in range(NFRAMES): newPage(CANVAS, CANVAS) frameDuration(1/20) fill(0) rect(0, 0, CANVAS, CANVAS) phase = 2 * pi * frame / NFRAMES startAngle = 90 * sin(phase) endAngle = 90 * sin(phase + 0.5 * pi) translate(CANVAS/2, CANVAS/2) fill(1) stroke(0) for i in range(20): save() translate(i * 10) scale(1.1) rotate(380) def star(x, y, n, r1, r2): pts = [] for i in range(n * 2): a = i * pi / n r = r2 if i % 2 else r1 pts.append((x + r * sin(a), y + r * cos(a))) polygon(*pts) star(1, -95, 5, 450, 260) restore() saveImage("StarTest.gif")
Any help would be very much appreciated
Best,
Robert
-
hi Robert,
very little needs to change in your previous script to replace the rectangles with stars:
- add the
star
function at the top of your script, so you can use it later on - where the
rect
function is called, usestar
instead
# copied from http://twitter.com/justvanrossum/status/1098138752805941248 def star(x, y, n, r1, r2): pts = [] for i in range(n * 2): a = i * pi / n r = r2 if i % 2 else r1 pts.append((x + r * sin(a), y + r * cos(a))) polygon(*pts) # --------------- # original script # --------------- CANVAS = 500 SQUARESIZE = 158 NSQUARES = 50 SQUAREDIST = 6 width = NSQUARES * SQUAREDIST NFRAMES = 50 for frame in range(NFRAMES): newPage(CANVAS, CANVAS) frameDuration(1/20) fill(0) rect(0, 0, CANVAS, CANVAS) phase = 2 * pi * frame / NFRAMES # angle in radians startAngle = 90 * sin(phase) endAngle = 90 * sin(phase + 0.5 * pi) translate(CANVAS/2 - width / 2, CANVAS/2) fill(1) stroke(0) for i in range(NSQUARES + 1): f = i / NSQUARES save() translate(i * SQUAREDIST, 0) scale(0.7, 1) rotate(startAngle + f * (endAngle - startAngle)) # replace rect with star # rect(-SQUARESIZE/2, -SQUARESIZE/2, SQUARESIZE, SQUARESIZE) star(0, 0, 5, SQUARESIZE, SQUARESIZE * 0.5) restore() saveImage("StackOfSquares.gif")
there’s one little catch: to draw the rectangles from center, we had to shift the origin point by half the square size (
-SQUARESIZE/2
). this is not necessary when drawing the star, because it is already drawn from center – so we simply draw it at(0, 0)
.hope it makes sense! cheers
- add the
-
Hello gferreira,
Thank you so much the reply has been very helpful. I can see that I was thinking too much about modifying all areas of the code instead of changing a small component.
Enjoy the day.
Best.
Robert