Reputation: 69
I am trying to test if a path or polyline describes a circle in java. How can i do this? I am thinking about testing if the biggest distance between two points is nearly the same for all points on the path (with some mistake in mind). Can this work? What other possibilities did I have to check that?
Upvotes: 3
Views: 634
Reputation: 51845
Assuming 2D...
compute bbox
so find min and max x,y
coordinate values from all the sample points x[i],y[i]
you got. Lets call them x0,y0,x1,y1
where x0<=x1
and y0<=y1
compute center of circle cx,cy
simply center of bbox so:
cx = 0.5*(x1+x0)
cy = 0.5*(y1+y0)
compute radius
if you got really a circle then bbox should be square so
fabs((x1-x0)-(y1-y0)) <= zero_threshold
if not you do not have a circle. If yes radius is
r = ~0.5*(x1-x0) = ~0.5*(y1-y0)
so do the average ...
r = 0.25*(x1-x0 + y1-y0)
check for deviation from circle
compute max abs difference ...
d = max ( fabs( (x[i]-cx)^2 + (y[i]-cy)^2 - r^2) )
if d > max_radius_difference_threshold^2
then you do not have a circle.
Also check this out:
There are also another tell tels like
Upvotes: 1
Reputation: 921
general equation of circle is a^2 + b^2=r^2
with center (0,0); where r is the radius of the circle and a and b the coordinates of point put in a and b and get the values of r if the value of r is same for all the values of a and b then the path is a circle .
Note:- for a center at a point (h,k) equation changes to :-
(a-h)^2+(b-k)^2=r^2;
Upvotes: -1