Friday, January 28, 2005

Gotcha, you little blighter!

I finally managed to find a solution to finding a point on an ellipse.

As is always the way, the answer was surprisingly simple. These 10-ish lines of code are the culmination of many frustrated cigarettes out on the front porch, constant annoyance for a few days, and a niggling feeling that yes, I am really a failure. I even have a small pile of paper covered with calculations trying to fathom how to do it. Don't you really hate it when it takes so much to produce what looks(at least to Mrs Mawoo) like so little.

So here it is. The code to find a point on the circumference of an ellipse based on the value you want to display.
Private Function GetHandEndPoint(Angle As Integer, BoundingRectangle As Rectangle) As PointF

Dim Radians As Double
Radians = Degrees * (System.Math.PI / 180)

With GetHandEndPoint
.X = CType((BoundingRectangle.Width / 2) + ((BoundingRectangle.Width / 2) * System.Math.Cos(Radians)), _
Integer)
.Y = CType(BoundingRectangle.Height / 2 + (BoundingRectangle.Height / 2) * System.Math.Sin(Radians), _
Integer)
End With
End Function
I can't believe I spent so long trying to work this out!

Note: Special credit goes to Paul for leaving the comment that spurred me to get this sorted. He managed to ask if I had in exactly the same tone that my mother used to use to ask me if I'd finished my homework. Thanks.

Edit: I know the angle isn't maintained when the ellipse is stretched out. This is intentional. Using this method of calculating the point gives (I reckon) a better display that going solely on angle. One of these days I'll post images to prove it!

No comments: