Author Topic: Fun Math Graphics  (Read 5867 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Fun Math Graphics
« on: July 22, 2019, 01:42:16 pm »
Yeah, math is not fun, but last night I was tinkering with my analog clock equations and finished this today. It's 11 pictures using the same equations, just different numbers. The 12th (last) one uses different equations but similar. Thanks to the Internet for the original first and last equations, I don't know how they work but they do. :) Feel free to use any of these in your own programs.

Code: QB64: [Select]
  1. 'There are 12 mathematically made pictures on this program.
  2. 'The first 11 use the same equations, just different numbers.
  3. 'The 12th is a little bit different.
  4. 'Feel free to use any of these in your own programs.
  5. 'Finished on July 22, 2019.
  6.  
  7. _TITLE "Fun Math Graphics"
  8. SCREEN _NEWIMAGE(640, 480, 32)
  9. PRINT "                             Fun Math Graphics"
  10. PRINT "                                 by Ken G."
  11. PRINT "         11 of these pictures are drawn using the same equations,"
  12. PRINT "         just with different numbers. The 12th is similar but different."
  13. INPUT "                           Press Enter to start.", a$
  14. PRINT "1"
  15. one:
  16. seconds = seconds + .01
  17. s = (60 - seconds) * 6 + 180
  18. x = INT(SIN(s / 180 * 3.141592) * 180) + 325
  19. y = INT(COS(s / 180 * 3.141592) * 180) + 225
  20. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  21. IF seconds > 60 THEN
  22.     seconds = 0
  23.     _DELAY 2
  24.     CLS
  25.     PRINT "2"
  26.     GOTO two:
  27. _DELAY .001
  28. GOTO one:
  29.  
  30. two:
  31. seconds = seconds + .01
  32. s = (60 - seconds) * 6 + 180
  33. x = INT(SIN(s / 90 * 3.141592) * 180) + 325
  34. y = INT(COS(s / 180 * 3.141592) * 180) + 225
  35. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  36. IF seconds > 60 THEN
  37.     seconds = 0
  38.     _DELAY 2
  39.     CLS
  40.     PRINT "3"
  41.     GOTO three:
  42. _DELAY .001
  43. GOTO two:
  44.  
  45. three:
  46. seconds = seconds + .01
  47. s = (60 - seconds) * 6 + 180
  48. x = INT(SIN(s / 180 * 3.141592) * 180) + 325
  49. y = INT(COS(s / 90 * 3.141592) * 180) + 225
  50. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  51. IF seconds > 60 THEN
  52.     seconds = 0
  53.     _DELAY 2
  54.     CLS
  55.     PRINT "4"
  56.     GOTO four:
  57. _DELAY .001
  58. GOTO three:
  59.  
  60. four:
  61. seconds = seconds + .01
  62. s = (60 - seconds) * 6 + 180
  63. x = INT(SIN(s / 135 * 3.141592) * 180) + 325
  64. y = INT(COS(s / 33.75 * 3.141592) * 180) + 225
  65. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  66. IF seconds > 60 THEN
  67.     seconds = 0
  68.     _DELAY 2
  69.     CLS
  70.     PRINT "5"
  71.     GOTO five:
  72. _DELAY .001
  73. GOTO four:
  74.  
  75. five:
  76. seconds = seconds + .01
  77. s = (60 - seconds) * 6 + 180
  78. x = INT(SIN(s / 360 * 3.141592) * 180) + 325
  79. y = INT(COS(s / 45 * 3.141592) * 180) + 225
  80. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  81. IF seconds > 60 THEN
  82.     seconds = 0
  83.     _DELAY 2
  84.     CLS
  85.     PRINT "6"
  86.     GOTO six:
  87. _DELAY .001
  88. GOTO five
  89.  
  90. six:
  91. seconds = seconds + .01
  92. s = (60 - seconds) * 6 + 180
  93. x = INT(SIN(s / 90 * 3.141592) * 180) + 325
  94. y = INT(COS(s / 360 * 3.141592) * 180) + 225
  95. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  96. IF seconds > 60 THEN
  97.     seconds = 0
  98.     _DELAY 2
  99.     CLS
  100.     PRINT "7"
  101.     GOTO seven:
  102. _DELAY .001
  103. GOTO six:
  104.  
  105. seven:
  106. seconds = seconds + .01
  107. s = (60 - seconds) * 6 + 180
  108. x = INT(SIN(s / 45 * 3.141592) * 180) + 325
  109. y = INT(COS(s / 360 * 3.141592) * 180) + 225
  110. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  111. IF seconds > 60 THEN
  112.     seconds = 0
  113.     _DELAY 2
  114.     CLS
  115.     PRINT "8"
  116.     GOTO eight:
  117. _DELAY .001
  118. GOTO seven:
  119.  
  120. eight:
  121. seconds = seconds + .01
  122. s = (60 - seconds) * 6 + 180
  123. x = INT(SIN(s / 120 * 3.141592) * 180) + 325
  124. y = INT(COS(s / 5.1428 * 3.141592) * 180) + 225
  125. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  126. IF seconds > 60 THEN
  127.     seconds = 0
  128.     _DELAY 2
  129.     CLS
  130.     PRINT "9"
  131.     GOTO nine:
  132. _DELAY .001
  133. GOTO eight:
  134.  
  135. nine:
  136. seconds = seconds + .01
  137. s = (60 - seconds) * 6 + 180
  138. x = INT(SIN(s / 360 * 3.141592) * 180) + 325
  139. y = INT(COS(s / 6 * 3.141592) * 180) + 225
  140. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  141. IF seconds > 60 THEN
  142.     seconds = 0
  143.     _DELAY 2
  144.     CLS
  145.     PRINT "10"
  146.     GOTO ten
  147. _DELAY .001
  148. GOTO nine:
  149.  
  150. ten:
  151. seconds = seconds + .01
  152. s = (60 - seconds) * 6 + 180
  153. x = INT(SIN(s / 360 * 3.141592) * 180) + 325
  154. y = INT(COS(s / 11.25 * 3.141592) * 180) + 225
  155. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  156. IF seconds > 60 THEN
  157.     seconds = 0
  158.     _DELAY 2
  159.     CLS
  160.     PRINT "11"
  161.     GOTO eleven:
  162. _DELAY .001
  163. GOTO ten:
  164.  
  165. eleven:
  166. seconds = seconds + .01
  167. s = (60 - seconds) * 6 + 180
  168. x = INT(SIN(s / 360 * 3.141592) * 180) + 325
  169. y = INT(COS(s / 1.57 * 3.141592) * 180) + 225
  170. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  171. IF seconds > 60 THEN
  172.     seconds = 0
  173.     _DELAY 2
  174.     CLS
  175.     PRINT "12"
  176.     GOTO twelve:
  177. _DELAY .001
  178. GOTO eleven:
  179.  
  180. twelve:
  181. s = 0
  182. FOR d = 160 TO 0 STEP -.125
  183.     s = s + 1
  184.     x = COS(s * 3.141592 / 180) * d
  185.     y = SIN(s * 3.151492 / 180) * d
  186.     CIRCLE (x + 325, y + 225), 2, _RGB32(127, 216, 127)
  187.     _DELAY .001
  188.  
