Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drawing Tools Subs or Functions with Demo
#47
I have revised my PieSlice drawing tools towards Steve's method only mine goes CW around the Center Point like the rest of the Trig Functions in Basic (but unlike an arc drawn with Circle Sub). In Basic Trig Functions angles increase around center point in Clockwise (CW) direction from East = 0 degrees/radians.

Code: (Select All)
' bplus mod to Steve way of coding 2024-10-13 but for East = 0 And CW arcs 10-14 rev Fill Paint tests
Sub PieSlice (CX As Long, CY As Long, R As Long, RAStart!, RAEnd!, C As _Unsigned Long, Fill As Long)
    Dim As Long x, y, x1, y1, xt, yt
    Dim As Single i, REnd ' rend to copy RAEnd! before changing value
    If RAStart! > RAEnd! Then REnd = RAEnd! + _Pi * 2 Else REnd = RAEnd! ' rev 10-14
    x1 = Cos(RAStart!) * R
    y1 = Sin(RAStart!) * R
    Line (CX, CY)-Step(x1, y1), C
    For i = RAStart! To REnd Step _D2R(Sgn(REnd - RAStart!))
        x = Cos(i) * R
        y = Sin(i) * R
        Line -(CX + x, CY + y), C
        If x <> x1 And y <> y1 And xt = 0 Then 'choose a point inside the arc to fill
            xt = CX + Cos(i) * R / 2
            yt = CY + Sin(i) * R / 2
        End If
    Next
    Line -(CX, CY), C
    If xt <> 0 And Fill <> 0 Then Paint (xt, yt), C ' rev 10-14  don't Paint unless found a free pixel inside slice
    'Circle ( xt,  yt), c
End Sub

Demo comparing Steve's Math Graph and mine consistent with Basic Funbctions and Screen Y axis:
Code: (Select All)
Option _Explicit
_Title "PieSlice Tests with Steve coding" ' bplus 2024-10-13 rev 10-14 option _explicit and paint test
$Color:32
Screen _NewImage(1200, 600, 32)
_ScreenMove 40, 60
Dim degree10, xc1, xc, yc, xc2, r, a, x1, y1, at, sa$, xoff, yoff, x2, y2
degree10 = _Pi(2 / 36) ' like hours on clock
xc1 = 300: xc2 = 900: yc = 300: r = 200
_PrintString (xc1 - _PrintWidth("CW  Regular East = 0 Deg/Rads Calcs") / 2, 292), "CW  Regular East = 0 Deg/Rads Calcs"
_PrintString (xc2 - _PrintWidth("CCW Steve's East = 0 Deg/Rads Calcs") / 2, 292), "CCW Steve's East = 0 Deg/Rads Calcs"

For a = 0 To _Pi(1.999) Step degree10

    ' Regular East = 0 calcs
    x1 = xc1 + r * Cos(a)
    y1 = yc + r * Sin(a)
    at = Int(_R2D(_Atan2(y1 - yc, x1 - xc1)) + .0001)
    If at < 0 Then at = at + 360
    sa$ = _Trim$(Str$(at))
    xoff = _PrintWidth(sa$) / 2
    yoff = 16 / 2
    _PrintString (x1 - xoff, y1 - yoff), sa$

    ' East = 0 calcs but Counter Clockwise
    x2 = xc2 + r * Sin(a + _Pi / 2)
    y2 = yc + r * Cos(a + _Pi / 2)
    at = Int(AtanSteve(xc2, yc, x2, y2))
    sa$ = _Trim$(Str$(at))
    xoff = _PrintWidth(sa$) / 2
    yoff = 16 / 2
    _PrintString (x2 - xoff, y2 - yoff), sa$

    _Limit 10
Next

