0

I am working on a computational geometry projects which contains classes for Point, Line, Triangle, etc. I store lines as a yint and a slope, however this is giving me issues with vertical lines, as shown in the issue below. I have to maintain the usage of slopes generally, however I could do a conversion to degrees if you feel this is appropriate for the situation.

Below is a method to get an array of all of the altitudes of a triangle, implemented in the triangle class. It does not work. I am pretty sure it is due to infinite slopes, but not entirely. Any ideas?

//sides is an array of LineSegs {AB, BC, CA}
public Line[] getLineAltitudes() {
    Line[] returner = new Line[3];

    for (int i=0; i<3; i++) {
        //decides which point is opposite to lineseg
        int pointgot = 0;
        if (i == 0) pointgot = 2;
        else if (i == 1) pointgot = 0;
        else pointgot = 1;

        //gets the perpendicular to a side          
        Double newSlope = (double)1/sides[i].getSlope();
        System.out.println(newSlope);

        if (newSlope.isInfinite()) newSlope = (double) 0;

        //constructs a line using the (working) constructor Line(point on line, slope)
        returner[i] = new Line(getPoints()[pointgot],newSlope);
    }

    return returner;
}
nanogru
  • 23
  • 2
  • 10
  • I'm not quite sure what "does not work" means in your question; but if I had to model lines in 2D Euclidean space, I'd use the form `ax + by + c = 0`, and have a class that stores `a`, `b` and `c`. Of course, two lines are treated as equal if the ratios of `a` to `b` to `c` are the same. – Dawood ibn Kareem Apr 22 '14 at 01:15
  • In this case, does not work means that it returns a null line when the line is either vertical or horizontal. And no, sadly I cannot modify how the line classes work. – nanogru Apr 22 '14 at 01:24
  • Incidentally, if you're looking to find the slope of a line that's perpendicular to another, you need to divide `-1` by the other line's slope, not `1`. – Dawood ibn Kareem Apr 22 '14 at 02:07

1 Answers1

0

To handle vertical slopes, using angular measurements instead of slope would allow you to do so without this issue. Consider using java.lang.Math.atan2(double y, double x) to get a vertical angle when there is a zero change in the x value.

SimonT
  • 2,219
  • 1
  • 18
  • 32