所以我尝试写一个简单的方法,需要在设置中的四个坐标,并决定他们是否形成一个正方形或not.My的做法是先从一个点,计算其他三个点和基础点之间的距离。由此我们可以得到双方具有相同的值,其中一个是diagonal.Then我用毕达哥拉斯定理发现,如果双方平方等于diagonal.If这是isSquare方法返回TRUE,否则false.The事我想找出是有一些情况下,我可能会错过了或者如果事情是错的approach.Thanks的所有帮助。
公共类CoordinatesSquare {
公共静态布尔isSquare(名单<点> listPoints){
如果(listPoints = NULL和放大器;!&安培; listPoints.size()== 4){
INT距离1 =距离(listPoints.get(0),listPoints.get(1));
INT distance2的距离=(listPoints.get(0),listPoints.get(2));
INT距离3 =距离(listPoints.get(0),listPoints.get(3));
如果(距离1 ==距离2){
//检查是否边等于对角线
如果(距离3 ==距离1 +距离2){
返回true;
}
}否则,如果(距离1 ==距离3){
//检查是否边等于对角线
如果(距离2 ==距离1 +距离3){
返回true;
}
}
}
返回false;
}
私有静态诠释距离(点对点,点点2){
//(X2-X1)^ 2 +(Y2-Y1)^ 2
返回(int)的(Math.pow(point2.x - point.x,2)+(Math.pow(point2.y
- point.y,2)));
}
公共静态无效的主要(字符串的args []){
名单<点> pointz =新的ArrayList<点>();
pointz.add(新点(2,2));
pointz.add(新点(2,4));
pointz.add(新点(4,2));
pointz.add(新点(4,4));
的System.out.println(CoordinatesSquare.isSquare(pointz));
}
}
//点类
公共类点{
整数x;
码整数Y;
布尔isVisited;
公共点(范围整数x,整数Y){
this.x = X;
this.y = Y;
}
@覆盖
公共布尔等于(obj对象){
如果(物镜=空&安培;!&安培; obj.getClass()等于(this.getClass())。){
返程((点)OBJ).x.equals(this.x)及及((点)OBJ).y.equals(this.y);
}
返回false;
}
}
解决方案
您知道吗,你可以做同样的检查更容易。你只需要检查两件事情: 四点做平行四边形和它的角度一个是正确的。
首先是真实的,当 P3 = P1 +(P2-P1)+(P4-P1)
和第二时(P2-P1)*(P4-P1)= 0
其中, A * B
是一个点积(AX * Bx的+好哦*通过)
在这里,唯一的缺点是计算错误。你不能指望花车是完全相等,所以不是 A = B
你应该考虑使用类似 ABS(AB)< Ë
,其中电子
是你的情况足够小。
So I am trying to write a simple method which takes in set of four coordinates and decide whether they form a square or not.My approach is start with a point and calculate the distance between the other three points and the base point.From this we can get the two sides which have same value and the one which is a diagonal.Then I use Pythagoras theorem to find if the sides square is equal to the diagonal.If it is the isSquare method return true else false.The thing I want to find out is there some cases I might be missing out on or if something is wrong with the approach.Thanks for the all the help.
public class CoordinatesSquare {
public static boolean isSquare(List<Point> listPoints) {
if (listPoints != null && listPoints.size() == 4) {
int distance1 = distance(listPoints.get(0), listPoints.get(1));
int distance2 = distance(listPoints.get(0), listPoints.get(2));
int distance3 = distance(listPoints.get(0), listPoints.get(3));
if (distance1 == distance2) {
// checking if the sides are equal to the diagonal
if (distance3 == distance1 + distance2) {
return true;
}
} else if (distance1 == distance3) {
// checking if the sides are equal to the diagonal
if (distance2 == distance1 + distance3) {
return true;
}
}
}
return false;
}
private static int distance(Point point, Point point2) {
//(x2-x1)^2+(y2-y1)^2
return (int) (Math.pow(point2.x - point.x, 2) + (Math.pow(point2.y
- point.y, 2)));
}
public static void main(String args[]) {
List<Point> pointz = new ArrayList<Point>();
pointz.add(new Point(2, 2));
pointz.add(new Point(2, 4));
pointz.add(new Point(4, 2));
pointz.add(new Point(4, 4));
System.out.println(CoordinatesSquare.isSquare(pointz));
}
}
//Point Class
public class Point {
Integer x;
Integer y;
boolean isVisited;
public Point(Integer x, Integer y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if(obj!=null && obj.getClass().equals(this.getClass())){
return ((Point) obj).x.equals(this.x)&&((Point) obj).y.equals(this.y);
}
return false;
}
}
解决方案
You know, you can do the same check much easier. You just have to check two things: "four points make a parallelogram" and "one of its angles is right".
First is true when P3 = P1 + (P2-P1) + (P4-P1)
And the second when (P2-P1)*(P4-P1) = 0
Where A*B
is a dot product (A.x * B.x + A.y * B.y)
The only catch here is computational error. You can't expect floats to be exactly equal, so instead of A=B
you should consider using something like abs(A-B) < E
where E
is small enough for your case.
上一篇:解决异或方程组方程组