cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package cn.ksource.test;
 
import java.awt.geom.Point2D;
 
public class GeometryUtil {
    // 两点之间的距离
    public static double distanceOfPoints(Point2D p1, Point2D p2) {
        double disX = p2.getX() - p1.getX();
        double disY = p2.getY() - p1.getY();
        double dis = Math.sqrt(disX * disX + disY * disY);
 
        return dis;
    }
 
    // 两点的中点
    public static Point2D middlePoint(Point2D p1, Point2D p2) {
        double x = (p1.getX() + p2.getX()) / 2;
        double y = (p1.getY() + p2.getY()) / 2;
 
        return new Point2D.Double(x, y);
    }
 
    // 在两点所在直线上,以从startPoint到endPoint为方向,离startPoint的距离disToStartPoint的点
    public static Point2D extentPoint(Point2D startPoint, Point2D endPoint, double disToStartPoint) {
        double disX = endPoint.getX() - startPoint.getX();
        double disY = endPoint.getY() - startPoint.getY();
        double dis = Math.sqrt(disX * disX + disY * disY);
        double sin = (endPoint.getY() - startPoint.getY()) / dis;
        double cos = (endPoint.getX() - startPoint.getX()) / dis;
        double deltaX = disToStartPoint * cos;
        double deltaY = disToStartPoint * sin;
 
        return new Point2D.Double(startPoint.getX() + deltaX, startPoint.getY() + deltaY);
    }
 
    // 绕原点的旋转矩阵,绕任意点旋转,可以先移动到原点,旋转,然后再移回去
    // cosθ -sinθ 0
    // sinθ +conθ 0
    // 0000 +0000 1
    // x = r*cosα, y = r*sinα
    // x' = r*cos(α+θ) = r*cosα*cosθ - r*sinα*sinθ = x*cosθ - y*sinθ
    // y' = r*sin(α+θ) = r*sinα*cosθ + r*cosα*sinθ = x*sinθ + y*cosθ
    // (x, y)绕圆心旋转degree度
    public static Point2D rotate(double x, double y, double degree) {
        return rotate(x, y, 0, 0, degree);
    }
 
    // (x, y)绕(ox, oy)旋转degree度
    public static Point2D rotate(double x, double y, double ox, double oy, double degree) {
        x -= ox;
        y -= oy;
 
        double cos = Math.cos(Math.toRadians(degree));
        double sin = Math.sin(Math.toRadians(degree));
 
        double temp = x * cos - y * sin;
        y = x * sin + y * cos;
        x = temp;
 
        return new Point2D.Double(x + ox, y + oy);
    }
 
    public static void main(String[] args) {
        Point2D p = rotate(50, 10, 10);
        System.out.println(p);
        p = rotate(100, 60, 50, 50, 10);
        System.out.println(p);
    }
}