Home > Enterprise >  Drawing triangles with java using trigonometry
Drawing triangles with java using trigonometry

Time:09-17

What I'm trying to do is create a program which take a side length and two angles and produces a triangle drawn using the AWT library.

I'm having trouble working out the logic to use the trigonometric ratios to produce the x and y points of the polygon.

for example, given two angles and a side, I'm attempting to calculate the next x and y points to draw the lines to.

Currently, I've just got some arbitrary points defined to draw a triangle at the moment.

the code block below the graphics is the math for working out all the sides to the triangle

Could anybody be of assistance?

Here is my code. It is badly written. It's mostly just me experimenting and learning more about 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;
    }
}

CodePudding user response:

The three rules that a triangle must satisfy:

  1. The angles always add to 180° A B C = 180 degrees

  2. Law of Sines (The Sine Rule) a/sin(A) = b/sin(B) = c/sin(C)

    • Note: Angle A is opposite to side a, B is opposite to side b, and C is opposite to side c.
  3. Law of Cosines (The Cosine Rule) c^2 = a^2 b^2 − 2ab*cos(C)

So, let's say you have a triangle XYZ.

The angle opposite to side x = X (=YXZ), the angle opposite to side y = Y (=XYZ), and the angle opposite to side z = Z (=XZY).

The input given by the user is the two angles and the side in between them.

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

To code it:

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)   ".");
}

And to get the x and y coordinates of each point of the triangle, you need to have some more information as there can be multiple triangles possible with 3 sides on a 2D plane.

Else, you may assume one of the coordinates to be (0,0) and find slopes of both sides. This way, you will get the x and y coordinates of the remaining point as you have the lengths of these two segments.

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

Assume three vertices to be (x1,y1), (x2,y2), and (x3,y3).

Check out this LINK

In this way, you can get the x and y coordinates of the remaining vertex.

To code it:

//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)   ")");
}

  • Related