Search code examples
javamathgeometrytrigonometrypoints

finding the other two points of a Perfect Right Triangle


I am writing code that is meant to use one given point of a perfect right triangle to find the remaining two. We assume for this exercise that it is a triangle like so: righttriangle

The first bit of code uses the Point2D class to establish the bottom left point like so:

public Triangle(Point2D.Double bottomLeftPoint, double height, double base){
        this.base = base;
        this.height = height;
        this.bottomLeftPoint = bottomLeftPoint; 
    }

public Point2D.Double getBottomLeftTrianglePoint() {
        return this.bottomLeftPoint;
    }

I know that mathematically, the top point of the triangle would have the same x value, but would have the y value added by the height. Also the bottom right point would have the same y value but the x value added by the base.

My question is for method purposes, how would I structure that?

Would it be something like:

public Point2D.Double getTopTrianglePoint() {
        return this.bottomLeftPoint(x, y + this.height);
    }

public Point2D.Double getBottomRightTrianglePoint() {
        return this.bottomLeftPoint(x + this.base, y);
    }

For further info, I have a separate class that is meant to test the methods with with a test triangle:

public class TriangleTest {
    private Triangle triangle01;
    
    public TriangleTest() {
        this.triangle01 = new Triangle(new Point2D.Double(1, 2), 5, 3);
    }

}

Any help is appreciated. Thanks!


Solution

  • return this.bottomLeftPoint(x, y + this.height);

    Break this down, then you'll notice this doesn't make sense. this.bottomLeftPoint is a variable of type Point2D.Double. You then.. try to treat this as a method somehow. It's not. This doesn't work.

    You want to create an entirely new Point2D.Double. new Point2.Double(x, y) as per usual; Thus:

    return new Point2D.Double(x, y + this.height);
    

    Except, of course, if you try this, the compiler will tell you this doesn't work either; the compiler has no idea what x means. So, what do you intend to use there? Clearly it's the x coordinate of the Point2D.Double object referenced by your this.bottomLeftPoint field. Which has a .getX() method. So:

    return new Point2D.Double(bottomLeftPoint.getX(), bottomLeftPoint.getY() + height);