使用三角学绘制 java 的三角形

Drawing triangles with java using trigonometry

我想做的是创建一个程序,它采用边长和两个角度,并生成使用 AWT 库绘制的三角形。

我在计算使用三角比生成多边形的 x 点和 y 点的逻辑时遇到问题。

例如,给定两个角和一条边,我正在尝试计算下一个 x 和 y 点以绘制线条。

目前,我刚刚定义了一些任意点来绘制三角形。

图形下方的代码块是计算三角形所有边的数学运算

有人能帮忙吗?

这是我的代码。写得不好。这主要是我在试验和学习更多关于 java。

import javax.swing.*;
import java.awt.*;
import java.math.*;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;

public class program{
    public static void main (String[] args) { 
        JFrame frame = new JFrame();
        CustomPaintComponent c = new CustomPaintComponent();
        frame.add(c);
        frame.setSize(1000,1000);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setTitle("Triangle");
        frame.setVisible(true);
    }
}
class CustomPaintComponent extends Component {

    public void paint(Graphics g){
        Graphics2D G2D = (Graphics2D)g;
        int[] xpoints = { 500, 750 , 1000 , 500};
        int[] ypoints = { 500 , 250 , 500 , 500};
        G2D.drawPolygon(xpoints,ypoints,4);
    }

    public Triangle calc(){
        double s1,a1,a2;
        s1 = Integer.parseInt(JOptionPane.showInputDialog("Please enter side 1"));
        a1 = Integer.parseInt(JOptionPane.showInputDialog("Please enter angle 1"));
        a2 = Integer.parseInt(JOptionPane.showInputDialog("Please enter angle 2"));
        double s2 = ((s1/Math.sin(Math.toRadians(a1))) * Math.sin(Math.toRadians(a2)));
        System.out.println(s1/Math.sin(a1));
        System.out.println(Math.sin(a2));
        double a3 = 180 - (a1 + a2);
        double s3 = Math.pow(s2, 2) + Math.pow(s1, 2) - (2 * s2 * s1 * Math.cos(Math.toRadians(a3)));
        s3 = Math.sqrt(s3);
        Math.toDegrees(a1);
        Math.toDegrees(a2);
        Math.toDegrees(a3);
        System.out.println("side 1 is " + s1 + "side 2 is " + s2 + " and the third angle is: " + a3 + " and the third side length is: " + s3);
        Triangle Triangle = new Triangle(s1,s2,s3,a1,a2,a3);
        return Triangle;
    }
}
class Triangle{
    double s1,s2,s3,a1,a2,a3;
    
    Triangle(double s1, double s2, double s3, double a1, double a2, double a3){
        this.s1 = s1;
        this.s2 = s2;
        this.s3 = s3;
        this.a1 = a1;
        this.a2 = a2;
        this.a3 = a3;
    }
}

三角形必须满足的三个规则:

  1. 角度之和总是180° A + B + C = 180 degrees

  2. 正弦定律(正弦定则)a/sin(A) = b/sin(B) = c/sin(C)

    • 注:角A对边a,角B对边b,角C对边c。
  3. 余弦定律(余弦法则)c^2 = a^2 + b^2 − 2ab*cos(C)

所以,假设你有一个三角形 XYZ。

x边的对角=X(=YXZ),y边的对角=Y(=XYZ),z边的对角=Z(=XZY)。

用户输入的是两个角和它们之间的边。

Let the input be:
Angle X = 76 degrees
Angle Y = 34 degrees
Side z = 9 cm

First, find the third angle Z by using the first property.

Angle Z = 180 - X - Y
        = 180 - 76 - 34
        = 70 degrees

Now, using the Law of Sines, find the sides x and y.

x/sin(X) = z/sin(Z)
=> x/sin(76°) = 9/sin(70°)
=> x = (9/sin(70°)) × sin(76°)
=> x = 9.29 cm

y/sin(Y) = z/sin(Z)
=> y/sin(34°) = 9/sin(70°)
=> y = (9/sin(70°)) × sin(34°)
=> y = 5.36 cm

编码:

static void printSidesOfTriangle(double side3, double angle1, double angle2){
        // The sum of all the angles of triangle is 180 degrees
        double angle3 = 180 - angle1 - angle2;

        // Converting the angles from degrees to radians 
        angle1 = Math.toRadians(angle1);
        angle2 = Math.toRadians(angle2);
        angle3 = Math.toRadians(angle3);

        double ratio = side3/(Math.sin(angle3));

        double side1 = ratio*(Math.sin(angle1));
        double side2 = ratio*(Math.sin(angle2));
        
        System.out.println("The sides of the traingle are: " + 
             String.valueOf(side1) + ", " +  
             String.valueOf(side2) + ", and " +  
             String.valueOf(side3) + ".");
}

要获得三角形每个点的 x 和 y 坐标,您需要了解更多信息,因为在 2D 平面上可能有多个具有 3 个边的三角形。

否则,您可以假设其中一个坐标为 (0,0) 并求出两侧的斜率。 这样,您将获得剩余点的 x 和 y 坐标,因为您拥有这两个线段的长度。

Let's assume:
Angle X = 76 degrees
Angle Y = 34 degrees
Angle Z = 70 degrees
Side x = 9.29 cm
Side y = 5.36 cm
Side z = 9.00 cm

假设三个顶点为 (x1,y1)、(x2,y2) 和 (x3,y3)。

看看这个 LINK

这样就可以得到剩余顶点的x和y坐标

编码:

//printVerticesOfTriangle(5.35,9.29,9.0,34,76,70);
//printVerticesOfTriangle(3,4,5,37,53,90);
static void printVerticesOfTriangle(double side1, double side2, double side, 
        double angle1, double angle2, double angle3)
{
        // side1 opposite to angle1
        // side2 opposite to angle2
        // side3 opposite to angle3

        // The vertices are (x1,y1), (x2,y2) and (x3,y3)
        double x1 = 0;
        double y1 = 0;

        double x2 = 0;
        double y2 = 0;

        double x3 = 0;
        double y3 = 0;

        // If an angle is 90 degrees, keep that vertex as the origin.
        if(angle1==90){
            x1 = 0;
            y1 = 0;

            x2 = side2;
            y2 = 0;

            x3 = 0;
            y3 = side3;
        }
        else if(angle2==90){
            x2 = 0;
            y2 = 0;

            x1 = side1;
            y1 = 0;

            x3 = 0;
            y3 = side3;
        }
        else if(angle3==90){
            x3 = 0;
            y3 = 0;

            x1 = side1;
            y1 = 0;

            x2 = 0;
            y2 = side2;
        }
        // If it is not a right-angled triangle
        else{
            // Converting the angles from degrees to radians 
            angle1 = Math.toRadians(angle1);
            angle2 = Math.toRadians(angle2);
            // For the slope, angle with the positive x-axis is considered
            angle3 = Math.toRadians(180-angle3);
            
            x1 = 0;
            y1 = 0;

            x3 = side2;
            y3 = 0;

            // Vertex (x2,y2) has to be calculated
            x2 = x3*Math.tan(angle3)/(Math.tan(angle1)-Math.tan(angle2));
            y2 = x2*Math.tan(angle1);
        }

        System.out.println("The vertices of the triangle are: " + 
            "(" + String.format("%.2f",x1) + "," + String.format("%.2f",y1) + "), " + 
            "(" + String.format("%.2f",x2) + "," + String.format("%.2f",y2) + "), " +
            "(" + String.format("%.2f",x3) + "," + String.format("%.2f",y3) + ")");
}