Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Little Challenge: Spin the Circle
#21
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?
Reply
#22
Woah, that's pretty tight coding, Steve.  I ain't even gonna try to shorten that.

- Dav

Find my programs here in Dav's QB64 Corner
Reply
#23
(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?
Reply
#24
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.
Reply
#25
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?
Reply




Users browsing this thread: 2 Guest(s)