Everything in Degrees instead of Radians
#3
Hey there's a thought, do everything in o'clock units, AKA Minutes   ... wait that's what we are doing with degrees. OK

Here is introduction to Regular Polygons about the center of the screen. You might think of Regular polygons as pie slices with straight line edges instead of curve. We'll skip the cuts from the center and just straighten up the edges according to number of slices or points on the outer edge.


Code: (Select All)
Option _Explicit
_Title "Degrees for Regular Polygon" '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 cx, cy, radius, degrees, x, y, xOffset, yOffset, dial&, dStart, NPoints, secDegrees, p, saveX, saveY
cx = _Width / 2 ' middle of the screen point  center x
cy = _Height / 2 ' center y
radius = 250 ' max is 300 for height 600
ArrowTo cx, cy, 0, radius - 3, &HFFFFFFFF
For degrees = 0 To 359 Step 10 ' go around a full circle in degrees in steps of 10 degrees

    ' calculate and draw points around the center of the screen
    x = cx + radius * CosD(degrees) ' use CosD for x dimensions
    y = cy + radius * SinD(degrees) ' use SinD for y dimensions
    Circle (x, y), 1 ' draw bigger points than single pixel

    ' labeling the degree angles before or after the point ?
    If x < cx Then xOffset = -10 * Len(_Trim$(Str$(degrees))): yOffset = 0
    If x > cx Then xOffset = 4 * Len(_Trim$(Str$(degrees))): yOffset = 0
    If x = cx Then
        xOffset = -4 * Len(_Trim$(Str$(degrees)))
        If y > cy Then yOffset = 20 Else yOffset = -20
    End If
    _PrintString (x + xOffset, y - 8 + yOffset), _Trim$(Str$(degrees))
Next

' save our compass dial to image
dial& = _NewImage(_Width, _Height, 32)
_PutImage , 0, dial& ' screen to dial image stored

' A look at Regular Polygons about the Center of the Screen,
' say they all should start at 270 Degrees so they all point North
dStart = 270
For NPoints = 3 To 12
    secDegrees = 360 / NPoints ' how many degree is each section of poly eg triangle = 120 degree
    Cls
    _PutImage , dial&, 0
    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), &HFFFFFF00: saveX = x: saveY = y Else Line -(x, y), &HFFFFFF00
    Next
    Line -(saveX, saveY), &HFFFFFF00 ' back to first point
    Print "N points ="; NPoints; "  Section angle ="; secDegrees; " degrees,   zzz... press any for next polygon or end..."
    _Display ' stop the blinking
    Sleep '             .
Next

' 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
b = b + ...
Reply


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



Users browsing this thread: 2 Guest(s)