'How to find the most left point in a Triangle?
I have this problem: I have this class named Point:
class Point {
private double x;
private double y;
public Point(final double x, final double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
I have a class Triangle:
class Triangle {
private Point a;
private Point b;
private Point c;
public Triangle(Point a, Point b , Point c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
public double area() {
double area = (a.getX() * (b.getY()- c.getY()) +
b.getX() * (c.getY() - a.getY()) + c.getX() * (a.getY() - b.getY())) / 2.0;
return Math.abs(area);
}
@Override
public String pointsToString() {
return "("+a.getX()+","+a.getY()+")"+"("+b.getX()+","+b.getY()+")"+"("+c.getX()+","+c.getY()+")";
}
public String toString() {
return this.getClass().getSimpleName() + "[" + pointsToString() + "]";
}
@Override
public Point leftmostPoint() {
return null;
}
}
The question is how to find the leftmost point in the Triangle ?
I should realize the method leftmostPoint(). The method leftmostPoint() should return leftmost point of the Triangle: the one having the least X coordinate and if there are many leftmost points, return any of them.
Solution 1:[1]
Think about how triangles work. The leftmost point (or one of them, if there are more than one) must be one of the 3 corners. So, it's a, b, or c.
Just compare a to b, and then the winner of that to c, and return the winner of that.
Solution 2:[2]
the least X coordinate of a Triangle will work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rzwitserloot |
| Solution 2 | Eugene B |
