-2

Below I have included the code I was working on.

I need to create a class that takes 3 points, and can return the side length, and angles of those points.

I am having trouble at my computeAngles method. I am trying to call three methods there to getSide1_Length and side2 and 3 lengths. But I am having a little trouble.

It is not finished. I still need to get angle1, and angle3, but I want to figure out angle 2 before I continue.

import java.awt.Point;
import java.lang.Math;

public class Triangle
{
    // instance variables - replace the example below with your own
    private Point p1, p2, p3;
    private double angle1,angle2,angle3;

    public Triangle(Point p1, Point p2, Point p3)
    {
        // initialise instance variables
        p1 = p1;
        p2 = p2;
        p3 = p3;
    }

    public Point setPoint1(Point p1)
    {
        this.p1 = new Point();
        return p1;
    }

    public Point setPoint2(Point p2)
    {
        this.p2 = new Point();
        return p2;
    }

    public Point setPoint3(Point p3)
    {
        this.p3 = new Point();
        return p3;
    }

    public Point getPoint1()
    {
        return p1;
    }

    public Point getPoint2()
    {
        return p2;
    }

    public Point getPoint3()
    {
        return p3;
    }

    public double getSide1_Length(double side1)
    {
        side1 = Math.sqrt(((Math.pow((p2.x - p1.x), 2)) + Math.pow((p2.y - p1.y), 2)));

        return side1;
    }

    public double getSide2_Length(double side2)
    {
        side2 = Math.sqrt(((Math.pow((p3.x - p2.x), 2)) + Math.pow((p3.y - p2.y), 2)));

        return side2;
    }

    public double getSide3_Length(double side3)
    {
        side3 = Math.sqrt(((Math.pow((p3.x - p1.x),2)) + Math.pow((p3.y - p1.y),2)));

        return side3;
    }

    public double getAngle1()
    {
        return angle1;
    }

    public double getAngle2()
    {
        return angle2;
    }

    public double getAngle3()
    {
        return angle3;
    }

    private double computeAngles(double angle1, double angle2, double angle3)
    {
        double side1 = getSide1_Length(side1);
        double side2 = getSide2_Length(side2);
        double side3 = getSide3_Length(side3);

        angle2 = Math.asin(((Math.pow(side3,2)) - (Math.pow(side2,2)) - (Math.pow(side1,2)) / (-2 * (side2 * side1))));

        return angle2;
    }
}

2 Answers2

0

First of all you should rework your constructor.

p1 = p1;

I think what you want to do is setting your fields to the parameters of your constructor. What you do there is putting the parameter into the parameter. Now there are 2 common solutions to that problem. Eighter you rename your parameter or place a "this." in front of the left p1.

this.p1 = p1; // this.p1 is the field | p1 is the parameter

Your setPoint() methods need some work too. What you want to do is set your field to the value of the parameter. You're creating new Objects each time.

this.p1 = p1; // this.p1 is the field | p1 is the parameter

Your getSide() methods shouldn't take any parameters since they are just returning the side's lenght based on the points you store in your fields. So change them to something like:

public double getSide1_Length()
{
    double side1 = Math.sqrt(((Math.pow((p2.x - p1.x), 2)) + Math.pow((p2.y - p1.y), 2)));

    return side1;
}

For your computeAngle() method take a look here: https://www.mathsisfun.com/algebra/trig-solving-sss-triangles.html

Based on that link you should do the following:

private double computeAngles()
{
    double side1 = getSide1_Length(side1);
    double side2 = getSide2_Length(side2);
    double side3 = getSide3_Length(side3);

    int angle2 = Math.acos(((Math.pow(side3,2)) + (Math.pow(side2,2)) - (Math.pow(side1,2)) / (2 * (side3 * side2))));

    return angle2;
}
Tom Stein
  • 345
  • 2
  • 15
0

I'd change things a little bit.

getSide1, getSide2 and getSide3: all of them do the same thing, the only difference is the points used in the calculation. So you should change them to have one single method that receives 2 points and do the math:

public double getSideLength(Point a, Point b)
{
    return Math.sqrt(((Math.pow((a.x - b.x),2)) + Math.pow((a.y - b.y),2)));
}

Then, to compute the angles, note that when you do:

private double computeAngles(double angle1, double angle2, double angle3)

Those angle variables are not the same as the ones declared at the beginning (the ones with the private modifier) - check here for details.

To correctly compute the angles, you call getSideLength 3 times to get the lengths of the 3 sides, and then do the respective calculations with the sides:

private void computeAngles() {
    double side1 = getSideLength(p1, p2);
    double side2 = getSideLength(p2, p3);
    double side3 = getSideLength(p3, p1);

    angle2 = Math.asin(((Math.pow(side3,2)) - (Math.pow(side2,2)) - (Math.pow(side1,2)) / (-2 * (side2 * side1))));
    angle1 = // do the same math using the respective sides
    angle3 = // do the same math using the respective sides
}

As the math to get the angle is the same (only the sides used change), you could also create a method for that:

private double getAngle(double sideA, double sideB, double sideC) {
    return Math.asin(((Math.pow(sideC, 2)) - (Math.pow(sideB, 2)) - (Math.pow(sideA, 2)) / (-2 * (sideB * sideA))));
}

Then you compute the angles like that:

private void computeAngles() {
    double side1 = getSideLength(p1, p2);
    double side2 = getSideLength(p2, p3);
    double side3 = getSideLength(p3, p1);

    angle1 = getAngle(side1, side2, side3);
    angle2 = // call getAngle changing the sides order accordingly
    angle3 = // call getAngle changing the sides order accordingly
}