Author Topic: Reciprocal Shape Maker  (Read 3461 times)

0 Members and 1 Guest are viewing this topic.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Reciprocal Shape Maker
« on: August 27, 2020, 07:45:21 pm »
I've wanted to make this for a long time. It's probably been here before by others, but check out my version. I learned how to use the DRAW command to easily make them. I also figured out a neat way to make a color wheel for it for the shape and the background. Plus I added the _CLIPBOARDIMAGE when pressing C so people can copy the image and paste it to their own graphics program.
Sometimes the shape won't be directly centered on the screen, but that has to do with your size selection and number of sides you choose. If both are very large, it will only show up partially on the screen. Also, just so you know, size 10 can just be barely seen, which is the smallest. One more thing, if you choose a triangle or square, they will be tilted because that's how I use the DRAW command. All other shapes come out good I believe.

Code: QB64: [Select]
  1. 'Reciprocal Shape Maker by SierraKen
  2. 'Aug. 27, 2020
  3. SCREEN _NEWIMAGE(1000, 800, 32)
  4. again:
  5. _TITLE "Reciprocal Shape Maker"
  6. INPUT "Amount of sides (3-90):", s
  7. IF s > 90 OR s < 3 OR s <> INT(s) THEN GOTO again:
  8. 'The more sides you use and the greater size, the more of a chance it will go off the screen.
  9. INPUT "Size (10-200)", l
  10. IF l < 10 OR l > 200 OR l <> INT(l) THEN GOTO again:
  11. PRINT "Filled In (Y/N)?"
  12. yn:
  13. a$ = INKEY$
  14. IF a$ = "y" OR a$ = "Y" THEN fill = 1: GOTO colors:
  15. IF a$ = "n" OR a$ = "N" THEN fill = 0: GOTO colors:
  16. GOTO yn:
  17.  
  18. colors:
  19. c = c + 1
  20. IF c = 1 THEN LOCATE 2, 45: PRINT "Click A Shape Color With Your Mouse"
  21. IF c = 2 THEN LOCATE 2, 45: PRINT "      Click A Background Color"
  22. 'Color Wheel
  23. FOR r = 1 TO 255 STEP 2
  24.     sz = sz + .2
  25.     CIRCLE (500, 400), sz, _RGB32(r, 0, 0)
  26. FOR g = 1 TO 255 STEP 2
  27.     sz = sz + .2
  28.     CIRCLE (500, 400), sz, _RGB32(0, g, 0)
  29. FOR b = 1 TO 255 STEP 2
  30.     sz = sz + .2
  31.     CIRCLE (500, 400), sz, _RGB32(0, 0, b)
  32. FOR rg = 1 TO 255 STEP 2
  33.     sz = sz + .2
  34.     CIRCLE (500, 400), sz, _RGB32(rg, rg, 0)
  35. NEXT rg
  36. FOR gb = 1 TO 255 STEP 2
  37.     sz = sz + .2
  38.     CIRCLE (500, 400), sz, _RGB32(0, gb, gb)
  39. NEXT gb
  40. FOR rb = 1 TO 255 STEP 2
  41.     sz = sz + .2
  42.     CIRCLE (500, 400), sz, _RGB32(rb, 0, rb)
  43. NEXT rb
  44. FOR rg = 1 TO 255 STEP 2
  45.     sz = sz + .2
  46.     CIRCLE (500, 400), sz, _RGB32(rg, rg / 2, 0)
  47. NEXT rg
  48. FOR rg = 1 TO 255 STEP 2
  49.     sz = sz + .2
  50.     CIRCLE (500, 400), sz, _RGB32(rg / 2, rg, 0)
  51. NEXT rg
  52. FOR gb = 1 TO 255 STEP 2
  53.     sz = sz + .2
  54.     CIRCLE (500, 400), sz, _RGB32(0, gb / 2, gb)
  55. NEXT gb
  56. FOR gb = 1 TO 255 STEP 2
  57.     sz = sz + .2
  58.     CIRCLE (500, 400), sz, _RGB32(0, gb, gb / 2)
  59. NEXT gb
  60. FOR rb = 1 TO 255 STEP 2
  61.     sz = sz + .2
  62.     CIRCLE (500, 400), sz, _RGB32(rb, 0, rb / 2)
  63. NEXT rb
  64. FOR rb = 1 TO 255 STEP 2
  65.     sz = sz + .2
  66.     CIRCLE (500, 400), sz, _RGB32(rb / 2, 0, rb)
  67. NEXT rb
  68. FOR w = 1 TO 255 STEP 2
  69.     sz = sz + .2
  70.     CIRCLE (500, 400), sz, _RGB32(w, w, w)
  71. sz = 0
  72. mouseLeftButton = 0
  73.     _LIMIT 7
  74.     mouseWheel = 0
  75.         mouseX = _MOUSEX
  76.         mouseY = _MOUSEY
  77.         mouseLeftButton = _MOUSEBUTTON(1)
  78.         mouseRightButton = _MOUSEBUTTON(2)
  79.         mouseMiddleButton = _MOUSEBUTTON(3)
  80.         mouseWheel = mouseWheel + _MOUSEWHEEL
  81.     LOOP
  82.     IF mouseLeftButton = -1 AND c = 1 THEN mouseLeftButton = 0: col& = POINT(mouseX, mouseY): GOTO colors:
  83.     IF mouseLeftButton = -1 AND c = 2 THEN mouseLeftButton = 0: backcol& = POINT(mouseX, mouseY): GOTO nex:
  84.  
  85. 'Create Shape
  86. nex:
  87. c = 0
  88. _TITLE "Press C to copy to Clipboard. Space Bar for more. Esc to quit."
  89. image& = _NEWIMAGE(1000, 800, 32)
  90. SCREEN image&
  91. 'Paint background color.
  92. PAINT (2, 2), backcol&
  93. 'Set starting point and color with PSET.
  94. PSET (325, 400), col&
  95. 'TA for angle relative to U for up and 80 pixels per side.
  96. angle = 180 / s
  97. oldangle = angle
  98. FOR sides = 1 TO s
  99.     angle$ = STR$(angle)
  100.     'l = 180 / s
  101.     u$ = STR$(l)
  102.     DRAW "TA-" + angle$ + " U" + u$
  103.     angle = angle + (oldangle * 2)
  104. NEXT sides
  105. IF fill = 1 THEN PAINT (500, 400), col&
  106. angle = 0: oldangle = 0
  107.     a$ = INKEY$
  108.     IF a$ = "c" OR a$ = "C" THEN _CLIPBOARDIMAGE = image&: cp = 1
  109.     IF a$ = CHR$(27) THEN END
  110.     IF a$ = " " THEN GOTO again:
  111.     IF cp = 1 THEN
  112.         _DELAY .5
  113.         LOCATE 2, 60
  114.         COLOR col&, backcol&
  115.         PRINT "Picture Copied"
  116.         _DELAY 3
  117.         cp = 0
  118.         LOCATE 2, 60
  119.         COLOR col&, backcol&
  120.         PRINT "               "
  121.     END IF
  122.  