« Last Edit: July 22, 2019, 05:54:53 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fun Math Graphics
« Reply #1 on: July 22, 2019, 05:47:51 pm »
From my website:
Watch your computer draw a circle, elipses, curves, peaks, valleys, meshes of peaks, etc. all done with the same equation but just using different numbers. At the end I added different equations to make a spiral.
« Last Edit: July 22, 2019, 06:09:53 pm by SierraKen »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Fun Math Graphics
« Reply #2 on: July 23, 2019, 06:48:55 am »
Ken, you write some nice programs and examples, but you tend to work yourself to death doing so.

Your program comes in at 206 lines of code; here's the exact same thing, down to about 1/4th that size:

Code: QB64: [Select]
  1. 'There are 12 mathematically made pictures on this program.
  2. 'The first 11 use the same equations, just different numbers.
  3. 'The 12th is a little bit different.
  4. 'Feel free to use any of these in your own programs.
  5. 'Finished on July 22, 2019.
  6.  
  7. _TITLE "Fun Math Graphics"
  8. SCREEN _NEWIMAGE(640, 480, 32)
  9. PRINT "                             Fun Math Graphics"
  10. PRINT "                                 by Ken G."
  11. PRINT "                         Modified by Steve McNeill."
  12. PRINT "         11 of these pictures are drawn using the same equations,"
  13. PRINT "         just with different numbers. The 12th is similar but different."
  14. INPUT "                           Press Enter to start.", a$
  15. DrawIt 180, 180 '1
  16. DrawIt 90, 180 '2
  17. DrawIt 180, 90 '3
  18. DrawIt 135, 33.75 '4
  19. DrawIt 360, 45 '5
  20. DrawIt 90, 360 '6
  21. DrawIt 45, 360 '7
  22. DrawIt 120, 5.1428 '8
  23. DrawIt 360, 6 '9
  24. DrawIt 360, 11.25 '10
  25. DrawIt 360, 1.52 '11
  26.  
  27. CLS: PRINT "12"
  28. FOR d = 160 TO 0 STEP -.125
  29.     s = s + 1
  30.     x = COS(s * 3.141592 / 180) * d
  31.     y = SIN(s * 3.151492 / 180) * d
  32.     CIRCLE (x + 325, y + 225), 2, _RGB32(127, 216, 127)
  33.     _DELAY .001
  34.  
  35. SUB DrawIt (xmod, ymod)
  36.     STATIC N: N = N + 1
  37.     CLS: PRINT N
  38.     DO
  39.         seconds = seconds + .01
  40.         s = (60 - seconds) * 6 + 180
  41.         x = INT(SIN(s / xmod * 3.141592) * 180) + 325
  42.         y = INT(COS(s / ymod * 3.141592) * 180) + 225
  43.         CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  44.         _DELAY .001
  45.     LOOP UNTIL seconds > 60
  46.     _DELAY 2

