Question
This is a follow up to my original question: I need help on this one. I have a hard time to figure out the condition
This is a follow up to my original question:
I need help on this one. I have a hard time to figure out the condition when 2 lines will be crossed along the way when the points are sorted using mergesort. In particular, how will an inversion happen affect the the # of intersection points with reference to 2 list of integers? I assume we will borrow the concept of mergesort and counting inversion for a list of integers to solve this problem. I will be solving this using Java. Problem: Suppose you are given two sets of n points, one set {p1, p2, . . . , pn} on the line y = 0 and the other set {q1, q2, . . . , qn} on the line y = 1. Create a set of n line segments by connecting each point pi to the corresponding point qi. Your goal is to develop an algorithm to determine how many pairs of these line segments intersect. Your algorithm should take the 2n points as input, and return the number of intersections. Using divide-and-conquer, you should be able to develop an algorithm that runs in O(n log n) time. Hint: What does this problem have in common with the problem of counting inversions in a list?
Chegg provided an answer and explain how we can count the intersections but it only showed a link on the sample java. I read the link but it's using vector algorithm, which is too complicated for me. Is there an easier way to detect the intersection using the points as integer? And, how do I put the merge algorithm to detect the intersection crossing the boundary as we use divide and conquer to divide the set into 2? Can you provide a full working code in Java for the whole thing? Thanks!
public boolean intersects(LineSegment other) {
// Check if this line segment intersects with another line segment.
// Return true if they intersect, false otherwise.
// See https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started