I have 2 sections, each section contains of 2 Points and each point has X and Y.
What is the best way to find the overlap between these 2 sections? (only on the X relevant here)
public class section
{
double leftPoint;
double rightPoint;
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here is example code to show you how to do it. I assume the two sections are (a_from, a_to) and (b_from, b_to) and set the resulting section to be (res_from, res_to). Also I only intersect the intervals on the x-axis as this seems to be what you want. The idea is that the results starts from the later of the two beginnings and ends at the earlier of the two ends.
Note that if res_to.x < res_from.x There is no intersection at all.
Also here I assume a_from.x <= a_to.x and b_from.x <= b_to.x which may not always be true. If it is not you have to calculate res_from.x as
Math.max(Math.min(a_from.getX(), a_to.getX()), Math.min(b_from.getX(), b_to.getX()))