# Finding the intersection of two 2D lines...

This topic is 5391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've got a problem with trying to find the intersection of two 2D lines, but despite consulting a book containing the answer - it seems to be incorrect. Here's the function I made using the book's equations... /////////////////////////////////////////////////////////////////////////////////////////////////// void LINE_intersection(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) //, int *rx, int *ry) { // Line 1 is represented with <x0,y0> to <x1,y1> // Line 2 is represented with <x2,y2> to <x3,y3> // Line 1's slope is represented by m0 // Line 2's slope is represented by m1 float m0 = (y1 - y0) / (x1 - x0); float m1 = (y3 - y2) / (x3 - x2); int x = 0; int y = 0; if(m0 == m1) { return; } // Compute x-axis intersection... x = ( (( -m0 / (m1 - m0)) * x2) + (m0 * (y2 - y0)) + x0 ); // Compute y-axis intersection... y = ( ((m0 * y0) - (m1 * y2) + (x2 - x0)) / (m0 - m1) ); DRAW_pixel( x, y, 31, 63, 31, pitch, video_buffer); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void LINE_intersection(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) //, int *rx, int *ry) { // Line 1 is represented with <x0,y0> to <x1,y1> // Line 2 is represented with <x2,y2> to <x3,y3> // Line 1's slope is represented by m0 // Line 2's slope is represented by m1 float m0 = (y1 - y0) / (x1 - x0); float m1 = (y3 - y2) / (x3 - x2); int x = 0; int y = 0; if(m0 == m1) { return; } // Compute x-axis intersection... x = ( (( -m0 / (m1 - m0)) * x2) + (m0 * (y2 - y0)) + x0 ); // Compute y-axis intersection... y = ( ((m0 * y0) - (m1 * y2) + (x2 - x0)) / (m0 - m1) ); DRAW_pixel( x, y, 31, 63, 31, pitch, video_buffer); } /////////////////////////////////////////////////////////////////////////////////////////////////// ...the pixel, when plotted is well-off mark. Any thoughts would be most welcome.

##### Share on other sites
Sorry, I accidently posted the function twice...

##### Share on other sites
Ah...now thats interesting.

Cheers!

• 21
• 15
• 9
• 17
• 13