When you have "11 pictures using the same equations, just different numbers", you don't need to write 11 different code segments for those equations.  You only need to write one SUB, and call it with the variables which change.

IF you wanted to change your program so that it would run with 100 different examples, instead of 11, you'd have to go in and copy/paste your work 100 different times and then modify the goto labels and amounts.  IF I wanted to change my alteration of your work to run 100, different examples, all I'd have to do is just go in and call DrawIt with an additional 89 xmod/ymod values...

Use of SUB/FUNCTION reduces the burden on both the programmer and the program significantly.  You definitely should look into incorporating their usage into your programs ASAP, and you'll see that your productivity will increase by leaps and bounds, and your programs size/efficiency will also improve by notable effects.

And, one last caveat:  Since you're writing code to teach others, try to reduce the usage of GOTO so much in your code.  It makes the readabily and understanding of the code much more difficult to follow (and heavy usage is one reason why some programmers call BASIC a language which makes programmers stupid), and often its not needed at all.  Let's take a moment to compare two code structures:

Code: QB64: [Select]
  1. PRINT "1"
  2. one:
  3. seconds = seconds + .01
  4. s = (60 - seconds) * 6 + 180
  5. x = INT(SIN(s / 180 * 3.141592) * 180) + 325
  6. y = INT(COS(s / 180 * 3.141592) * 180) + 225
  7. CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  8. IF seconds > 60 THEN
  9.     seconds = 0
  10.     _DELAY 2
  11.     CLS
  12.     PRINT "2"
  13.     GOTO two:
  14. _DELAY .001
  15. GOTO one:
  16.  
  17. two:

Compared to:

