Author Topic: Drawing Polygons  (Read 5061 times)

0 Members and 1 Guest are viewing this topic.

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Drawing Polygons
« on: November 25, 2019, 11:11:36 pm »
Hey all,
I've been trying to make a program that can draw any polygon (pentagon, octagon, pentacontagon, etc...), although I can't get it to work.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. l = 100
  3. s = 8
  4.  
  5. angle = (360 / s)
  6. lx = (_WIDTH / 2) + ((COS(angle)) * l)
  7. ly = (_HEIGHT / 2) + ((SIN(angle)) * l)
  8.  
  9. FOR n = 1 TO s
  10.     x = (_WIDTH / 2) + ((COS(angle)) * l)
  11.     y = (_HEIGHT / 2) + ((SIN(angle)) * l)
  12.  
  13.     LINE (lx, ly)-(x, y), _RGB(255, 255, 255)
  14.  
  15.     lx = x
  16.     ly = y
  17.  
  18.     angle = (360 / s) * n
  19.     PRINT angle
  20.  

l is the length of each line
s is the amount of sides i want.

No matter what I do either the shape doesnt line up or it overlaps.

Thanks,
Zep
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Drawing Polygons
« Reply #1 on: November 25, 2019, 11:28:05 pm »
Man you are so close! Give me a sec...
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. l = 100
  3. s = 8
  4.  
  5. angle = (360 / s)
  6. lx = _WIDTH / 2 + COS(_D2R(angle)) * l
  7. ly = _HEIGHT / 2 + SIN(_D2R(angle)) * l
  8. FOR n = 0 TO s
  9.  
  10.     x = _WIDTH / 2 + COS(_D2R(n * angle)) * l
  11.     y = _HEIGHT / 2 + SIN(_D2R(n * angle)) * l
  12.  
  13.     LINE (lx, ly)-(x, y), _RGB(255, 255, 255)
  14.  
  15.     lx = x
  16.     ly = y
  17.  
  18.  
  19.     PRINT n * angle
  20.  

SIN and COS use radians not degrees 2*PI = 360 degrees, so we had to convert the angles from degree to radians
_D2R() function.

Then n*angle under SIN and COS function, you had that I think, then  to go full circle you have to do the first point again, I just start a 0 and go to n sides of poly.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. l = 100
  3. FOR s = 3 TO 12
  4.     CLS
  5.     angle = (360 / s)
  6.     lx = _WIDTH / 2 + COS(_D2R(0)) * l
  7.     ly = _HEIGHT / 2 + SIN(_D2R(0)) * l
  8.     FOR n = 1 TO s
  9.         x = _WIDTH / 2 + COS(_D2R(n * angle)) * l
  10.         y = _HEIGHT / 2 + SIN(_D2R(n * angle)) * l
  11.         LINE (lx, ly)-(x, y), _RGB(255, 255, 255)
  12.         lx = x
  13.         ly = y
  14.         PRINT n * angle
  15.     NEXT n
  16.     _DELAY 1
« Last Edit: November 25, 2019, 11:43:57 pm by bplus »

Marked as best answer by Zeppelin on November 25, 2019, 11:53:46 pm

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Drawing Polygons
« Reply #2 on: November 25, 2019, 11:50:50 pm »
Subtract 90 degrees in the _D2R() to get them all to start drawing (pointing North or UP):
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2. l = 100
  3. FOR s = 3 TO 12
  4.     CLS
  5.     angle = (360 / s)
  6.     lx = _WIDTH / 2 + COS(_D2R(0 - 90)) * l
  7.     ly = _HEIGHT / 2 + SIN(_D2R(0 - 90)) * l
  8.     FOR n = 1 TO s
  9.         x = _WIDTH / 2 + COS(_D2R(n * angle - 90)) * l
  10.         y = _HEIGHT / 2 + SIN(_D2R(n * angle - 90)) * l
  11.         LINE (lx, ly)-(x, y), _RGB(255, 255, 255)
  12.         lx = x
  13.         ly = y
  14.         PRINT n * angle
  15.     NEXT n
  16.     _DELAY 1
  17.  

Offline Zeppelin

  • Newbie
  • Posts: 43
    • View Profile
    • Zeppelin Games ItchIo
