二维直角坐标系,输入四个点的坐标(x1, y1), (x2, y2), (x3, y3), (x4, y4)。写一算法,判断这四个点是不是能够围成一个矩形 算法几何 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 余弦定理:a平方=b平方+c平方—2*b*c*cosA这个算不算; 你要是能自己写个求cosA的算法而不用库函数,就算你解决问题了。 public static void main(String[] args) { System.out.println(isRectangle(3,1,2,-1,0,0,1,2)); } public static boolean isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { return (isRightangle(x1, y1, x2, y2, x4, y4) && isRightangle(x3, y3, x2, y2, x4, y4)) ||(isRightangle(x1, y1, x2, y2, x3, y3) && isRightangle(x4, y4, x2, y2, x3, y3)) ||(isRightangle(x1, y1, x3, y3, x4, y4) && isRightangle(x2, y2, x3, y3, x4, y4)); } public static boolean isRightangle(double x1, double y1, double x2, double y2, double x3, double y3) { return (x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0.0; } 对于输入System.out.println(isRectangle(7,0,161,47,7,0,161,0)),输出是true。但是应该输出false吧? 把他们放在一个数组里array[0] = {p1, p2, p3, p4} 排序,先y后x,那么array[0].y == array[1].y,...,如果不满足则不能。 考虑有重复点的话那么就这样变下public static void main(String[] args) { System.out.println(isRectangle(1,1,23,-18,11,-23,13,6)); } public static boolean isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { return (isRightangle(x1, y1, x2, y2, x4, y4) && isRightangle(x3, y3, x2, y2, x4, y4)) || (isRightangle(x1, y1, x2, y2, x3, y3) && isRightangle(x4, y4, x2, y2, x3, y3)) || (isRightangle(x1, y1, x3, y3, x4, y4) && isRightangle(x2, y2, x3, y3, x4, y4)); } public static boolean isRightangle(double x1, double y1, double x2, double y2, double x3, double y3) { return !(((x1 == x2) && (y1 == y2)) || ((x1 == x3) && (y1 == y3))) && (x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0.0; } 上面的只判断了对角直角,用3个直角才行,或者换个思路,用平行+直角判断。 public static void main(String[] args) { System.out.println(isRectangle(1, 1, 23, -18, 13, 6, 11, -23)); } public static boolean isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { return (isRightangle(x1, y1, x2, y2, x4, y4) && x1 + x3 == x2 + x4 && y1 + y3 == y2 + y4) || (isRightangle(x1, y1, x2, y2, x3, y3) && x1 + x4 == x2 + x3 && y1 + y4 == y2 + y2) || (isRightangle(x1, y1, x3, y3, x4, y4) && x1 + x2 == x3 + x4 && y1 + y2 == y3 + y4); } public static boolean isRightangle(double x1, double y1, double x2, double y2, double x3, double y3) { return !(((x1 == x2) && (y1 == y2)) || ((x1 == x3) && (y1 == y3))) && (x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0.0; } 打错个数字 public static void main(String[] args) { System.out.println(isRectangle(1, 1, 23, -18, 13, 6, 11, -23)); } public static boolean isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { return (isRightangle(x1, y1, x2, y2, x4, y4) && x1 + x3 == x2 + x4 && y1 + y3 == y2 + y4) || (isRightangle(x1, y1, x2, y2, x3, y3) && x1 + x4 == x2 + x3 && y1 + y4 == y2 + y3) || (isRightangle(x1, y1, x3, y3, x4, y4) && x1 + x2 == x3 + x4 && y1 + y2 == y3 + y4); } public static boolean isRightangle(double x1, double y1, double x2, double y2, double x3, double y3) { return !(((x1 == x2) && (y1 == y2)) || ((x1 == x3) && (y1 == y3))) && (x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0.0; } JavaSE对象引用问题 java中如何停止实时数据传输? InetAddress.getLocalhost().getHostAddress() 多网卡? 请问一个关于一个jdbc的问题? java IO 问题 200分求问题答案 大家请看看,帮忙分析一下 哪里有jai包下载 java中非阻塞I/O操作疑问? main函数所在的类是否一定得为public?? 为何敌人坦克看不到,跟我的坦克同事被监听了?请高手指教 一道关于计算的题,请教下!!
这个算不算;
你要是能自己写个求cosA的算法而不用库函数,就算你解决问题了。
System.out.println(isRectangle(3,1,2,-1,0,0,1,2));
} public static boolean isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
return (isRightangle(x1, y1, x2, y2, x4, y4) && isRightangle(x3, y3, x2, y2, x4, y4))
||(isRightangle(x1, y1, x2, y2, x3, y3) && isRightangle(x4, y4, x2, y2, x3, y3))
||(isRightangle(x1, y1, x3, y3, x4, y4) && isRightangle(x2, y2, x3, y3, x4, y4));
} public static boolean isRightangle(double x1, double y1, double x2, double y2, double x3, double y3) {
return (x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0.0;
}
对于输入System.out.println(isRectangle(7,0,161,47,7,0,161,0)),输出是true。但是应该输出false吧?
public static void main(String[] args) {
System.out.println(isRectangle(1,1,23,-18,11,-23,13,6));
} public static boolean isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
return (isRightangle(x1, y1, x2, y2, x4, y4) && isRightangle(x3, y3, x2, y2, x4, y4))
|| (isRightangle(x1, y1, x2, y2, x3, y3) && isRightangle(x4, y4, x2, y2, x3, y3))
|| (isRightangle(x1, y1, x3, y3, x4, y4) && isRightangle(x2, y2, x3, y3, x4, y4));
} public static boolean isRightangle(double x1, double y1, double x2, double y2, double x3, double y3) {
return !(((x1 == x2) && (y1 == y2)) || ((x1 == x3) && (y1 == y3)))
&& (x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0.0;
}
public static void main(String[] args) {
System.out.println(isRectangle(1, 1, 23, -18, 13, 6, 11, -23));
} public static boolean isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
return (isRightangle(x1, y1, x2, y2, x4, y4) && x1 + x3 == x2 + x4 && y1 + y3 == y2 + y4)
|| (isRightangle(x1, y1, x2, y2, x3, y3) && x1 + x4 == x2 + x3 && y1 + y4 == y2 + y2)
|| (isRightangle(x1, y1, x3, y3, x4, y4) && x1 + x2 == x3 + x4 && y1 + y2 == y3 + y4);
} public static boolean isRightangle(double x1, double y1, double x2, double y2, double x3, double y3) {
return !(((x1 == x2) && (y1 == y2)) || ((x1 == x3) && (y1 == y3)))
&& (x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0.0;
}
public static void main(String[] args) {
System.out.println(isRectangle(1, 1, 23, -18, 13, 6, 11, -23));
} public static boolean isRectangle(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
return (isRightangle(x1, y1, x2, y2, x4, y4) && x1 + x3 == x2 + x4 && y1 + y3 == y2 + y4)
|| (isRightangle(x1, y1, x2, y2, x3, y3) && x1 + x4 == x2 + x3 && y1 + y4 == y2 + y3)
|| (isRightangle(x1, y1, x3, y3, x4, y4) && x1 + x2 == x3 + x4 && y1 + y2 == y3 + y4);
} public static boolean isRightangle(double x1, double y1, double x2, double y2, double x3, double y3) {
return !(((x1 == x2) && (y1 == y2)) || ((x1 == x3) && (y1 == y3)))
&& (x2 - x1) * (x3 - x1) + (y2 - y1) * (y3 - y1) == 0.0;
}