Kens Octagon from QB64.jpg
* Kens Octagon from QB64.jpg (Filesize: 30.7 KB, Dimensions: 1000x800, Views: 208)
« Last Edit: August 27, 2020, 07:50:25 pm by SierraKen »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Reciprocal Shape Maker
« Reply #1 on: August 27, 2020, 07:54:15 pm »
Also, don't ask how I got line 117 to work with this:   angle = angle + (oldangle * 2)
I still have no idea why it needs * 2 but it works! LOL

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Reciprocal Shape Maker
« Reply #2 on: August 28, 2020, 12:03:32 am »
@SierraKen  what you are drawing are Regular Polygons, many sided figures with equal side lengths.

Here is a demo for an easier way to control exactly where and how big your polygon will be:
Code: QB64: [Select]
  1. _TITLE "Basic Polygon" 'b+ 2020-08-27
  2.  
  3. ' a circle is 360 degree
  4. ' a polygon of n side has central angles = 360 / n  > think of a pie the central angle are the angle of slices in center
  5. SCREEN _NEWIMAGE(500, 500, 32)
  6. _DELAY .25
  7. xC = _WIDTH / 2 '  middle of screen
  8. yC = _HEIGHT / 2 ' ditto
  9. r = 200 '          radius = less than half screen height
  10. FOR n = 3 TO 12 ' n = number of sides
  11.     CLS
  12.     PRINT "Sides = "; n
  13.     CIRCLE (xC, yC), r ' here is our pie,  Apple or Pepperroni :-))
  14.     FOR angle = 0 TO 360 STEP 360 / n ' step the size of pie angles
  15.         ' let xC, yC be the coordinates at the center of the pie circle
  16.         ' let r be the radius of the pie
  17.         ' then the n outside points are
  18.         x = xC + r * COS(_D2R(angle)) ' x coordinate of outer edge point
  19.         y = yC + r * SIN(_D2R(angle)) ' y coordinate of outer edge point
  20.         IF angle = 0 THEN PSET (x, y) ELSE LINE -(x, y) ' outer edge edge
  21.         LINE (xC, yC)-(x, y) ' slice from center of pie to outer edge
  22.         _LIMIT 4
  23.     NEXT
  24.     _DELAY 2
  25.     PRINT "press any to see next polygon up to 12..."
  26.     SLEEP
  27. PRINT "Demo is done."
  28.  
  29.  

 