Re: Drawing Polygons
« Reply #3 on: November 26, 2019, 04:47:45 am »
Thanks bPlus,
It's always a small issue that underpins what could've been :D

Zep
« Last Edit: November 26, 2019, 04:53:57 am by Zeppelin »
+[--->++<]>+.+++[->++++<]>.[--->+<]>+.-[---->+<]>++.+[->+++<]>+.+++++++++++.----------.[--->+<]>----.+[---->+<]>+++.---[->++++<]>.------------.+.++++++++++.+[---->+<]>+++.+[->+++<]>++.[--->+<]>+.[->+++<]>+.++++++++++.+.>++++++++++.

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: Drawing Polygons
« Reply #4 on: November 26, 2019, 03:13:14 pm »
? where are diagonals ?

 
komblife.gif



Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Drawing Polygons
« Reply #5 on: November 26, 2019, 04:40:52 pm »
Hi Danlin,

Ha! nice graphics, yours?

We leave the diagonals or chords as classic exercise with polygons along with drawing N pointed stars. :D
Oh, and twirling them as they streak across the screen... :)

https://www.google.com/search?client=opera&q=my+god+it%27s+full+of+stars&sourceid=opera&ie=UTF-8&oe=UTF-8
« Last Edit: November 26, 2019, 05:46:21 pm by bplus »

Offline DANILIN

  • Forum Regular
  • Posts: 128
    • View Profile
    • Danilin youtube
Re: Drawing Polygons
« Reply #6 on: November 26, 2019, 05:18:01 pm »
yes gif & youtube is my

in Russia task of connecting of vertices of polygon

this is a classical problem on loops and arrays

 
soedin.gif
 
