java求矩形中心_如何用java写出过一个矩形中心点并与其最长边平行的直线方程...

该博客展示了一段Java代码,用于创建一个矩形类,判断四点是否构成矩形,并计算矩形中心。如果矩形不是正方形,则找出与最长边平行的直线方程。
摘要由CSDN通过智能技术生成

展开全部

是这个意思么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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值