Posted By

ch21st on 05/21/11


Tagged


Versions (?)

Project a 2D Point on a Line


 / Published in: C#
 

URL: http://www.vcskicks.com/code-snippet/point-projection.php

Perpendicularly project a 2D point onto a 2D line.

  1. private Point Project(Point line1, Point line2, Point toProject)
  2. {
  3. double m = (double)(line2.Y - line1.Y) / (line2.X - line1.X);
  4. double b = (double)line1.Y - (m * line1.X);
  5.  
  6. double x = (m * toProject.Y + toProject.X - m * b) / (m * m + 1);
  7. double y = (m * m * toProject.Y + m * toProject.X + b) / (m * m + 1);
  8.  
  9. return new Point((int)x, (int)y);
  10. }

Report this snippet  

You need to login to post a comment.