grafline.gif
« Last Edit: November 26, 2019, 05:19:45 pm by DANILIN »
Russia looks world from future. big data is peace data.
https://youtube.com/playlist?list=PLBBTP9oVY7IagpH0g9FNUQ8JqmHwxDDDB
i never recommend anything to anyone and always write only about myself

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Drawing Polygons
« Reply #7 on: November 26, 2019, 09:45:59 pm »
Here is a star or two to wish upon or connect :D
Code: QB64: [Select]
  1. _TITLE "Star Worlds" 'B+ 2019-11-26
  2.  
  3. CONST xmax = 1200, ymax = 700, nS = 500
  4. TYPE starType
  5.     x AS SINGLE
  6.     y AS SINGLE
  7.     dx AS SINGLE
  8.     dy AS SINGLE
  9.     ri AS SINGLE
  10.     ro AS SINGLE
  11.     nP AS INTEGER
  12.     aOff AS SINGLE
  13.     da AS SINGLE
  14.     filled AS INTEGER
  15.     cc AS _UNSIGNED LONG
  16. DIM SHARED stars(1 TO nS) AS starType
  17.  
  18. SCREEN _NEWIMAGE(xmax, ymax, 32)
  19. _SCREENMOVE 100, 20
  20.  
  21. FOR i = 1 TO nS
  22.     newStar i
  23. WHILE _KEYDOWN(27) = 0
  24.     LINE (0, 0)-(xmax, ymax), _RGBA32(0, 0, 0, 30), BF
  25.     FOR i = 1 TO nS
  26.         drawStar i
  27.         stars(i).x = stars(i).x + stars(i).dx: stars(i).y = stars(i).y + stars(i).dy
  28.         IF stars(i).x < -50 OR stars(i).x > xmax + 50 OR stars(i).y < -50 OR stars(i).y > ymax + 50 THEN newStar i
  29.         stars(i).aOff = stars(i).aOff + stars(i).da
  30.     NEXT
  31.     _DISPLAY
  32.     _LIMIT 60
  33.  
  34. SUB newStar (i)
  35.     stars(i).x = irnd%(0, xmax)
  36.     stars(i).y = irnd%(0, ymax)
  37.     stars(i).ri = irnd%(1, 6)
  38.     stars(i).ro = stars(i).ri * (RND * 1 + 1.5)
  39.     stars(i).nP = irnd%(3, 12)
  40.     stars(i).aOff = RND * _PI(2)
  41.     IF RND < .5 THEN stars(i).da = RND * _PI / -45 ELSE stars(i).da = RND * _PI / 45
  42.     stars(i).filled = irnd%(0, 1)
  43.     stars(i).dx = irnd%(-10, 10)
  44.     stars(i).dy = irnd%(-10, 10)
  45.     stars(i).cc = _RGBA32(irnd%(128, 255), irnd%(128, 255), irnd%(128, 255), irnd%(1, 255))
  46.  
  47. FUNCTION irnd% (n1, n2) 'return an integer between 2 numbers
  48.     DIM l%, h%
  49.     IF n1 > n2 THEN l% = n2: h% = n1 ELSE l% = n1: h% = n2
  50.     irnd% = INT(RND * (h% - l% + 1)) + l%
  51.  
  52. SUB fIrrPoly (arr() AS SINGLE, c AS _UNSIGNED LONG)
  53.     'this just draws a bunch of triangles according to x, y points in arr()
  54.     DIM ox AS SINGLE, oy AS SINGLE, i AS INTEGER
  55.     ox = arr(0): oy = arr(1) 'the first 2 items in arr() need to be center
  56.     FOR i = 2 TO UBOUND(arr) - 3 STEP 2
  57.         ftri ox, oy, arr(i), arr(i + 1), arr(i + 2), arr(i + 3), c
  58.     NEXT
  59.  
  60. SUB drawStar (i AS INTEGER)
  61.     ' x, y are same as for circle,
  62.     ' rInner is center circle radius
  63.     ' rOuter is the outer most point of star
  64.     ' nPoints is the number of points,
  65.     ' angleOffset = angle offset in radians
  66.     ' this is to allow us to spin the star
  67.     DIM pAngle AS SINGLE, radAngleOffset AS SINGLE, p AS INTEGER, idx AS INTEGER
  68.     DIM ar(INT(stars(i).nP) * 4 + 3) AS SINGLE 'add two for origin
  69.  
  70.     pAngle = _PI(2) / stars(i).nP: radAngleOffset = stars(i).aOff - _PI(1 / 2)
  71.     ar(0) = stars(i).x: ar(1) = stars(i).y
  72.     ar(2) = stars(i).x + stars(i).ro * COS(radAngleOffset)
  73.     ar(3) = stars(i).y + stars(i).ro * SIN(radAngleOffset)
  74.     idx = 4
  75.     FOR p = 0 TO stars(i).nP - 1
  76.         ar(idx) = stars(i).x + stars(i).ri * COS(p * pAngle + radAngleOffset + .5 * pAngle)
  77.         idx = idx + 1
  78.         ar(idx) = stars(i).y + stars(i).ri * SIN(p * pAngle + radAngleOffset + .5 * pAngle)
  79.         idx = idx + 1
  80.         ar(idx) = stars(i).x + stars(i).ro * COS((p + 1) * pAngle + radAngleOffset)
  81.         idx = idx + 1
  82.         ar(idx) = stars(i).y + stars(i).ro * SIN((p + 1) * pAngle + radAngleOffset)
  83.         idx = idx + 1
  84.     NEXT
  85.     IF stars(i).filled THEN
  86.         fIrrPoly ar(), stars(i).cc
  87.     ELSE
  88.         FOR p = 2 TO stars(i).nP * 4 + 3 STEP 2
  89.             IF p > 2 THEN LINE (ar(p - 2), ar(p - 1))-(ar(p), ar(p + 1)), stars(i).cc
  90.         NEXT
  91.     END IF
  92.  
  93. SUB ftri (x1, y1, x2, y2, x3, y3, K AS _UNSIGNED LONG)
  94.     DIM D AS LONG
  95.     STATIC a&
  96.     D = _DEST
  97.     IF a& = 0 THEN a& = _NEWIMAGE(1, 1, 32)
  98.     _DEST a&
  99.     PSET (0, 0), K
  100.     _DEST D
  101.     _MAPTRIANGLE _SEAMLESS(0, 0)-(0, 0)-(0, 0), a& TO(x1, y1)-(x2, y2)-(x3, y3)
  102.  
  103.  

Until now, I never did get around to drawing filled polygons.

Oh! I just realized, run this long enough and all the stars will become stationary, survival of the slowest.
« Last Edit: November 26, 2019, 10:21:51 pm by bplus »