Code: QB64: [Select]
  1.     CLS
  2.     PRINT "1"
  3.     DO
  4.         seconds = seconds + .01
  5.         s = (60 - seconds) * 6 + 180
  6.         x = INT(SIN(s / 180 * 3.141592) * 180) + 325
  7.         y = INT(COS(s / 180 * 3.141592) * 180) + 225
  8.         CIRCLE (x, y), 2, _RGB32(127, 216, 127)
  9.         _DELAY .001
  10.     LOOP UNTIL seconds > 60
  11.     _DELAY 2

I don't know about you, but I can assure you which one I find easier to follow and understand, and I believe most people (and bosses) would agree that the second is "much cleaner code" than the first appears to be.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Fun Math Graphics
« Reply #3 on: July 23, 2019, 08:49:07 am »
...
I don't know about you, but I can assure you which one I find easier to follow and understand, and I believe most people (and bosses) would agree that the second is "much cleaner code" than the first appears to be.
I agree. But when I was new to QB64, just like SierraKen, I used to do same thing. This is because I didn't knew about SUBs, FUNCTIONs (and many other things.)
Then slowly, slowly I learned by many example codes done by Bosses.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Fun Math Graphics
« Reply #4 on: July 23, 2019, 11:52:08 am »
I totally agree with Ashish. Steve, when I started, the DIM command, DIM SHARED, was completely unknown to me. Same SUB and FUNCTIONS. Even though I have been using it for a long time, I am watched to your SUB as the calf at the new door, because it was you, and I thank you very much for showing it, who show me the two-way SUB. This means that you can return results using parameters. And there is still much new.
I remember - your _DEST,  _SOURCE examples thread. This was something great for me!
SierraKen, we all learning us still.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fun Math Graphics
« Reply #5 on: July 23, 2019, 12:08:47 pm »
Thank you SMcNeill. Yes I knew I could shorten it down to something like that, but it doesn't work me to death because all I do is copy/paste segments of code. Plus I wanted to make it easy for programmers to copy any segment they wanted to use in their own programs. Of course they could have done it your way as well but would have to know what they are looking for.  But I agree, I do need to learn SUBs more. I guess right now I'm at the point where I don't understand the difference between SUBs and just using a GOSUB / RETURN routine, which I'm starting to use more and more now and that also reduces the code. But I appreciate the comments and examples. I might look more into this later on. And yeah, I do need to learn more about what QB64 has to offer if I want to keep putting examples to learn by on my website and this forum. My mind is a slow learner. :) I've been doing BASIC in different kinds since the 80's, way back when you needed line numbers. LOL This was before SUBs or they would have taught us them in the college course I took and the High School class. But when QBasic came out, everything changed. :)

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Fun Math Graphics
« Reply #6 on: July 23, 2019, 12:47:22 pm »
There’s not a lot of differences in SUB and GOSUB routines, but I find them significant.  Biggest things to remember:

* SUB/FUNCTION are self-contained; GOSUB aren’t.

GOSUB Example:

Code: [Select]
x = 3
PRINT x

GOSUB AddOne
PRINT x

END

AddOne:
x = x + 1
PRINT x
RETURN

The above will print 3, 4, 4 as the three lines of results.

SUB example:
Code: [Select]
x = 3
PRINT x

AddOne
PRINT x

END

SUB AddOne
    x = x + 1
    PRINT x
END SUB

Now here, we’ll see 3, 1, 3 as the result.  AddOne is self-contained as a SUB, and x inside it has no relation to the value of x outside it.  When we call AddOne, SUB_x has a default value of 0, adds 1 to that, and SUB_x becomes 1.  The Main_x we set at a value of 3, and it never changes. 

This “self-contained” feature of SUB/FUNCTION makes them very suitable for creating libraries which we can then $INCLUDE into other programs.  GOSUB tends to be very Program specific, as its variables can affect the rest of your code, whereas SUB/FUNCTION doesn’t.



*SUB/FUNCTION have passable parameters; GOSUB doesn’t.

Since a SUB is self-contained, we need some way to pass values back and forth to it — the most basic way is via parameters.

Code: [Select]
x = 3
PRINT x

