методы класса Segment (найти точку пересечения)
Уже много на эту тему написано вот здесь Java найти пересечение двух отрезков, но я не могу разобраться с Point intersection(Segment another) {}
Вот так выглядит весь код. Подскажите, как получить точку пересечения в методе //Point intersection(Segment second) {}
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;
}
}
class Segment {
private Point start;
private Point end;
public Segment(Point start, Point end) {
if (start == null || end == null) {
throw new IllegalArgumentException("Cannot have null points");
} else if (start.equals(end)) {
throw new IllegalArgumentException("The points must differ");
}
this.start = start;
this.end = end;
}
public Point getStart() {
return start;
}
public Point getEnd() {
return end;
}
double length() {
double xDistanceSquare = Math.pow(start.getX() - end.getX(), 2);
double yDistanceSquare = Math.pow(start.getY() - end.getY(), 2);
return Math.sqrt(xDistanceSquare + yDistanceSquare);
}
Point middle() {
return new Point((start.getX() + end.getX()) / 2,
(start.getY() + end.getY()) / 2);
}
Point intersection(Segment another) {
if (isParallel(another)) {
return null;
}
return getIntersection(another);
}
private boolean isParallel(Segment to) {
return start == to.start;
}
public Point getIntersection(Segment second) {
Point line1x = getStart();
Point line1y = getEnd();
Point line2x = second.getStart();
Point line2y = second.getEnd();
double x = (line2x.getX() - line1x.getX()) / (line1y.getY() - line2y.getY());
double y = (line1x.getX() * line2y.getY() - line2x.getX() * line1y.getY()) / (line1y.getX() - line2y.getY());
return new Point(x, y);
}
}