10-10-2024, 10:33 PM
Instead of Asteroid making I did a host or regular Polygons to demo Sin and Cos Function with Angles and Movement for lesson 17
Code: (Select All)
_Title "Regular Polygons Demo of Sin and Cos" ' bplus 2024-10-10
Option _Explicit
Type XYPair
As Single X, Y
End Type
Type PolyType 'first 5 for DrawPoly
As XYPair Loc ' Location on screen
As Long NSides ' Number of sides = Number of points
As Single Radius ' From center to point
As Single Rotation ' Rotation offset = how much is poly turned
As _Unsigned Long Colr ' color
As XYPair V ' Vx, Vy Vector components for movement
As Single RotAmt ' Rotate Poly this angle amount
End Type
Dim Shared NPoly: NPoly = 20
Dim Shared Poly(1 To NPoly) As PolyType
Screen _NewImage(800, 600, 32)
_ScreenMove 250, 60
Dim As Long i
For i = 1 To NPoly
NewPoly i
Next
Do
Cls
For i = 1 To NPoly 'draw amd update polys
DrawPoly Poly(i).Loc, Poly(i).NSides, Poly(i).Radius, Poly(i).Rotation, Poly(i).Colr
' update poly position and rotation
Poly(i).Loc.X = Poly(i).Loc.X + Poly(i).V.X
If Poly(i).Loc.X < 0 Then Poly(i).Loc.X = 0: Poly(i).V.X = -Poly(i).V.X
If Poly(i).Loc.X > _Width Then Poly(i).Loc.X = _Width: Poly(i).V.X = -Poly(i).V.X
Poly(i).Loc.Y = Poly(i).Loc.Y + Poly(i).V.Y
If Poly(i).Loc.Y < 0 Then Poly(i).Loc.Y = 0: Poly(i).V.Y = -Poly(i).V.Y
If Poly(i).Loc.Y > _Height Then
Poly(i).Loc.Y = _Height: Poly(i).V.Y = -Poly(i).V.Y
If Poly(i).NSides < 9 Then Poly(i).NSides = Poly(i).NSides + 1 Else Poly(i).NSides = 3
End If
Poly(i).Rotation = Poly(i).Rotation + Poly(i).RotAmt
Next
_Display
_Limit 30
Loop Until _KeyDown(27) ' escape
Sub NewPoly (index) ' for shared array Poly() as PolyType
Dim r, red, grn, blu
Poly(index).Radius = Rnd * 60 + 20
Poly(index).Loc.X = Rnd * (_Width - 2 * Poly(index).Radius) + Poly(index).Radius
Poly(index).Loc.Y = Rnd * (_Height - 2 * Poly(index).Radius) + Poly(index).Radius
Poly(index).NSides = 3 + Int(Rnd * 7)
Poly(index).Rotation = Rnd * _Pi(2)
red = -Sgn(Rnd < .5) * (76 * Rnd + 180)
grn = -Sgn(Rnd < .5) * (156 * Rnd + 100)
blu = -Sgn(Rnd < .5) * (156 * Rnd + 100)
Poly(index).Colr = _RGB32(red, grn, blu)
Poly(index).V.X = Rnd * 3 + 1
If Rnd < .5 Then Poly(index).V.X = -Poly(index).V.X
Poly(index).V.Y = Rnd * 3 + 1
If Rnd < .5 Then Poly(index).V.Y = -Poly(index).V.Y
Poly(index).RotAmt = Rnd * _Pi(1 / 10) - _Pi(1 / 20)
End Sub
Sub DrawPoly (Place As XYPair, NSides As Long, Radius, RadRotation, C As _Unsigned Long)
Dim pAngle: pAngle = _Pi(2 / NSides) ' angles from center to side points
Dim a, savex, savey
For a = 0 To _Pi(1.9999) Step pAngle
If a = 0 Then
PSet (Place.X + Radius * Cos(a + RadRotation), Place.Y + Radius * Sin(a + RadRotation)), C
savex = Place.X + Radius * Cos(a + RadRotation)
savey = Place.Y + Radius * Sin(a + RadRotation)
Else
Line -(Place.X + Radius * Cos(a + RadRotation), Place.Y + Radius * Sin(a + RadRotation)), C
End If
Next
Line -(savex, savey), C
End Sub
b = b + ...