PieSlice 80, 90, 80, _D2R(45), 0, Red, 1 ' 1 for fill
PieSlice 200, 200, 50, _D2R(270), _D2R(120), Green, 1 ' 1 for fill
PieSlice 400, 400, 50, _D2R(120), _D2R(270), Gold, 1 ' 1 for fill
PieSliceSteve 620, 90, 80, 45, 0, Red
PieSliceSteve 800, 200, 50, 270, 120, Green
PieSliceSteve 1000, 400, 50, 120, 270, Gold
Locate 33, 1: Print "PieSlice 80, 90, 80, _D2R(45), 0, Red, 1 ' 1 for fill";
Locate 34, 1: Print "PieSlice 200, 200, 50, _D2R(270), _D2R(120), Green, 1 ' 1 for fill";
Locate 35, 1: Print "PieSlice 400, 400, 50, _D2R(120), _D2R(270), Gold, 1 ' 1 for fill";
Locate 33, 76: Print "PieSliceSteve 620, 90, 80, 45, 0, Red";
Locate 34, 76: Print "PieSliceSteve 800, 200, 50, 270, 120, Green";
Locate 35, 76: Print "PieSliceSteve 1000, 400, 50, 120, 270, Gold";

' nice method for PieSlice but goes CCW not CW
Sub PieSliceSteve (cx As Long, cy As Long, r As Long, startAngle As Long, endAngle As Long, c As _Unsigned Long)
    Dim As Long x, y, x1, y1, i, xt, yt
    If startAngle > endAngle Then endAngle = endAngle + 360
    x1 = Sin(_D2R(startAngle + 90)) * r
    y1 = Cos(_D2R(startAngle + 90)) * r
    Line (cx, cy)-Step(x1, y1), c
    For i = startAngle To endAngle Step Sgn(endAngle - startAngle)
        x = Sin(_D2R(i + 90)) * r
        y = Cos(_D2R(i + 90)) * r
        Line -(cx + x, cy + y), c ' <<< bplus fixed cy + y not cx + y !!!!
        If x <> x1 And y <> y1 And xt = 0 Then 'chose a point inside the arc to fill
            xt = Sin(_D2R(i + 90)) * r / 2
            yt = Cos(_D2R(i + 90)) * r / 2
        End If
    Next
    Line -(cx, cy), c
    Paint (cx + xt, cy + yt), c
End Sub

Function AtanSteve (x1, y1, x2, y2)
    AtanSteve = (360 - _R2D(_Atan2(y2 - y1, x2 - x1))) Mod 360
End Function

' bplus mod to Steve way of coding 2024-10-13 but for East = 0 And CW arcs 10-14 rev Fill Paint tests
Sub PieSlice (CX As Long, CY As Long, R As Long, RAStart!, RAEnd!, C As _Unsigned Long, Fill As Long)
    Dim As Long x, y, x1, y1, xt, yt
    Dim As Single i, REnd ' rend to copy RAEnd! before changing value
    If RAStart! > RAEnd! Then REnd = RAEnd! + _Pi * 2 Else REnd = RAEnd! ' rev 10-14
    x1 = Cos(RAStart!) * R
    y1 = Sin(RAStart!) * R
    Line (CX, CY)-Step(x1, y1), C
    For i = RAStart! To REnd Step _D2R(Sgn(REnd - RAStart!))
        x = Cos(i) * R
        y = Sin(i) * R
        Line -(CX + x, CY + y), C
        If x <> x1 And y <> y1 And xt = 0 Then 'choose a point inside the arc to fill
            xt = CX + Cos(i) * R / 2
            yt = CY + Sin(i) * R / 2
        End If
    Next
    Line -(CX, CY), C
    If xt <> 0 And Fill <> 0 Then Paint (xt, yt), C ' rev 10-14  don't Paint unless found a free pixel inside slice
    'Circle ( xt,  yt), c
End Sub
b = b + ...
Reply


Messages In This Thread
RE: Drawing Tools Subs or Functions with Demo - by bplus - 10-14-2024, 04:19 PM



Users browsing this thread: 2 Guest(s)