Given four coordinate points, how to find the four interior angles of a quadrilateral?

1. Theory

The simplest way is to use vectors to solve

 As shown in the figure:

cosθ=a*b/(|a|*|b|)

Given the coordinates of the three points, it is easy to obtain the product a*b of the two vectors and the modulus of each

2. Four angle solution process

  1. First, we define four coordinate points pointA, pointB, pointC, pointD, which form a quadrilateral.

  2. Next, we call calculateAnglemethods to calculate each interior angle of the quadrilateral. calculateAngleThe method accepts three parameters, which are the coordinates of three points, used to calculate the angle.

  3. In calculateAnglethe method, we first calculate the x and y components of vector 1, which is the vector between point p1 and point p2. Then calculate the x and y components of vector 2, the vector between point p3 and point p2.

  4. Next, we calculate the dot product of vector 1 and vector 2, that is dotProduct = vector1x * vector2x + vector1y * vector2y.

  5. Calculate the modulus of vector 1 and vector 2, that is, their respective lengths, using Math.sqrt()the method to calculate the square root.

  6. Next, we calculate the cosine of the angle, ie cosTheta = dotProduct / (magnitude1 * magnitude2).

  7. Finally, we Math.acos()calculate the angle in radians using the method and convert it to degrees using Math.toDegrees()the method.

  8. In mainthe method, we calculate the four interior angles of the quadrilateral in sequence and print them out.

3. Code implementation

import java.awt.geom.Point2D;

public class QuadrilateralAngles {
    public static void main(String[] args) {
        // 四个坐标点
        Point2D.Double pointA = new Point2D.Double(0, 0);
        Point2D.Double pointB = new Point2D.Double(1, 1);
        Point2D.Double pointC = new Point2D.Double(0, 1);
        Point2D.Double pointD = new Point2D.Double(-1, 0);

        // 计算四边形的四个内角
        double angleA = calculateAngle(pointD, pointA, pointB);
        double angleB = calculateAngle(pointA, pointB, pointC);
        double angleC = calculateAngle(pointB, pointC, pointD);
        double angleD = calculateAngle(pointC, pointD, pointA);

        // 输出结果
        System.out.println("Angle A: " + angleA);
        System.out.println("Angle B: " + angleB);
        System.out.println("Angle C: " + angleC);
        System.out.println("Angle D: " + angleD);
    }

    // 计算夹角的方法
    public static double calculateAngle(Point2D.Double p1, Point2D.Double p2, Point2D.Double p3) {
        // 计算向量1的x和y分量
        double vector1x = p1.getX() - p2.getX();
        double vector1y = p1.getY() - p2.getY();

        // 计算向量2的x和y分量
        double vector2x = p3.getX() - p2.getX();
        double vector2y = p3.getY() - p2.getY();

        // 计算向量1和向量2的点积
        double dotProduct = vector1x * vector2x + vector1y * vector2y;

        // 计算向量1和向量2的模
        double magnitude1 = Math.sqrt(vector1x * vector1x + vector1y * vector1y);
        double magnitude2 = Math.sqrt(vector2x * vector2x + vector2y * vector2y);

        // 计算两向量夹角的余弦值
        double cosTheta = dotProduct / (magnitude1 * magnitude2);

        // 计算夹角的弧度值
        double theta = Math.acos(cosTheta);

        // 将弧度转换为度数并返回
        return Math.toDegrees(theta);
    }
}

Guess you like

Origin blog.csdn.net/qq_55888300/article/details/132330845