I want to find whether a point lies inside a square or not. I have been given end coordinates of a diagonal of a square and I have found out the other diagonal's end coordinates.
One method I could think of it was to solve it by using property of circumcircle. I have included all the points inside the circumcircle of the square but I cannot think of a method to exclude the extra points.
The edges are not parallel to any coordinate axis.
Is there any method to do it in java?
Heres my part of code:
if(distance(x1,y1,x2,y2,j,i)<=dd)
A[i][j]=1;
else
A[i][j]=0;
x1,y1,x2,y2 are the coordinates of the diagonal of the square and dd is the length of diagonal and i,j are the coordinates greater than 0 and less than 100.
I am using array to store the points satisfying the condition.
解决方案
There's no built-in method to do it in Java, but it's simple math.
The equation of a line is:
a * x + b * y = c1 (1)
The equation of a line parallel to this is:
a * x + b * y = c2 (2)
The equation of two lines perpendicular to these are:
-b * x + a * y = c3 (3)
-b * x + a * y = c4 (4)
These are the equations of the four edges of the square.
Determine the coefficients of the equations above (a, b, c1..c4) for your square.
The point is inside the square iff both of the following conditions are true:
min(c1, c2) <= a * x + b*y <= max(c1, c2)
min(c3, c4) <= -b * x + a * y <= max(c3, c4)