Chinchan Zu
Chinchan Zu

Reputation: 918

Javascript: compute the radius of circle given center point and another point

Suppose I have a circle with a center point at origin (0,0) and a 90deg point from it at (0,10)... from this 2 obvious points the radius of the circle would definitely be 10, right?

I researched that the formula of finding the radius based on center point and another point is:

Math.sqrt( ((x1-x2)*2)  + ((y1-y2)*2) )

but I'm getting a value of 4.47213595499958 instead of what I thought would be 10.

Can anyone teach me the correct formula I should use to make a perfect circle from a center point to another point?

Upvotes: 2

Views: 2552

Answers (2)

Igor
Igor

Reputation: 15893

Power in javascript is done by using Math.pow:

Math.sqrt( Math.pow((x1-x2), 2) + Math.pow((y1-y2), 2) )

Upvotes: 6

slebetman
slebetman

Reputation: 113878

In javascript, the * operator means multiply, not raise to the power. The correct formula should be:

Math.sqrt( ((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2)) )

Upvotes: 5

Related Questions