Computer problems,Computer help
*AX SOFT>>>Programming & Design

Java Line Segment Intersection?


I am working on a larger project about points and line segments, but I am having difficulty with the code to check if two line segments intersect. I have read a few Java books, and they suggest a sweep line algorithm - however that is very much beyond my capabilities

Is there an easier way to do this ?

The following is an algorithm, not a program. I hope it helps.

------------
Let there be two line segments S12 and S34.
Let S12 have endpoints (x1,y1) and (x2, y2).
Let S34 have endpoints (x3,y3) and (x4, y4).

There are four cases:
鈥?Case 1: both line segments are vertical
鈥?Case 2; one is vertical and one is not
鈥?Case 3: neither is vertical but both have the same slope
鈥?Case 4: neither is vertical and their slopes are different

Some convenience definitions:
Let x12min = min(x1,x2).
Let x12max = max(x1,x2).
Let x34min = min(x3,x4),
Let x34max = max(x3,x4).
Let y12min = min(y1,y2).
Let y12max = max(y1,y2).
Let y34min = min(y3,y4),
Let y34max = max(y3,y4).

Some CONDITIONAL convenience definitions:

Where S12 is not vertical:
Let the function f12 map the x axis into the y axis as follows:
Let m12 = (y2-y1)/(x2-x1).
Let b12 = y1 - m12*x1.
Let f12(x) = m12*x +b12

Where S34 is not vertical:
Let the function f34 map the x axis into the y axis as follows:
Let m34 = (y4-y3)/(x4-x3).
Let b34 = y3 - m34*x3.
Let f34(x) = m34*x +b34

Case 1: x1 = x2 and x3 = x4
if x1 = x3 and y12min 鈮?y34max and y34min 鈮?y12max, then the segments intersect. DONE
otherwise they don't. DONE

Case 2: Assume without loss of generality that S34 is vertical (x3 = x4)
if x12min 鈮?x3 鈮?x12max and y34min 鈮?f12(x3) 鈮?y34max, then the segments intersect. DONE
otherwise they don't. DONE

Case 3: m12 = m34
if b12 = b34 and x12min 鈮?x34max and x34min 鈮?x12max, then the lines intersect. DONE
otherwise they don't. DONE

Case 4:
if x34max < x12min or x12max < x34min, then the segments cannot intersect. (This can actually be used as a shortcut negative indicator for all four cases) DONE

otherwise:
The overlap in x values is as follows:
Let xOmin = max(x12min,x34min).
Let xOmax = min(x12max,x34max).

Solve the following linear system of equations:
y - m12*x = b12
y - m34*x = b34

Let the solution be (xS,yS).
if xOmin 鈮?xS 鈮?xOmax, then the segments intersect. DONE
otherwise they don't. DONE

----------
Good luck!

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • Html help plz?

    (note the position of the slash) or <p>Your text</p><p>Some other text </p>

    ...
  • What site or what do i do to make a clickable drop down menu?

    ...

  • Help with Fireworks CS3?

    you need to install that plug in that you can find at the CD software shop. it is the Adobe plug in.

    ...
  • How can i convert the value of a array and put them in to a string?

    okay.. in array[i] you have a number right? in your case it is 19. an array can hold multi digit numbers at a single position but string is a array of characters and each position in a string can ...

  • Given an integer value input,determine if that value is even or odd?

    void checkNumber(int number) { if(number is divisible by 2) print "The number is even"; else print "The number is odd"; }

    ...
  • Open source dev operating system.?

    Gnome is just one of the GUI for Linux. Mac OS X already has a far superior GUI. Open source more refers to the licencing agreement of the software written then it does the operating system. Bas...

  • Command prompt got messed up?

    try to check ur 'PATH' environment variable. It should have at least this: %SystemRoot%\system32;%SystemRoot%;

    ...
  • Can you make me a design with my picture?

    Sure, I am pretty good with Photoshop. I can email you, and have it done by the weekend for sure, maybe even tonight depending on how much you would like done. If you aren't satisfied with ...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster