The following code got 2 functions . (credit to @AlexRudenko who helped me)
They both get a segment, a line(not necessary an infinitive one). In addition to the one they already have. The first function checks whether there is an intersection point(only one), and returns a boolean value. The second one should check the same and return the point itself.
// Returns true if the lines intersect, false otherwise
public boolean isIntersecting(Line other) {
if (equals(other)){
return false;
}
double x11 = this.start.getX();
double y11 = this.start.getY();
double x12 = this.end.getX();
double y12 = this.end.getY();
double x21 = other.start.getX();
double y21 = other.start.getY();
double x22 = other.end.getX();
double y22 = other.end.getY();
if (x11 == x12 && x21 == x22) { // both lines are constant x
// no intersection point
return false;
} else if (x11 == x12 || x21 == x22) { // either line is constant x
double x;
double m;
double b;
if (x11 == x12) { // first line is constant x, second is sloped
x = x11;
m = (y22 - y21) / (x22 - x21);
b = (x22 * y21 - x21 * y22) / (x22 - x21);
} else { // second line is constant x, first is sloped
x = x21;
m = (y12 - y11) / (x12 - x11);
b = (x12 * y11 - x11 * y12) / (x12 - x11);
}
double y = m * x + b;
Point.intersection = new Point (x,y);
return true;
} else { // both lines are sloped
double m1 = (y12 - y11) / (x12 - x11);
double b1 = (x12 * y11 - x11 * y12) / (x12 - x11);
double m2 = (y22 - y21) / (x22 - x21);
double b2 = (x22 * y21 - x21 * y22) / (x22 - x21);
if ((long) m1 == (long) m2) {
// no intersection point
return false;
}
// calculating intersection coordinates
double x = (b2 - b1)/(m1 - m2);
double y = m1 * x + b1; // or m2 * x + b2
Point.intersection = new Point (x,y);
return true;
}
}
// Returns the intersection point if the lines intersect,
// and null otherwise.
public Point intersectionWith(Line other) {
if (isIntersecting(other)) {
return Point.intersection;
}
return null;
}
I thought about "saving" the point in a static variable in the first function before returning a "true" value, and then using this static variable to "pull" the point in the second function and return the values of it.
Also, do you think that regarding segments and not infinitive lines, I should add more conditions to the first function? I mean yes, lets say the both have the same slope, but it does not mean they can't have only one intersection point. Because if we are talking about Line1: (1,0) (1,2) and Line2: (1,2) (1,3) They both have the same slope, but they still have an intersection point at (1,2). So what do you think I should add to the code to eliminate all these cases? I thought about something like:
if(!this.start().equals(line.start())
||!this.start().equals(line.end())
||!this.end().equals(line.start())
||!this.end().equals(line.end()))
return false;
I think I should also add a small check that checks whether the intersection point is on both of the segments. I should take the X & Y values of the intersection point and check whether the X value is between the X value of the starting point and the X value of the ending point (on both segments) and do the same check for the Y value. What do you think?
Thanks.
wouldn't it be easier to de it the other way around tho ?
public Point isIntersecting(Line other) {
//your calculation here
return Point
}
public Boolean intersectionWith(Line other) {
Point p=isIntersecting(other)
if(p==null)
return false
return true
}