Peter Pan
Peter Pan

Reputation: 21

Find the Point on a Circle Given an Angle and the Radius

I use code as below to draw a rectangle, a circle inside rectangle with radius 200, a center point (300,300) and after that I draw a point base on center point and a degrees is 90, but the result not as I want. I use the formula:

x = centerX + radius * cos(degrees )
y = centerY + radius * sin(degrees )

Here is the code:

Dim CAVtable As New Hashtable
Dim oCanvas As New Bitmap(507, 507)

Dim graphicsObj As Graphics = Graphics.FromImage(oCanvas)
graphicsObj.Clear(Color.White)

Dim pCenter As New Point(300, 300)
DrawPoint(pCenter, graphicsObj, Color.Blue)
Dim rc2 As New Rectangle(pCenter, New Size(1, 1))

Dim penARC2 As New Pen(Color.Red, 2)
rc2.Inflate(200, 200)
graphicsObj.DrawRectangle(Pens.Black, rc2)

graphicsObj.DrawArc(penARC2, rc2, 0, 360)

Dim xtem As Double = pCenter.X + 200 * Math.Cos(90.0!)
Dim ytem As Double = pCenter.Y + 200 * Math.Sin(90.0!)
Dim ptem As New Point(xtem, ytem)
DrawPoint(ptem, graphicsObj, Color.Blue)

Response.ContentType = "image/jpeg"
oCanvas.Save(Response.OutputStream, ImageFormat.Jpeg)
Response.End()

graphicsObj.Dispose()
oCanvas.Dispose()

The result is

actual results

Why is it not (because I use 90 degrees):

expected results

And more, I want calculate location point (x,y) on circle on coordinates as in the image below. How do I do it? What formula can I apply?

final desired result

Upvotes: 1

Views: 2121

Answers (1)

Jules SJ
Jules SJ

Reputation: 430

Math.Sin and Math.Cos use radians, not degrees. Try: Dim xtem As Double = pCenter.X + 200 * Math.Cos(90 * Math.PI / 180) Dim ytem As Double = pCenter.Y + 200 * Math.Sin(90 * Math.PI / 180)

Those are the x and y coordinates of a point on the circle.

Upvotes: 4

Related Questions