private static boolean intersectsLine(final Point2D point, Point2D lineP1,
Point2D lineP2) {
- // Sort line points by y coordinate.
+ // Sort line points by y coordinate.
if (lineP1.y > lineP2.y) {
final Point2D tmp = lineP1;
lineP1 = lineP2;
}
// Check if point is within line y range.
- if (point.y < lineP1.y)
- return false;
- if (point.y > lineP2.y)
+ if (point.y < lineP1.y || point.y > lineP2.y)
return false;
// Check if point is on the line.
final double xp = lineP2.x - lineP1.x;
final double yp = lineP2.y - lineP1.y;
- final double yp2 = point.y - lineP1.y;
-
- final double crossX = lineP1.x + ((xp * yp2) / yp);
+ final double crossX = lineP1.x + ((xp * (point.y - lineP1.y)) / yp);
return point.x >= crossX;
-
}
public static boolean pointWithinPolygon(final Point2D point,
*/
public class Rectangle {
+ /**
+ * Rectangle points.
+ */
public Point2D p1, p2;
+ /**
+ * Creates new rectangle with given size.
+ * The rectangle will be centered at the origin.
+ * The rectangle will be square.
+ *
+ * @param size The size of the rectangle.
+ */
public Rectangle(final double size) {
p2 = new Point2D(size / 2, size / 2);
p1 = p2.clone().invert();
}
+ /**
+ * @param p1 The first point of the rectangle.
+ * @param p2 The second point of the rectangle.
+ */
public Rectangle(final Point2D p1, final Point2D p2) {
this.p1 = p1;
this.p2 = p2;
return abs(p1.y - p2.y);
}
+ /**
+ * @return The leftmost x coordinate of the rectangle.
+ */
public double getLowerX() {
return min(p1.x, p2.x);
}
return min(p1.y, p2.y);
}
+ /**
+ * @return rectangle width.
+ */
public double getWidth() {
return abs(p1.x - p2.x);
}