Basic Polygons.PNG


The angle 0 degrees points due East so first Sin(0) and Cos(0) is point all the way right in middle of screen.
From there we work Clockwise around the circle at increments of 360/n degrees until we return to 0 = 360 degrees.


I give this lecture because I don't think you can do this with your approach to drawing Polygons:
Code: QB64: [Select]
  1. _TITLE "Polygon Demo by bplus"
  2. ' polygon demo.bas for QB64 (B+=MGA) 2017-09-17
  3. CONST xmax = 700
  4. CONST ymax = 700
  5. SCREEN _NEWIMAGE(xmax, ymax, 32)
  6. _DELAY .25
  7. x0 = xmax / 2: y0 = ymax / 2
  8. FOR n = 3 TO 9
  9.     radius = 345
  10.     CLS
  11.     rr = RND * 75: gg = RND * 75: bb = RND * 75
  12.     FOR a = 0 TO _PI(2) STEP _PI(1 / 20)
  13.         radius = radius - 8
  14.         pc& = _RGB(radius / 345 * 200 + rr, radius / 345 * 200 + gg, radius / 345 * 200 + bb)
  15.         COLOR pc&
  16.         polygon x0, y0, radius, n, a
  17.         PAINT (x0, y0), pc&, pc&
  18.         _LIMIT 10
  19.     NEXT
  20.     SLEEP 2
  21.  
  22. SUB polygon (xOrigin, yOrigin, radius, nVertex, RadianAngleOffset)
  23.     polyAngle = _PI(2) / nVertex
  24.     x1 = xOrigin + radius * COS(polyAngle + RadianAngleOffset)
  25.     y1 = yOrigin + radius * SIN(polyAngle + RadianAngleOffset)
  26.     FOR i = 2 TO nVertex + 1
  27.         x2 = xOrigin + radius * COS(i * polyAngle + RadianAngleOffset)
  28.         y2 = yOrigin + radius * SIN(i * polyAngle + RadianAngleOffset)
  29.         LINE (x1, y1)-(x2, y2)
  30.         x1 = x2: y1 = y2
  31.     NEXT
  32.  
  33.  
Polygon demo.PNG

« Last Edit: August 28, 2020, 12:24:24 am by bplus »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Reciprocal Shape Maker
« Reply #3 on: August 28, 2020, 12:19:48 am »
Thanks B+. Yours is a trig. way of doing it. I guess I like my way with the DRAW command because it reminds me of the old 1980's programming language called LOGO. It was the very first thing I did on a computer back in the 7th grade in school. You told the "turtle" to walk steps and draw a line, then turn an angle and go another line, etc. :) But I kept yours and might learn from it sometime.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Reciprocal Shape Maker
« Reply #4 on: August 28, 2020, 12:37:56 am »
OK I guess we can do it that way too.


Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Reciprocal Shape Maker
« Reply #5 on: August 28, 2020, 12:53:54 am »
Wow... I like your second example. Someday I will learn the Trig functions like the palm of my hand. :) I saved that one too.

Offline loudar

  • Newbie
  • Posts: 73
  • improve it bit by bit.
    • View Profile
Re: Reciprocal Shape Maker
« Reply #6 on: August 28, 2020, 07:56:12 am »
That polygon() function is really neat @bplus I might have to adapt it for an art generator program I'm currently writing...
Check out what I do besides coding: http://loudar.myportfolio.com/