QB64 Phoenix Edition
A Little Challenge: Spin the Circle - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: A Little Challenge: Spin the Circle (/showthread.php?tid=3021)

Pages: 1 2 3


RE: A Little Challenge: Spin the Circle - SMcNeill - 09-12-2024

Code: (Select All)
Screen _NewImage(600, 400, 32) 'A Spinning Circle: bplus mod 2 of NakedApe, tweaked by Steve for LOC
Dim As Integer i, start, fini, col ' Playing with Aspects
Dim As _Unsigned Long col(-1 To 0): col(0) = &HFFFF0000: col(-1) = &HFF00FF00
Dim asp(1 To 90): asp(1) = 1: start = 1: fini = 90
For i = 2 To fini: asp(i) = 1 / Cos(_D2R(i)): Next ' make an array of aspect values
Do ' Note: asp(i) = Cos(_D2R(i)) will spin on the other axis
For i = start To fini Step Sgn(fini - start)
Cls
Circle (300, 200), 100, -1~&, , , asp(i)
Paint (300, 200), col(col), -1~&
_Limit 60
_Display
Next
Swap start, fini: If start = 90 Then col = Not col ' flip colors on odd cycles
Loop Until _KeyDown(27)

Cleaned it up a bit more and removed some more unnecessary variables and such. If there's anything that seems obtuse in this, just ask and I'll be more than happy to explain the idea behind my changes.

I'm really pretty happy with this little brainstorm of an idea:
Code: (Select All)
For i = start To fini Step Sgn(fini - start)

Now all one needs do is swap start and fini, and that loop will count up by 1 or down by 1 automagically! How's that for a neat little trick of logic?


RE: A Little Challenge: Spin the Circle - Dav - 09-12-2024

Woah, that's pretty tight coding, Steve.  I ain't even gonna try to shorten that.

- Dav


RE: A Little Challenge: Spin the Circle - SMcNeill - 09-12-2024

(09-12-2024, 11:44 PM)Dav Wrote: Woah, that's pretty tight coding, Steve.  I ain't even gonna try to shorten that.

- Dav


Code: (Select All)
Screen _NewImage(600, 400, 32) 'A Spinning Circle: bplus mod 2 of NakedApe, tweaked by Steve for LOC
Dim As Integer i, start, fini, col ' Playing with Aspects
Dim As _Unsigned Long col(-1 To 0): col(0) = &HFFFF0000: col(-1) = &HFF00FF00
Dim asp(1 To 90): asp(1) = 1: start = 1: fini = 90
For i = 2 To fini: asp(i) = 1 / Cos(_D2R(i)): Next ' make an array of aspect values
flip_coin: For i = start To fini Step Sgn(fini - start)
_Display: Cls
Circle (300, 200), 100, -1~&, , , asp(i)
Paint (300, 200), col(col), -1~&
If _KeyDown(27) Then System Else _Limit 60
Next
Swap start, fini: If start = 90 Then col = Not col ' flip colors on odd cycles
GoTo flip_coin

Think that's about as compact as I can get it. This version even makes the ESC sequence more responsive by keeping it inside the innermost loop. At this point, all I can see is just merging lines with colons to try and pretend like we're getting them shorter.

13 lines isn't so bad for something like this, is it?


RE: A Little Challenge: Spin the Circle - NakedApe - 09-13-2024

Sure, @SMcNeill, would you explain this dark magic?

For i = start To fini Step Sgn(fini - start)

And I like this, Steve:
  If _KeyDown(27) Then System Else _Limit 60

That's getting your _LIMIT in there for free! Tricky.


RE: A Little Challenge: Spin the Circle - SMcNeill - 09-13-2024

For i = start To fini Step Sgn(fini - start)

Start = 1: Fini = 90
SGN(90 - 1) = +1 'It's a positive number, so what we end up with is:

FOR i = 1 TO 90 STEP 1



Now, swap start and fini

Start = 90: Fini = 1
SGN (1 -90) = SGN (-89) = -1 it's a negative number, so we end up with:

FOR I = 90 TO 1 STEP -1



See the logic here?