Project a 2D Point on a Line


/ Published in: C#
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.