Algorithm for rectilinear spiral motion (Page 1) ● SmileBASIC Source Forums

Register

# Algorithm for rectilinear spiral motion

• #1 ✎ 184 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies Can't wrap my head around this one. I'm trying to get an algorithm to read/write from/on a set of data in a spiraling, rectilinear manner. Not complicated, really. Let me picture it for you: 'Consider a 3x3 gridspace: '000 '000 '000 'Begin algorithm in center '000 '010 '000 'Algorithm proceeds in spiral fashion '010 '010 '000 '... '011 '010 '000 '... '011 '011 '000 '... '011 '011 '001 '... '011 '011 '011 '... '011 '011 '111 '... 'etc. Now, when the 3x3 gridspace becomes filled, the algorithm may be pressed to continue, building upon itself with layers. I'm trying to get something like this that can continue building for n-steps. What's the best way to do this? The algorithm isn't allowed to check gridspace for its position or the states of other elements surrounding it. It's basically a snail that wants to run around in circles for n-number of paces and not touch its trail, but it can't detect its trail and its blind. However, it can remember how many paces it traveled and has a basic grasp of mathematics and SB. Posted Edited by MZ952
• #2 ✎ 1573 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website Does it matter where the start of each "circle" is? Posted
• #3 ✎ 184 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies Each "circle" starts one unit above where the last one ended, though, if it's any simpler starting somewhere else, it's not too big a deal Posted
• #4 ✎ 1573 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website HALF = SIZE DIV 2 PRINT HALF, HALF FOR J=1 TO HALF FOR I=-(J-1) TO J PRINT HALF+I,HALF-J NEXT FOR I=-(J-1) TO J PRINT HALF+J,HALF+I NEXT FOR I=J-1 TO -J STEP -1 PRINT HALF+I,HALF+J NEXT FOR I=-J-1 TO -J STEP -1 PRINT HALF-J,HALF+I NEXT NEXT Posted
• #5 ✎ 184 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies I may not have described this well enough, so I drew a picture hopefully explaining this a little better than the text example from earlier. Each unit square represents a "step," and the algorithm is due to run only a given number of steps in the clockwise spiraling direction I tried to outline there^ Each move is counted as a step, including the start at the origin. It starts at the origin and moves up one step where it then continues to the right, down, left, up, turning clockwise about itself. When it can no longer turn about itself without reaching its starting position, it moves up one unit and proceeds. It proceeds for as many steps as it is given, ceasing anywhere it runs out of available steps. Posted Edited by MZ952
• #7 ✎ 1573 12Me21 Head Admin Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Second Year My account is over 2 years old Website You can calculate the next position directly from the current position as long as you know where the center is: (Here I'm assuming it to be x=0, y=0) IF -X>ABS(Y)-0.5 THEN 'up DEC Y ELSEIF X>ABS(Y+0.5) THEN 'down INC Y ELSEIF Y>ABS(X-0.5) THEN 'left DEC X ELSE 'right INC X ENDIF '(The last 5 lines can be simplified to ELSE : DEC X,SGN(Y) : ENDIF) IF X==Y && X<=0 THEN 'move to next layer DEC Y ELSEIF -Y>=ABS(X+0.5) THEN 'right INC X ELSEIF X>=ABS(Y+0.5) THEN 'down INC Y ELSEIF Y>=ABS(X-0.5) THEN 'left DEC X ELSE 'up DEC Y ENDIF Posted Edited by 12Me21
• #8 ✎ 72 seggiepants I like the mathematical simplicity and compactness of your code, the graph is very nice as well. Posted
• #9 ✎ 184 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies
Please give it a look and let me know if you have any questions about the code or concepts. I tried to leave you a ton of comments to help. ...
I think I'll have to get in touch with my good friend Petit Modem first haha Thanks for your effort. After I give the code a whirl I'll have more thoughts to share I'm sure.
Posted
• #10 ✎ 184 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies
You can calculate the next position directly from the current position as long as you know where the center is: (Here I'm assuming it to be x=0, y=0) IF -X>ABS(Y)-0.5 THEN 'up DEC Y ELSEIF X>ABS(Y+0.5) THEN 'down INC Y ELSEIF Y>ABS(X-0.5) THEN 'left DEC X ELSE 'right INC X ENDIF '(The last 5 lines can be simplified to ELSE : DEC X,SGN(Y) : ENDIF)
This is so shockingly simple and easy to follow
Posted
• #11 ✎ 184 MZ952 Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength Drawing I like to draw! Hobbies Reading I like to read books! Hobbies https://codegolf.stackexchange.com/q/173869/81375 Posted
• #12 ✎ 1 FantaSanta What's the best way to do this? Posted
• #13 ✎ 72 seggiepants If you want to code golf it, I would go with 12Me21's solution as code golf places a premium on smallest possible code size. My approach is decidedly verbose. If you want a general purpose tool that you can refer to over and over again for a multitude of problems, well I would suggest the Finite State Machine approach. I haven't tried, but either way, it should be fairly simple to start at the top left corner and run either one backwards. Just stop when you get to 0, 0. Posted