计算直角坐标系中,两点之间距离、两条直线之间的交点坐标、某点是否在某条直线上。...

两点之间距离,该方法遵循公式:

 1 Point p1 = new Point(5, 6);// 定义第一个点的坐标(5,6)
 2         Point p2 = new Point(7, 8);// 定义第二个点的坐标(7,8)
 3         //定位坐标
 4         System.out.println("p1的x坐标为" + p1.getX());
 5         System.out.println("p1的y坐标为" + p1.getY());
 6         System.out.println("p2的x坐标为" + p2.getX());
 7         System.out.println("p2的y坐标为" + p2.getY());
 8         // 计算两点间距离公式
 9         double juli = Math.sqrt(Math.abs((p1.getX() - p2.getX()) * (p1.getX() - p2.getX()) + (p1.getY() - p2.getY()) * (p1.getY() - p2.getY())));
10         System.out.println("两点间的距离是:" + juli);
两点之间距离

 两点之间的交点坐标,遵循公式:

 1 @Getter
 2 @Setter
 3 public class Lseg {
 4 
 5     public Lseg(Light.Point startPoint, Light.Point endPoint) {
 6         this.startPoint = startPoint;
 7         this.endPoint = endPoint;
 8     }
 9 
10     private Light.Point startPoint;
11 
12     private Light.Point endPoint;
13 }
Lseg.java
 1 /**
 2      * 获取两条线之间的交点
 3      * @param lsegA 起点
 4      * @param lsegB 终点
 5      */
 6     public static Light.Point getCrossPoint(Lseg lsegA, Lseg lsegB) {
 7         double x;
 8         double y;
 9         double x1 = lsegA.getStartPoint().getX();
10         double y1 = lsegA.getStartPoint().getY();
11         double x2 = lsegA.getEndPoint().getX();
12         double y2 = lsegA.getEndPoint().getY();
13         double x3 = lsegB.getStartPoint().getX();
14         double y3 = lsegB.getStartPoint().getY();
15         double x4 = lsegB.getEndPoint().getX();
16         double y4 = lsegB.getEndPoint().getY();
17         double k1 = Float.MAX_VALUE;
18         double k2 = Float.MAX_VALUE;
19         boolean flag1 = false;
20         boolean flag2 = false;
21 
22         if ((x1 - x2) == 0)
23             flag1 = true;
24         if ((x3 - x4) == 0)
25             flag2 = true;
26 
27         if (!flag1)
28             k1 = (y1 - y2) / (x1 - x2);
29         if (!flag2)
30             k2 = (y3 - y4) / (x3 - x4);
31 
32         if (k1 == k2)
33             return null;
34 
35         if (flag1) {
36             if (flag2)
37                 return null;
38             x = x1;
39             if (k2 == 0) {
40                 y = y3;
41             } else {
42                 y = k2 * (x - x4) + y4;
43             }
44         } else if (flag2) {
45             x = x3;
46             if (k1 == 0) {
47                 y = y1;
48             } else {
49                 y = k1 * (x - x2) + y2;
50             }
51         } else {
52             if (k1 == 0) {
53                 y = y1;
54                 x = (y - y4) / k2 + x4;
55             } else if (k2 == 0) {
56                 y = y3;
57                 x = (y - y2) / k1 + x2;
58             } else {
59                 x = (k1 * x2 - k2 * x4 + y4 - y2) / (k1 - k2);
60                 y = k1 * (x - x2) + y2;
61             }
62         }
63         if (between(x1, x2, x) && between(y1, y2, y) && between(x3, x4, x) && between(y3, y4, y)) {
64             Light.Point point = new Light.Point();
65             point.setX(x);
66             point.setY(y);
67             if (point.equals(lsegA.getStartPoint()) || point.equals(lsegA.getEndPoint()))
68                 return null;
69             return point;
70         } else {
71             return null;
72         }
73     }
74 
75     public static boolean between(double a, double b, double target) {
76         if (target >= a - 0.01 && target <= b + 0.01 || target <= a + 0.01 && target >= b - 0.01)
77             return true;
78         else
79             return false;
80     }
计算交点
 1  @Test
 2     void test01() {
 3         Light.Point point1 = new Light.Point();
 4         point1.setX(1);
 5         point1.setY(1);
 6         Light.Point point2 = new Light.Point();
 7         point2.setX(5);
 8         point2.setY(5);
 9         Lseg lseg = new Lseg(point1, point2);
10         Light.Point point3 = new Light.Point();
11         point3.setX(1);
12         point3.setY(5);
13         Light.Point point4 = new Light.Point();
14         point4.setX(5);
15         point4.setY(1);
16         Lseg lseg1 = new Lseg(point3, point4);
17         Light.Point point = getCrossPoint(lseg, lseg1);
18         System.out.println("直线A 坐标为 1,1  5,5");
19         System.out.println("直线B 坐标为 1,5  5,1");
20         System.out.println("两线之间的交点为 " + point.getX() + "," + point.getY());
21     }
调用示例

某点是否在某条直线上,遵循公式:

(Q-P1)*(P1-P2)=0 即三点共线
 1 @Getter
 2 @Setter
 3 public class LineVo {
 4 
 5     public LineVo(Light.Point startPoint, Light.Point endPoint) {
 6         this.startPoint = startPoint;
 7         this.endPoint = endPoint;
 8     }
 9 
10     private Light.Point startPoint;
11 
12     private Light.Point endPoint;
13 }
LineVo
 1  /**
 2      * 判断某点是否在一条直线上
 3      *
 4      * @param lineVo 线
 5      * @param point  点
 6      */
 7     public static boolean flagOnLine(LineVo lineVo, Light.Point point) {
 8         if (((point.getX() - lineVo.getStartPoint().getX()) * (lineVo.getStartPoint().getY() - lineVo.getEndPoint().getY()))
 9                 == ((lineVo.getStartPoint().getX() - lineVo.getEndPoint().getX()) * (point.getY() - lineVo.getStartPoint().getY()))
10                 && (point.getX() >= Math.min(lineVo.getStartPoint().getX(), lineVo.getEndPoint().getX())
11                 && point.getX() <= Math.max(lineVo.getStartPoint().getX(), lineVo.getEndPoint().getX()))
12                 && ((point.getY() >= Math.min(lineVo.getStartPoint().getY(), lineVo.getEndPoint().getY()))
13                 && (point.getY() <= Math.max(lineVo.getStartPoint().getY(), lineVo.getEndPoint().getY())))) {
14             return true;
15         } else {
16             return false;
17         }
18     }
通用方法
 1 @Test
 2     void test02() {
 3         LinePoint startPoint = new LinePoint();
 4         startPoint.setXCoordinates(19d);
 5         startPoint.setYCoordinates(25d);
 6         LinePoint endPoint = new LinePoint();
 7         endPoint.setXCoordinates(19d);
 8         endPoint.setYCoordinates(30d);
 9         LineVo lineVo1 = new LineVo(startPoint, endPoint);
10         Light.Point point = new Light.Point();
11         point.setX(19);
12         point.setY(26);
13         Assert.isTrue(LineCalculateUtil.flagOnLine(lineVo1, point), "不在一条线上");
14     }
调用示例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值