Reputation: 674
I drawn line passing through points - (x1,y1) , (x2,y2)
Now i want to draw another line perpendicular to this line with same length.
Please guide me for this ..
Upvotes: 1
Views: 2139
Reputation: 11992
Well, it's simple maths :
int dx = x2 - x1;
int dy = y2 - y1;
int ox,oy; // Origin of new line
//...
drawLine( ox, oy, ox+dy, oy-dx) // This line will be perpendicular to original one
All you have to do is to choose the origin. For example, if you want that the lines cuts at their center, let :
ox = x1 + (dx - dy) / 2;
oy = y1 + (dx + dy) / 2;
Upvotes: 3
Reputation: 21351
Think of your line as a vector from (x1,y1) to (x2,y2). Then we get the x and y components of this vector according to
vX = x2-x1
vY = y2-y1
The vector of equal size to this but perpendicular to it in the plane has x and y components
vXP = -(y2-y1)
vYP = x2-x1
you can verify these 2 vectors are perpendicular by taking the scalar product of the 2 vectors which will be zero. Now you have your vector of equal length and perpendicular to your first vector, you simply need to decide the start point of your line. We will call that (a,b). Then using your start point, the end point of your line is given by
(a - (y2-y1), b + (x2-x1))
or if you want it to point in the reverse direction (still perpendicular) it will be
(a + (y2-y1), b - (x2-x1))
Upvotes: 4