Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Everything in Degrees instead of Radians
#4
OK let's just slip that main code into a Sub and generalize to draw a Regular Polygon about any point x, y with given radius, dStart is where you want to start the polygon st it points out for sure at that angle, for instance we will want allot of Regular Polygons to point due North like 12 o'clock on clock so set dStart to 270 degrees. When testing any alpha color you will see a brighter point where the end line overlaps with the start line. We can do polyFills later where that won't occur.
Code: (Select All)
Option _Explicit
_Title "Sub for Regular Polygon Using Degrees" 'b+ 2022-10-12
Screen _NewImage(800, 600, 32) ' standard screen size 800 wide, 600 height for quick QB64 Demos with full color potential (the 32)
_ScreenMove 250, 50
Dim i

' Give polygon sub a random workout
Do
    Cls
    For i = 1 To 50
        RegularPoly Rnd * _Width, Rnd * _Height, Rnd * 100 + 5, Int(Rnd * 10) + 3, Rnd * 360, _RGB32(225 * Rnd + 30, 255 * Rnd, 255 * Rnd, 225 * Rnd + 30)
    Next
    Sleep
Loop Until _KeyDown(27)

Sub RegularPoly (cx, cy, radius, nPoints, dStart, K As _Unsigned Long)
    Dim secDegrees, p, x, y, saveX, saveY
    secDegrees = 360 / nPoints
    For p = 1 To nPoints
        x = cx + radius * CosD(dStart + p * secDegrees)
        y = cy + radius * SinD(dStart + p * secDegrees)
        If p = 1 Then PSet (x, y), K: saveX = x: saveY = y Else Line -(x, y), K
    Next
    Line -(saveX, saveY), K ' back to first point
End Sub

' use angles in degrees units instead of radians (converted inside sub)
Function CosD (degrees)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    CosD = Cos(_D2R(degrees))
End Function

' use angles in degrees units instead of radians (converted inside sub)
Function SinD (degrees)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    SinD = Sin(_D2R(degrees))
End Function

' use angles in degrees units instead of radians (converted inside sub)
Function DAtan2 (x1, y1, x2, y2) ' The angle in degrees a 2nd point (x2, y2)  makes to a first point (x1, y1)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    ' Delta means change between 1 measure and another for example x2 - x1
    Dim deltaX, deltaY, rtn
    deltaX = x2 - x1
    deltaY = y2 - y1
    '  To find the angle point(x2, y2) makes to (x1, y1) in Degrees
    ' Take DegreeAngle = DAtan2(y2 - y1, x2 - x1)
    rtn = _R2D(_Atan2(deltaY, deltaX))
    If rtn < 0 Then DAtan2 = rtn + 360 Else DAtan2 = rtn
End Function

' use angles in degrees units instead of radians (converted inside sub)
Sub ArrowTo (BaseX As Long, BaseY As Long, dAngle As Double, lngth As Long, colr As _Unsigned Long)
    Dim As Long x1, y1, x2, y2, x3, y3
    Dim As Double rAngle
    rAngle = _D2R(dAngle)
    x1 = BaseX + lngth * Cos(rAngle)
    y1 = BaseY + lngth * Sin(rAngle)
    x2 = BaseX + .8 * lngth * Cos(rAngle - _Pi(.05))
    y2 = BaseY + .8 * lngth * Sin(rAngle - _Pi(.05))
    x3 = BaseX + .8 * lngth * Cos(rAngle + _Pi(.05))
    y3 = BaseY + .8 * lngth * Sin(rAngle + _Pi(.05))
    Line (BaseX, BaseY)-(x1, y1), colr
    Line (x1, y1)-(x2, y2), colr
    Line (x1, y1)-(x3, y3), colr
End Sub

' use angles in degrees units instead of radians (converted inside sub)
Sub drawArc (xc, yc, radius, dStart, dMeasure, colr As _Unsigned Long)
    ' xc, yc Center for arc circle
    ' rStart is the Radian Start Angle, use _D2R for conversion from Degrees to Radians
    ' rMeasure is the measure of Arc in Radain units, use _D2R for conversion from Degrees to Radians
    ' Arc will start at rStart and go clockwise around for rMeasure Radians

    Dim rStart, rMeasure, rEnd, stepper, a, x, y
    rStart = _D2R(dStart)
    rMeasure = _D2R(dMeasure)
    rEnd = rStart + rMeasure
    stepper = 1 / radius ' the bigger the radius the smaller  the steps
    For a = rStart To rEnd Step stepper
        x = xc + radius * Cos(a)
        y = yc + radius * Sin(a)
        If a > rStart Then Line -(x, y), colr Else PSet (x, y), colr
    Next
End Sub

Correction(2022-10-15): I forgot to change the color of the first point from yellow to K, now corrected.
b = b + ...
Reply


Messages In This Thread
RE: Everything in Degrees instead of Radians - by bplus - 10-13-2022, 01:44 AM



Users browsing this thread: 4 Guest(s)