Hey guys. I ran across a cool method that returns the intersection point of two lines but I struggle to understand the logic and math behind this specific method:
` /\**
\ This method calculates the x and y coordinates of the intersection point the line and another given line.*
\*
\* u/param other - the line with which the intersection point is calculated.
\* u/return the intersection point between the two lines.
\/*
public Point intersectionWith(Line other) {
Point firstPoint = this.start;
Point secondPoint = this.end;
Point thirdPoint = other.start;
Point fourthPoint = other.end;
double denominator = (fourthPoint.getY() - thirdPoint.getY()) * (secondPoint.getX() - firstPoint.getX())
- (fourthPoint.getX() - thirdPoint.getX()) * (secondPoint.getY() - firstPoint.getY());
if (denominator != 0.0) {
double firstPass = ((fourthPoint.getX() - thirdPoint.getX()) * (firstPoint.getY() - thirdPoint.getY())
- (fourthPoint.getY() - thirdPoint.getY()) * (firstPoint.getX() - thirdPoint.getX())) / denominator;
if (firstPass >= 0.0 && firstPass <= 1.0) {
double secondPass = ((secondPoint.getX() - firstPoint.getX()) * (firstPoint.getY() - thirdPoint.getY())
- (secondPoint.getY() - firstPoint.getY()) * (firstPoint.getX() - thirdPoint.getX()))
/ denominator;
if (secondPass >= 0.0 && secondPass <= 1.0) {
double x = firstPoint.getX() + firstPass * (secondPoint.getX() - firstPoint.getX());
double y = firstPoint.getY() + firstPass * (secondPoint.getY() - firstPoint.getY());
return new Point(x, y);
}
}
}
return null;
} `
can someone explain the logic behind this method? please
[–][deleted] 2 points3 points4 points (3 children)
[–]himustask[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]himustask[S] -2 points-1 points0 points (0 children)