Jump to content
  • Advertisement
Sign in to follow this  
Anri

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!