Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through N points that are perpendicular to another line

Tags:

c#

winforms

I have 1 line with 2 known points:

PointF p2_1 = new PointF();
p2_1.X = 100; // x1
p2_1.Y = 150; // y1

PointF p2_2 = new PointF();
p2_2.X = 800; // x2
p2_2.Y = 500; // y2

float dx = p2_2.X - p2_1.X;
float dy = p2_2.Y- p2_1.Y;

float slope = dy / dx; // slope m
float intercept = p2_1.Y - slope * p2_1.X; // intercept c 
// y = mx + c

I'd like to iterate through 10 pixels to the left (or right) to 1 line (at x1, y1).

enter image description here

The red dots are the ones that I'd like process. Example:

for (int i = 10; i > 0; i--)
{
   // start with distant coordinates
   PointF new_point = new Point(); // (grab x,y, coords accordingly)
   // repeat until I'm at (x1, y1)
}

How do I iterate through these coords?

like image 657
Alex Avatar asked Apr 11 '13 19:04

Alex


2 Answers

A perpendicular vector will be of the form: [-dy dx] where [dx dy] is your current vector. Once you have the perpendicular vector, you can normalize it (unit length), then iterate by a set amount:

float perp_dx = -dy / Math.sqrt(dy*dy+dx*dx); //normalized
float perp_dy = dx /Math.sqrt(dy*dy+dx*dx); //normalized

for(int i =0; /*logic here*/){
 float new_x = perp_dx * i + start_x;
 float new_y = perp_dy * i + start_y;
}
like image 134
munch1324 Avatar answered Nov 07 '22 13:11

munch1324


The line perpendicular to a given line has slope equal to the negative inverse of the slope of the given line.

The slope of the given line is (y2-y1) / (x2-x1)

So the red line has slope = - 1 / [(y2-y1) / (x2-x1)]

So each ith point on this line has coordinates (xi, yi) where

  (yi - y1) / (xi - x1)  = - 1 / (y2-y1) / x2-x1)

and is a multiple of one pixel fixed distance away from (x1, y1), i.e., where

   (yi-y1) * (yi-y1) +  (xi-x1) * (xi-x1) =  i * i

what I would do is calculate what this increment vector (dx, dy) is for or between each point on the red line, and then just keep adding that increment in a loop that iterates 10 times.

like image 24
Charles Bretana Avatar answered Nov 07 '22 13:11

Charles Bretana