Thread: ellipse with angle canvas draw

1. ellipse with angle canvas draw

drawing the ellipse with an angle is always a problem, i found in the PowerBasic forum a thread very usefull for this purpose:
http://www.powerbasic.com/support/pb...ad.php?t=40315
here i port the program by Paul Dixon in that thread to thinbasic:
with a little variations
```'orogonal code from powerbasic forum http://www.powerbasic.com/support/pb...ad.php?t=40315 by Paul Dixon
Uses "UI","math"
Dim ScreenWidth     As Long = 640
Dim ScreenHeight    As Long = 480
Dim hWin            As DWord   '---Handle of the canvas window
Dim colr, colrFill As Long
Dim angle, cx, cy, semiminor, semimajor As Double
'-----------------------------------------------------------
' Main program
'-----------------------------------------------------------
hWin = Canvas_Window("Figures", 1, 1, ScreenWidth, ScreenHeight )
Canvas_Attach(hWin, 0, %TRUE) ' <- double buffer

'---Init canvas
Canvas_Clear(%BLACK)
Canvas_Width(5)
Type PolyPoint
x As Single
y As Single
End Type
Type PolyArray
COUNT As Long
xy(1024) As PolyPoint   'note, this 1024 represents the maximum points that can be handled
End Type
Local MyPolygon As PolyArray
MyPolygon.count = 200  'set the number of points to calculate
colr = Rgb(255, 100, 20) : colrFill = Rgb(205, 242, 255)
cx = ScreenWidth/5: cy = ScreenHeight/2
semiminor = 40 : semimajor = 100
For angle = 1 To 360 Step 1
cx = cx + 1 : 'cy = cy - 1
calculateEllipse( cx, cy, semiminor,semimajor,angle,MyPolygon)
Canvas_Polygon(MyPolygon  ,colr, colrFill)
Canvas_Redraw
'Sleep 2
Canvas_Clear(%BLACK)
Next
Canvas_WaitKey
Canvas_Window End
Sub calculateEllipse(x As Ext, y As Ext, a As Ext, b As Ext, angle As Ext, ReturnArray As PolyArray)
'based on algorithm found here: http://en.wikipedia.org/wiki/Ellipse
'x = x axis offset
'y = y axis offset
'a = semimajor axis of ellipse
'b = semiminor axis of ellipse
'angle = angle to the x-axis to draw ellipse in degrees

Local beta, cosbeta, sinbeta As Ext
Local alpha, cosalpha, sinalpha As Ext
Local i As Ext
Local index As Long
beta = -DegToRad(angle)
sinbeta = Sin(beta)
cosbeta = Cos(beta)
For i = 0 To 360 Step (360/ReturnArray.count)
alpha = DegToRad(i)
sinalpha = Sin(alpha)
cosalpha = Cos(alpha)
Incr index
ReturnArray.xy(index).x = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta)
ReturnArray.xy(index).y = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta)

Next
End Sub
```

2. Thanks for the example code, saved for future reference... I am sure it will come in handy.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•