Here is JAL PolarToCartesian with 1 degree accuracy and byte based
lookup table

const byte SINLOOKUP[] = {0, 3, 8, 12, 17, 21, 26, 30, 35, 39, 43, 48,
52, 57,
    61, 65, 70, 74, 78, 82, 87, 91, 95, 99, 103, 107, 111, 115, 119,
123, 127,
    131, 135, 138, 142, 146, 149, 153, 157, 160, 164, 167, 170, 174,
177, 180,
    183, 186, 189, 192, 195, 198, 201, 203, 206, 209, 211, 214, 216,
218, 221,
    223, 225, 227, 229, 231, 233, 235, 236, 238, 240, 241, 242, 244,
245, 246,
    247, 248, 249, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255,
255, 255 }
-- 0 to 90 degrees in 1 degree steps
-- WARNING
-- Don't forget to to add 1 to lookup, apart from value zero !
-- use the SINLOOKUP cast to an sword +1, then divide expression by
256 when done.
-- fixed point Integer math

procedure PolarToCartesian(sword in radius, sword in angle, sword out
xPos, sword out yPos) is
    --make sure works for ANY angle ...
    While angle < -90   loop
        angle = angle + 360
    end Loop
    While angle > 270   loop
        angle = angle - 360
    end Loop
    If angle > -90 And angle < 0 Then
        angle = angle + 90
        xPos = sword(SINLOOKUP[angle]) + 1
        yPos = -sword(SINLOOKUP[90 - angle]) - 1
    ElsIf angle > 0 And angle < 90 Then
        yPos = sword(SINLOOKUP[angle]) + 1
        xPos = sword(SINLOOKUP[90 - angle]) + 1
    ElsIf angle > 90 And angle < 180 Then
        angle = angle - 90
        xPos = -sword(SINLOOKUP[angle]) - 1
        yPos = sword(SINLOOKUP[90 - angle]) + 1
    ElsIf angle > 180 And angle < 270 Then
        angle = angle - 180
        xPos = -sword(SINLOOKUP[90 - angle]) - 1
        yPos = -sword(SINLOOKUP[angle]) - 1
    End If
    Case angle of
    0, 360:
         xPos = radius
         yPos = 0
    90:
        xPos = 0
        yPos = radius
    180:
        xPos = -radius
        yPos = 0
    -90, 270:
         xPos = 0
         yPos = -radius
    otherwise
        xPos = xPos * radius / 256
        yPos = yPos * radius / 256
    End case
End procedure

Above is not tested!


The VB6 version below is working
Private Sub PolarToCartesian(ByVal radius As Integer, ByVal angle As
Integer, ByRef xPos As Integer, ByRef yPos As Integer)
Dim sinLookup As Variant
    'apart from zero, all values are 1 less than actual to allow
storage in 1 byte each
    sinLookup = Array(0, 3, 8, 12, 17, 21, 26, 30, 35, 39, 43, 48, 52,
57, 61, 65, 70, 74, 78, 82, 87, 91, 95, 99, 103, 107, 111, 115, 119,
123, 127, 131, 135, 138, 142, 146, _
    149, 153, 157, 160, 164, 167, 170, 174, 177, 180, 183, 186, 189,
192, 195, 198, 201, 203, 206, 209, 211, 214, 216, 218, 221, 223, 225,
227, 229, _
    231, 233, 235, 236, 238, 240, 241, 242, 244, 245, 246, 247, 248,
249, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255, 255)
    'make sure works for ANY angle ...
    Do While angle <= -90
        angle = angle + 360
    Loop
    Do While angle >= 270
        angle = angle - 360
    Loop
    If angle > -90 And angle < 0 Then
        angle = angle + 90
        xPos = sinLookup(angle) + 1
        yPos = -sinLookup((90 - angle)) - 1
    ElseIf angle > 0 And angle < 90 Then
        yPos = sinLookup(angle) + 1
        xPos = sinLookup((90 - angle)) + 1
    ElseIf angle > 90 And angle < 180 Then
        angle = angle - 90
        xPos = -sinLookup(angle) - 1
        yPos = sinLookup((90 - angle)) + 1
    ElseIf angle > 180 And angle < 270 Then
        angle = angle - 180
        xPos = -sinLookup((90 - angle)) - 1
        yPos = -sinLookup(angle) - 1
    End If
    Select Case angle
    Case -90:
         xPos = 0
         yPos = -radius
    Case 0:
         xPos = radius
         yPos = 0
    Case 90:
        xPos = 0
        yPos = radius
    Case 180:
        xPos = -radius
        yPos = 0
    Case Else
        xPos = xPos * radius \ 256
        yPos = yPos * radius \ 256
    End Select
End Sub

dim  xOrg = 62
dim yOrg = 31
dim radius =31

Private Sub DrawClockFace(radius as Integer , xOrg as Integer, yOrg as
Integer)

Dim faceDots As Integer
Dim xPos As Integer
Dim yPos As Integer
    LCD_Init
    LCD_DrawRectangle xOrg, yOrg, 2, 2, True 'centre
    'draw minutes dots
    For faceDots = 0 To 59
        PolarToCartesian radius, faceDots * 6, xPos, yPos
        LCD_Plot xOrg + xPos, yOrg + yPos, True
    Next
    'draw hour squares
    For faceDots = 0 To 11
        PolarToCartesian radius, faceDots * 30, xPos, yPos
        LCD_DrawRectangle xOrg + xPos, yOrg + yPos, 2, 2, True
    Next
End Sub



> I multiplied the sin values by 4096 (maybe 256 is enough on smaller
> displays) to have an Integer lookup to do integer math with Radius.
> then divide result (power of 2 so a shift).

-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en.

Reply via email to