Posts: 13
Threads: 4
Joined: Jun 2024
Reputation:
4
Code: (Select All)
Code: (Select All)
| Screen 12
| | Window (-160, 120)-(160, -120)
| | Const pi = 3.1415926# / 180
| | Dim x(100) As Double, y(100) As Double, Nx(100) As Single, Ny(100) As Single, r1 As Double
| | Dim r As Integer, n As Integer, i As Integer, j As Integer, m As Integer
| | Input "number of N angles of N-pointed star:", n
| | If n < 3 Or n > 100 Then End
| | r = 100
| | m = 0
| | r1 = r * Sin(((360 / n) * (1 / 4)) * pi) / Cos((360 / (2 * n)) * pi)
| | For i = 0 To 360 Step 360 / n
| | x(m) = r * Cos((i + 90) * pi)
| | y(m) = r * Sin((i + 90) * pi)
| | Nx(m) = r1 * Cos((126 + i) * pi)
| | Ny(m) = r1 * Sin((126 + i) * pi)
| | m = m + 1
| | Next i
| | If 360 Mod n <> 0 Then x(n) = x(0): y(n) = y(0)
| | For j = 0 To n - 1
| | Line (x(j), y(j))-(Nx(j), Ny(j)), 4
| | Line (Nx(j), Ny(j))-(x(j + 1), y(j + 1)), 4
| | Next j
|
Posts: 13
Threads: 4
Joined: Jun 2024
Reputation:
4
Code: (Select All)
Code: (Select All)
| Screen 12
| | Const pi = 3.1415926#
| | a% = 20
| | b% = 10
| | Dim deg As Double
| | Dim Shared As Single x2, y2
| | Window (-30, 30)-(30, -30)
| | Circle (0, 0), 2, 10
| | For theta = 0 To 360 Step 0.5
| | deg = theta * pi / 180
| | x = a% * Cos(deg)
| | y = b% * Sin(deg)
| | ' PSet (x, y), 10
| | u1 = 30 * pi / 180
| | u2 = 90 * pi / 180
| | u3 = 150 * pi / 180
| | Call rote(x, y, u1)
| | PSet (x2, y2), 10
| | Call rote(x, y, u2)
| | PSet (x2, y2), 10
| | Call rote(x, y, u3)
| | PSet (x2, y2), 10
| | Next theta
| | Sub rote (x1, y1, u)
| | x2 = x1 * Cos(u) - y1 * Sin(u)
| | y2 = y1 * Cos(u) + x1 * Sin(u)
| | End Sub
|
Posts: 4,161
Threads: 187
Joined: Apr 2022
Reputation:
243
12-09-2024, 12:26 PM
(This post was last modified: 12-09-2024, 01:10 PM by bplus.)
It's really radian = theta * pi / 180
and you take cos(radian) and sin(radian)
So the numbers are right but your words are misleading.
BTW QB64 has _Pi though uglier than just Pi you don't need the Pi = 3.14... line either.
Here is much more versatile routine with any inner and outer radius for star, any number of points, and color and you can fill it or not, demo of sub Star
This requires you to set color before calling Star to draw it.
Code: (Select All) Sub Star (x, y, rInner, rOuter, nPoints, angleOffset, TFfill)
' x, y are same as for circle,
' rInner is center circle radius
' rOuter is the outer most point of star
' nPoints is the number of points,
' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
' this is to allow us to spin the polygon of n sides
' TFfill filled True or False (1 or 0)
p_angle = Radians * (360 / nPoints): rad_angle_offset = Radians * angleOffset
x1 = x + rInner * Cos(rad_angle_offset)
y1 = y + rInner * Sin(rad_angle_offset)
For i = 0 To nPoints - 1
x2 = x + rOuter * Cos(i * p_angle + rad_angle_offset + .5 * p_angle)
y2 = y + rOuter * Sin(i * p_angle + rad_angle_offset + .5 * p_angle)
x3 = x + rInner * Cos((i + 1) * p_angle + rad_angle_offset)
y3 = y + rInner * Sin((i + 1) * p_angle + rad_angle_offset)
Line (x1, y1)-(x2, y2)
Line (x2, y2)-(x3, y3)
x1 = x3: y1 = y3
Next
If TFfill Then
'Circle (x, y), 2, &HFFFFFFFF
Paint (x, y), _DefaultColor, _DefaultColor
End If
End Sub
Demo
Code: (Select All) _Title "Even Better Stars" 'b+ 2021-11-18 trans of
'Better Stars.sdlbas (B+=MGA) 2016-05-16
' odd or even number of point, fat or skinny, better fills
Const Pi = _Acos(-1) 'cute way to get pi
'Print (Pi) 'check pi
'End
Const Radians = Pi / 180 'to convert an angle measured in degrees to and angle measure in radians, just mutiply by this
Const Xmax = 700
Const Ymax = 700
Const Cx = Xmax / 2
Const Cy = Ymax / 2
'setdisplay(xmax, ymax, 32, 1)
Screen _NewImage(Xmax, Ymax, 32)
_ScreenMove 300, 40
'setcaption("Better Stars demo")
'autoback(-2)
'main
Const NS = 100
Dim Shared x(NS), y(NS), dx(NS), dy(NS), ri(NS), ro(NS), p(NS), a(NS), turn(NS), fill(NS), c(NS) As _Unsigned Long
loopcounter = 0
For i = 0 To NS
NewStar i
Next
While _KeyDown(27) = 0
Line (0, 0)-(Xmax, Ymax), _RGB32(0, 0, 0, 30), BF
For i = 0 To NS
If x(i) > 0 And x(i) < Xmax And y(i) > 0 And y(i) < Ymax Then
'ink(colr(c(i)))
Color c(i)
Star x(i), y(i), ri(i), ro(i), p(i), a(i), fill(i)
x(i) = x(i) + dx(i)
y(i) = y(i) + dy(i)
ri(i) = 1.015 * ri(i)
ro(i) = 1.015 * ro(i)
a(i) = a(i) + turn(i)
Else
NewStar i
End If
Next
'screenswap
_Display
_Limit 100
'wait(50)
loopcounter = loopcounter + 1
Wend
Sub NewStar (nxt)
angle = Rnd * 2 * Pi
r = Rnd * 6 + 1
dx(nxt) = r * Cos(angle)
dy(nxt) = r * Sin(angle)
r = Rnd * 300
x(nxt) = Cx + r * dx(nxt)
y(nxt) = Cy + r * dy(nxt)
ri(nxt) = Rnd
ro(nxt) = ri(nxt) + 1 + Rnd
p(nxt) = 3 + Int(Rnd * 9)
a(nxt) = Rnd * 2 * Pi
turn(nxt) = Rnd * 6 - 3
fill(nxt) = Int(Rnd * 2)
c(nxt) = rndColor~&
End Sub
Function rndColor~& ()
rndColor~& = _RGB32(Rnd * 255, Rnd * 255, Rnd * 255)
End Function
Sub Star (x, y, rInner, rOuter, nPoints, angleOffset, TFfill)
' x, y are same as for circle,
' rInner is center circle radius
' rOuter is the outer most point of star
' nPoints is the number of points,
' angleOffset = angle offset IN DEGREES, it will be converted to radians in sub
' this is to allow us to spin the polygon of n sides
' TFfill filled True or False (1 or 0)
p_angle = Radians * (360 / nPoints): rad_angle_offset = Radians * angleOffset
x1 = x + rInner * Cos(rad_angle_offset)
y1 = y + rInner * Sin(rad_angle_offset)
For i = 0 To nPoints - 1
x2 = x + rOuter * Cos(i * p_angle + rad_angle_offset + .5 * p_angle)
y2 = y + rOuter * Sin(i * p_angle + rad_angle_offset + .5 * p_angle)
x3 = x + rInner * Cos((i + 1) * p_angle + rad_angle_offset)
y3 = y + rInner * Sin((i + 1) * p_angle + rad_angle_offset)
Line (x1, y1)-(x2, y2)
Line (x2, y2)-(x3, y3)
x1 = x3: y1 = y3
Next
If TFfill Then
'Circle (x, y), 2, &HFFFFFFFF
Paint (x, y), _DefaultColor, _DefaultColor
End If
End Sub
Oh ha! I was using my own way to get Pi not using _Pi either
Oh and this was before using _D2R(degrees) ie radians = _D2R(degrees) and the reverse of course is degrees = _R2D(radians) so we used Pi/180 or 180/Pi to convert degrees to radians or vice versa.
b = b + ...
Posts: 4,161
Threads: 187
Joined: Apr 2022
Reputation:
243
Thanks @macalwan I was impelled to update my own code for Star:
Code: (Select All)
| | | | | Sub star (x, y, rInner, rOuter, nPoints, DegAngleOffset, K As _Unsigned Long, FillTF) | | | | | | | | | | | | | | Dim pAngle, radAngleOffset, x1, y1, x2, y2, x3, y3, i As Long | | | | pAngle = _D2R(360 / nPoints): radAngleOffset = _D2R(DegAngleOffset) | | x1 = x + rInner * Cos(radAngleOffset) | | y1 = y + rInner * Sin(radAngleOffset) | | For i = 0 To nPoints - 1 | | x2 = x + rOuter * Cos(i * pAngle + radAngleOffset + .5 * pAngle) | | y2 = y + rOuter * Sin(i * pAngle + radAngleOffset + .5 * pAngle) | | x3 = x + rInner * Cos((i + 1) * pAngle + radAngleOffset) | | y3 = y + rInner * Sin((i + 1) * pAngle + radAngleOffset) | | If FillTF Then | | fTri x1, y1, x2, y2, x3, y3, K | | Line (x3, y3)-(x1, y1), K | | Line (x1, y1)-(x2, y2), K | | Line (x2, y2)-(x3, y3), K | | Else | | Line (x1, y1)-(x2, y2), K | | Line (x2, y2)-(x3, y3), K | | End If | | x1 = x3: y1 = y3 | | Next | | If FillTF Then Paint (x, y), K, K | | End Sub | | | | Sub fTri (x1, y1, x2, y2, x3, y3, K As _Unsigned Long) | | Dim D As Long | | Static a& | | D = _Dest | | If a& = 0 Then a& = _NewImage(1, 1, 32) | | _Dest a& | | _DontBlend a& | | PSet (0, 0), K | | _Blend a& | | _Dest D | | _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3) | | End Sub |
And it's Demo:
Code: (Select All)
| Option _Explicit | | _Title "Better Stars 4" | | | | | | | | | | | | | | Const Xmax = 700 | | Const Ymax = 700 | | Const Cx = Xmax / 2 | | Const Cy = Ymax / 2 | | | | Screen _NewImage(Xmax, Ymax, 32) | | _ScreenMove 300, 40 | | | | | | Const NS = 100 | | Dim Shared x(NS), y(NS), dx(NS), dy(NS), ri(NS), ro(NS), p(NS), a(NS), turn(NS), fill(NS), c(NS) As _Unsigned Long | | Dim As Long Loopcounter, i | | | | Loopcounter = 0 | | For i = 0 To NS | | NewStar i | | Next | | While _KeyDown(27) = 0 | | If _KeyDown(19200) Then | | For i = 0 To NS | | x(i) = x(i) + 2 * ri(i) ^ 2 | | dx(i) = dx(i) + 1 | | Next | | End If | | | | If _KeyDown(19712) Then | | For i = 0 To NS | | x(i) = x(i) - 2 * ri(i) ^ 2 | | dx(i) = dx(i) - 1 | | Next | | End If | | | | If _KeyDown(18432) Then | | For i = 0 To NS | | y(i) = y(i) + 2 * ri(i) ^ 2 | | dy(i) = dy(i) + 1 | | Next | | End If | | If _KeyDown(20480) Then | | For i = 0 To NS | | y(i) = y(i) - 2 * ri(i) ^ 2 | | dy(i) = dy(i) - 1 | | Next | | End If | | | | Line (0, 0)-(Xmax, Ymax), _RGB32(0, 0, 0, 10), BF | | For i = 0 To NS | | If x(i) > 0 And x(i) < Xmax And y(i) > 0 And y(i) < Ymax Then | | star x(i), y(i), ri(i), ro(i), p(i), a(i), c(i), fill(i) | | x(i) = x(i) + dx(i) | | y(i) = y(i) + dy(i) | | ri(i) = 1.015 * ri(i) | | ro(i) = 1.015 * ro(i) | | a(i) = a(i) + turn(i) | | Else | | NewStar i | | End If | | Next | | _Display | | _Limit 120 | | Loopcounter = Loopcounter + 1 | | Wend | | | | Sub NewStar (nxt) | | Dim angle, r | | angle = Rnd * 2 * _Pi | | r = Rnd * 6 + 1 | | dx(nxt) = r * Cos(angle) | | dy(nxt) = r * Sin(angle) | | r = Rnd * 300 | | x(nxt) = Cx + r * dx(nxt) | | y(nxt) = Cy + r * dy(nxt) | | ri(nxt) = Rnd | | ro(nxt) = ri(nxt) + 1 + Rnd | | p(nxt) = 3 + Int(Rnd * 9) | | a(nxt) = Rnd * 2 * _Pi | | turn(nxt) = Rnd * 6 - 3 | | fill(nxt) = Int(Rnd * 2) | | c(nxt) = _RGB32(Rnd * 255, Rnd * 255, Rnd * 255) | | End Sub | | | | | | Sub star (x, y, rInner, rOuter, nPoints, DegAngleOffset, K As _Unsigned Long, FillTF) | | | | | | | | | | | | | | Dim pAngle, radAngleOffset, x1, y1, x2, y2, x3, y3, i As Long | | | | pAngle = _D2R(360 / nPoints): radAngleOffset = _D2R(DegAngleOffset) | | x1 = x + rInner * Cos(radAngleOffset) | | y1 = y + rInner * Sin(radAngleOffset) | | For i = 0 To nPoints - 1 | | x2 = x + rOuter * Cos(i * pAngle + radAngleOffset + .5 * pAngle) | | y2 = y + rOuter * Sin(i * pAngle + radAngleOffset + .5 * pAngle) | | x3 = x + rInner * Cos((i + 1) * pAngle + radAngleOffset) | | y3 = y + rInner * Sin((i + 1) * pAngle + radAngleOffset) | | If FillTF Then | | fTri x1, y1, x2, y2, x3, y3, K | | Line (x3, y3)-(x1, y1), K | | Line (x1, y1)-(x2, y2), K | | Line (x2, y2)-(x3, y3), K | | Else | | Line (x1, y1)-(x2, y2), K | | Line (x2, y2)-(x3, y3), K | | End If | | x1 = x3: y1 = y3 | | Next | | If FillTF Then Paint (x, y), K, K | | End Sub | | | | Sub fTri (x1, y1, x2, y2, x3, y3, K As _Unsigned Long) | | Dim D As Long | | Static a& | | D = _Dest | | If a& = 0 Then a& = _NewImage(1, 1, 32) | | _Dest a& | | _DontBlend a& | | PSet (0, 0), K | | _Blend a& | | _Dest D | | _MapTriangle _Seamless(0, 0)-(0, 0)-(0, 0), a& To(x1, y1)-(x2, y2)-(x3, y3) | | End Sub |
b = b + ...
Posts: 570
Threads: 101
Joined: Apr 2022
Reputation:
47
12-09-2024, 09:44 PM
(This post was last modified: 12-09-2024, 09:52 PM by SierraKen.)
Awesome stuff guys! A couple days ago I found out how to make a 5 sided star. But I've also wanted to make the user be able to add as many points as they wish. I tried and tried but since I don't know a lot of math, I used Chat GPT and got this.
Macalwen, you might want to look at this code and see the explanations if you want to make pointed stars. I use this kind of math for different graphics and my clocks. But since I don't know how they actually work, I just copy the code to each thing I use. I also like experimenting with the numbers, variables, and equations. But I do like the way you did yours with the circles!
B+, as usual, I am amazed.
Code: (Select All)
| | | | | | | Screen _NewImage(800, 600, 32) | | | | start: | | Clear | | Cls | | | | | | Input "Enter the number of spikes for the star (5 or more): "; numSpikes | | | | If numSpikes > 10000 Then | | Print "The number of spikes cannot be more than 10000, using 10000 spikes instead" | | numSpikes = 10000 | | End If | | | | | | If numSpikes < 5 Then | | Print "The number of spikes must be at least 5. Using 5 spikes instead." | | numSpikes = 5 | | End If | | | | | | cx = 400 | | cy = 300 | | | | | | Pi = _Pi | | | | | | size = 200 | | | | | | angleStep = 360 / numSpikes | | | | | | Dim pointsX(numSpikes) | | Dim pointsY(numSpikes) | | | | | | For i = 0 To numSpikes - 1 | | angle = i * angleStep | | pointsX(i) = cx + size * Cos(angle * Pi / 180) | | pointsY(i) = cy - size * Sin(angle * Pi / 180) | | Next | | | | | | For i = 0 To numSpikes - 1 | | | | Line (pointsX(i), pointsY(i))-(pointsX((i + 2) Mod numSpikes), pointsY((i + 2) Mod numSpikes)), _RGB32(255, 255, 255) | | Next | | | | Locate 35, 5: Input "Again (Y/N): ", ag$ | | If Left$(ag$, 1) = "y" Or Left$(ag$, 1) = "Y" Then GoTo start: | | | | End |
Posts: 677
Threads: 146
Joined: Apr 2022
Reputation:
64
I did this a while back. Every once in a while it doesn't make a complete star (not sure why). Someone might find this useful. Code: (Select All)
| Screen _NewImage(600, 500, 256) | | Dim st(100, 10) | | Const sx = 1: Const sy = 2: Const pnts = 3: Const sr1 = 4: Const sr2 = 5: Const klr = 6: Const styl = 7 | | Const hdg = 8: Const mr = 9: Const rtn = 10 | | Randomize Timer | | For st = 0 To 9 | | Print "starpoly: a variation of the polygon drawing routine I've posted earlier" | | Print | | Print "Style "; st | | Print "(press any key to continue)" | | | | starpoly 300, 250, 160, 82, 5, 180, 12, st | | | | any$ = Input$(1) | | Cls | | Next | | For s = 1 To 100 | | st(s, sr1) = Int(Rnd * 100) + 20 | | st(s, sr2) = Int(st(s, sr1) / (1.5 + Rnd * 4)) | | st(s, klr) = Int(Rnd * 256) | | st(s, pnts) = Int(5 + Rnd * 10) | | st(s, sx) = Int(Rnd * 700) + 50 | | st(s, sy) = Int(Rnd * 400) + 50 | | st(s, styl) = Int(Rnd * 13) - 3 | | If st(s, styl) < 1 Then st(s, styl) = 0 | | st(s, hdg) = Int(Rnd * 360) | | st(s, rtn) = Int(1 + Rnd * 360) | | st(s, mr) = Rnd * 4 | | Next | | Do | | Cls | | _Limit 60 | | | | For s = 1 To 100 | | starpoly st(s, sx), st(s, sy), st(s, sr1), st(s, sr2), st(s, pnts), st(s, rtn), st(s, klr), st(s, styl) | | st(s, sx) = st(s, sx) + st(s, mr) * Sin(0.01745329 * st(s, hdg)) | | st(s, sy) = st(s, sy) + st(s, mr) * Cos(0.01745329 * st(s, hdg)) | | st(s, rtn) = st(s, rtn) + Int(Rnd * 3) + Int(Rnd * 3) | | If st(s, sx) < -50 Or st(s, sx) > 850 Or st(s, sy) < -50 Or st(s, sy) > 550 Then | | st(s, sr1) = Int(Rnd * 100) + 20 | | st(s, sr2) = Int(st(s, sr1) / (1.5 + Rnd * 4)) | | st(s, klr) = Int(Rnd * 256) | | st(s, pnts) = Int(5 + Rnd * 10) | | st(s, sx) = Int(Rnd * 700) + 50 | | st(s, sy) = Int(Rnd * 400) + 50 | | st(s, styl) = Int(Rnd * 13) - 3 | | If st(s, styl) < 1 Then st(s, styl) = 0 | | | | st(s, hdg) = Int(Rnd * 360) | | st(s, rtn) = Int(1 + Rnd * 360) | | st(s, mr) = Rnd * 4 | | End If | | st(s, mr) = st(s, mr) + 0.001 | | Next s | | Locate 1, 1: Print "<ESC> to exit" | | _Display | | kk$ = InKey$ | | Loop Until kk$ = Chr$(27) | | End | | | | | | Sub starpoly (cx, cy, rr, r2, points, turn, klr As _Unsigned Long, style) | | | | | | | | | | | | | | | | | | | | | | shapedeg = 360 / points | | Select EveryCase style | | Case 0 To 3 | | n = 0 | | x = rr * Sin(0.01745329 * turn) | | y = rr * Cos(0.01745329 * turn) | | Line (cx + x, cy + y)-(cx + x, cy + y), klr | | For deg = turn To turn + 360 Step shapedeg / 2 | | If n = 0 Then | | x2 = rr * Sin(0.01745329 * deg) | | y2 = rr * Cos(0.01745329 * deg) | | End If | | If n = 1 Then | | x2 = r2 * Sin(0.01745329 * deg) | | y2 = r2 * Cos(0.01745329 * deg) | | End If | | Line -(cx + x2, cy + y2), klr | | n = n + 1 | | If n = 2 Then n = 0 | | Next | | Case 1, 3 | | n = 0 | | For deg = turn To turn + 360 Step shapedeg / 2 | | If n = 0 Then | | x2 = rr * Sin(0.01745329 * deg) | | y2 = rr * Cos(0.01745329 * deg) | | Line (cx, cy)-(cx + x2, cy + y2), klr | | End If | | n = n + 1 | | If n = 2 Then n = 0 | | Next | | Case 2, 3 | | n = 0 | | For deg = turn To turn + 360 Step shapedeg / 2 | | If n = 0 Then | | Else | | x2 = r2 * Sin(0.01745329 * deg) | | y2 = r2 * Cos(0.01745329 * deg) | | Line (cx, cy)-(cx + x2, cy + y2), klr | | End If | | n = n + 1 | | If n = 2 Then n = 0 | | Next | | Case 4 To 9 | | shapedeg = shapedeg / 2 | | x = rr * Sin(0.01745329 * turn) | | y = rr * Cos(0.01745329 * turn) | | Line (cx + x, cy + y)-(cx + x, cy + y), klr | | n = 0 | | For deg = turn To turn + 360 Step shapedeg / 2 | | If n = 0 Then | | x2 = rr * Sin(0.01745329 * deg) | | y2 = rr * Cos(0.01745329 * deg) | | End If | | If n = 2 Or n = 4 Then | | x2 = ((r2 + rr) / 2) * Sin(0.01745329 * deg) | | y2 = ((r2 + rr) / 2) * Cos(0.01745329 * deg) | | End If | | If n = 1 Or n = 3 Then | | x2 = r2 * Sin(0.01745329 * deg) | | y2 = r2 * Cos(0.01745329 * deg) | | End If | | Line -(cx + x2, cy + y2), klr | | n = n + 1 | | If n = 4 Then n = 0 | | Next | | Case 5, 8, 9 | | n = 0 | | For deg = turn To turn + 360 Step shapedeg / 2 | | If n = 0 Then | | x2 = rr * Sin(0.01745329 * deg) | | y2 = rr * Cos(0.01745329 * deg) | | Line (cx, cy)-(cx + x2, cy + y2), klr | | End If | | | | n = n + 1 | | If n = 4 Then n = 0 | | Next | | Case 6, 9 | | n = 0 | | For deg = turn To turn + 360 Step shapedeg / 2 | | If n = 1 Or n = 3 Then | | x2 = r2 * Sin(0.01745329 * deg) | | y2 = r2 * Cos(0.01745329 * deg) | | Line (cx, cy)-(cx + x2, cy + y2), klr | | End If | | n = n + 1 | | If n = 4 Then n = 0 | | Next | | Case 7, 8, 9 | | n = 0 | | For deg = turn To turn + 360 Step shapedeg / 2 | | If n = 2 Or n = 4 Then | | x2 = ((r2 + rr) / 2) * Sin(0.01745329 * deg) | | y2 = ((r2 + rr) / 2) * Cos(0.01745329 * deg) | | Line (cx, cy)-(cx + x2, cy + y2), klr | | End If | | n = n + 1 | | If n = 4 Then n = 0 | | Next | | End Select | | End Sub |
Posts: 230
Threads: 24
Joined: Mar 2023
Reputation:
17
Very cool stars, @bplus and @James D Jarvis! I could get lost in those multi-verses...
Posts: 13
Threads: 4
Joined: Jun 2024
Reputation:
4
thanks for @bplus and @James D Jarvis @SierraKen,My variable name “DEG” wasn't quite right. “RAD” would be more accurate. Your fireworks were beautiful.
|