AddOne x
PRINT x

AddOne x
PRINT x

END

SUB AddOne (x)
    x = x + 1
END SUB

And here, we print 3, 4, 5.  We pass the value of x back and forth in the parenthesis when we declare our SUB.  (The (x) in this case.)



And that’s the very basics, to highlight the differences and help get you started on learning how to use them.  Just post when interested in learning more, and ask any questions you might have, and I — and the rest of the forum members, I’m certain — will be happy to help any way we can with your concerns.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fun Math Graphics
« Reply #7 on: July 24, 2019, 01:28:30 pm »
Thank you SMcNeill. So it's like a program within a program, very cool! I can almost visualize it, but I will need practice with it for awhile first. Not sure if I will use it yet or not though, my mind is so wrapped with GOTOs and GOSUBs and one even flow that adding more is REALLY different. lol

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Fun Math Graphics
« Reply #8 on: July 24, 2019, 02:45:48 pm »
One of the first subs I ever created and used over and over was LP(row, col, string$)

LP here stands for Locate and Print:, the sub's code is simply:
Code: QB64: [Select]
  1. SUB LP(row, col, string$)
  2.    LOCATE row, col: PRINT string$; '< probably want ; at end if printing near bottom of screen

It's handy because it saves typing out LOCATE and PRINT and combines two lines of code into one meaty one.
Screen design starts with deciding where to place and print everything.
« Last Edit: July 24, 2019, 02:49:34 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Fun Math Graphics
« Reply #9 on: July 24, 2019, 03:30:20 pm »
Thank you SMcNeill. So it's like a program within a program, very cool! I can almost visualize it, but I will need practice with it for awhile first. Not sure if I will use it yet or not though, my mind is so wrapped with GOTOs and GOSUBs and one even flow that adding more is REALLY different. lol

Once you get used to SUB/FUNCTION, you’ll slowly start to forget that GOSUB even exists.  There’s often very few advantages to using GOSUB, and a lot for using SUB.  Consider this code:

FOR I = 1 TO 10
     GOSUB AddOne
NEXT

END

AddOne:
FOR I = 1 TO 10
    x = x + 1
NEXT
PRINT x
RETURN

With the above, you’re just going to print 10 to the screen and end.  Your GOSUB changed the value of I and immediately jumped out of the previous loop when RETURN was called.

Now, what do we get with a SUB?

FOR I = 1 TO 10
    AddOne
NEXT

SUB AddOne
    STATIC x
    FOR I = 1 TO 10
        x = x + 1
    NEXT
    PRINT x
END SUB

And with the above, we print 10, 20, 30, ..., 100.  The I inside the loop has no effect on the I in the main module, meaning we’re not going to corrupt any values in our main program.



And your observation of, “its like a program within a program”, is spot on.  From the way I was taught waaay back in the early 90’s, GOSUB meant to GO to a SUB-routine.  SUB, however, was used to call a SUB-module — and that’s a huge difference all by itself.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fun Math Graphics
« Reply #10 on: July 24, 2019, 06:42:56 pm »
That's pretty neat SMcNeill, thanks. Will look into it sometime soon I think. I know the more I use new programming methods, the more I get used to it.

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Fun Math Graphics
« Reply #11 on: July 25, 2019, 11:28:58 am »
Love the math graphics program. Its almost a graphing calculator. Just needs some axis and tick marks and axis values.
QB64 is the best!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fun Math Graphics
« Reply #12 on: July 25, 2019, 12:04:50 pm »
LOL yep, thanks Jack.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Fun Math Graphics
« Reply #13 on: July 25, 2019, 07:10:09 pm »
I learned something new, and possibly quite timely. I was unaware of STATIC as a way to retain variable values in a SUB.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Fun Math Graphics
« Reply #14 on: July 25, 2019, 11:22:15 pm »
I just used 2 SUBs in the newly updated Tech Invaders game. Thank you for pushing me to use them! I had no idea how good they could be. But I'm still learning :). Read the last post or so on the Tech Invaders thread.