展开全部
是这个意思么public class Main{
public static void main(String[] args) {
62616964757a686964616fe4b893e5b19e31333337396264Pointa = new Point(0, 0),
b = new Point(2, 2),
c = new Point(4, 0),
d = new Point(2, -2);
Rect rect = new Rect(a, b, c, d);
if(rect.isRect()) {
if(rect.getMidOfShort() != null) {
System.out.println(new Line(rect.getCenter(), rect.getMidOfShort()));
} else {
System.out.println("这是个正方形,不存在长边。");
}
} else {
System.out.print("这四个点不构成长方形。");
}
}
}
class Rect {
Point a, b, c, d;
public Rect(Point a, Point b, Point c, Point d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
public boolean isRect() {
if((b.x - a.x) * (c.x - b.x) + (b.y - a.y) * (c.y - b.y) != 0) return false;
if((d.x - c.x) * (c.x - b.x) + (d.y - c.y) * (c.y - b.y) != 0) return false;
if((b.x - a.x) * (a.x - d.x) + (b.y - a.y) * (a.y - d.y) != 0) return false;
if((d.x - c.x) * (a.x - d.x) + (d.y - c.y) * (a.y - d.y) != 0) return false;
return true;
}
public Point getCenter() {
return new Point((a.x + b.x + c.x + d.x) / 4, (a.y + b.y + c.y + d.y) / 4);
}
public Point getMidOfShort() {
if((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y) > (c.x - b.x) * (c.x - b.x) + (c.y - b.y) * (c.y - b.y))
return new Point((b.x + c.x) / 2, (b.y + c.y)/ 2);
else if((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y)
return new Point((b.x + a.x) / 2, (b.y + a.y)/ 2);
else
return null;
}
}
class Line {
Point a, b;
double m, n, k;
public Line(Point a, Point b) {
this.a = a;
this.b = b;
m = b.y - a.y;
n = -(b.x - a.x);
k = -a.x * (b.y - a.y) + a.y * (b.x - a.x);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if(n != 0) {
sb.append("y=");
double t = -m/n;
if(t!=0)
sb.append(t+"x");
double s = -k/n;
if(s>0)
sb.append("+" + s);
else if(s<0)
sb.append(s);
} else {
sb.append("x=" + -k/m);
}
return sb.toString();
}
}
class Point {
double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}