cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 
GDL
About building parametric objects with GDL.

Generating a square with rotating FOR-NEXT loop - why isn't it working?

Bilkins
Contributor

Hi, 

I'm making a 2D script where I define the side of a square, and then I want to use a FOR-NEXT loop to generate the rest of the square. 

I've made two scripts that I think should work exactly the same, but they don't - can anytone illuminate me as to why?

 

First I made a manual script. This generates a perfect square as intended:

side = 1

LINE2 0, 0, side, 0 ! bottom edge

ADD2 side, 0
ROT2 90

LINE2 0, 0, side, 0 ! right edge

ADD2 side, 0
ROT2 90

LINE2 0, 0, side, 0 ! top edge

ADD2 side, 0
ROT2 90

LINE2 0, 0, side, 0 ! left edge

END

Then I try to make the same script as a FOR-NEXT loop:

side = 1

FOR i = 1 TO 4
    ROT2 90*(i-1)
    LINE2 0, 0, side, 0
    ADD2 side, 0     ! move to the endpoint for the next edge
NEXT i

END

This script only generates the bottom and right edge - why? 

I would imagine that increasing i>4 would generate an overlapping loop, but the geometry just keeps getting more and more wonky - what is the logic I'm not getting?

EDIT: 20 seconds after posting I realised I just needed to change the rotation to a simple 90 degrees turn at the bottom of the transformation stack instead of a "fancy" 90*(i-1) at the start.
I don't see a button to delete this post, so I'm leaving it up to be shamed. 

 

Solution:

FOR i = 1 TO 4
    LINE2 0, 0, side, 0
    ADD2 side, 0     ! move to the endpoint for the next edge
    ROT2 90
NEXT i

 

26.7004 NOR │ Win 11 Enterprise x64 ● Intel 9 185H @ 2.30GHz ● 64GB ● Nvidia RTX 3000 Ada
0 REPLIES 0

Didn't find the answer?

Check other topics in this Forum

Back to Forum

Read the latest accepted solutions!

Accepted Solutions

Start a new conversation!