# draw line which is perpindicular to another line with a wanted length

can anyone help me? I have a line $Ax+By+C=0$ (let's call it Line $1$); I want to draw a perpicidular line (Line $2$) from the center of line $1$ with a length of $10$. what I did is get the slope and intercept of line $1$ as well as the its middle point (point $m$). and I used the length equation $L=\sqrt{(\text{line}\space 2\cdot x-m\cdot x)^2+(\text{line}\space 2\cdot y-m\cdot y)^2}$, but I dont get the right result. Thanks in advance

-

## migrated from stackoverflow.comMar 9 '11 at 11:24

This question came from our site for professional and enthusiast programmers.

is this homework? – xtofl Mar 9 '11 at 8:57
No it's not a homework, i just want the user to draw a perpindicular line with a length that he can change anytime :) – Sara Saeed Mar 9 '11 at 9:48

Thanks guys for your support. here's what i did ( and yaay it works) 1-As i have the line equation (or points) i got dy and dx. 2- i got the normal vector to the line(-dy,dx) and (dy,-dx) let's call it NormalVec1 and NormalVec2. 3- i got the magnitude of NormalVec 4- i got the UNIT normalvector by dividing NormalVec by its Magnitude. 5- assume that i want a line with length L. so it gonna be L/2 on both sides. 6- assume that i want to draw this perpindicular line crosing the original line at its center point M. 7-so the perpindicular line's points are: endpoint=(M.X+unitNormalVec1*L/2 ,M.X+unitNormalVec1*L/2) ; for normalVec1 is (-dy,dx) startpoint=(M.X+unitNormalVec2*L/2 ,M.X+unitNormalVec2*L/2) ; for normalVec1 is (dy,-dx) then you can draw a line segment from these points. Thanks,

-

Defining the line Ax + By + C = 0 as a function f(x), you can use linear interpolation between the two endpoints where f(a) = p(0) [beginning] and f(b) = p(t) [end]. The value t = 0.5 will then be at the center of the line and the perpendicular slope is the reciprocal of f(x).

Useful resource for performing this task: http://en.wikipedia.org/wiki/Linear_interpolation

-

i have a line Ax+By+C=0(let's call it Line1); i want to draw a perpicidular line(Line2) from the center of line1 with a length of 10

I am curious: how do you define the center of an infinite line? Is your line in fact a segment defined by two points? If so, do you want to draw a perpendicular line from the middle point of those segment ends and perpendicular to that?

In that case, you can use the vector that is perpendicular to the original vector. If those 2 point are named A and B. The vector being AB has components (x, y). A perpendicular vector to AB has the components (y, x). Then you simply need to draw a line following that vector from the point in the middle of [A, B] (simply add coordinates of the points and divide by 2).

If you have already have the middle point and the equation, then you can have a look at this tutorial to help you solve the equation. (find the slope of the line then invert and negate to get the slope of the perpendicular)

-
Thanks Marvin ,yes it's a line segment and i already got its middle point.. aslo i got the slope and intercept of the perpindicular line so i guess i have to get the perpindicular line unit vector and multily it with the wanted length to get the endpoint – Sara Saeed Mar 9 '11 at 9:13

Your code calculates the length of half the existing line. There are many ways to solve your problem. Get start and end coords, sx, sy, ex, ey. Calculate the slope angle with angle=atan(dy/dx), where dx=ex-sx, dy=ey-sy. Add or subtract pi/2 to angle. Calculate midpoint with mx=(ex-sx)/2 and my=(ey-sy)/2. This is the start point of the perpendicular line. Get the end point with mex=mx+10*cos(angle), mey=my+10*sin(angle). Easy as pi/2. :)

-

The line l = (A,B,C) where lx = 0 has the normal $(A, B)$. There are two perpendicular vectors to this normal namely $(A, -B)$ and $(-A, B)$.

Two lines are perpendicular if their normals cross prouduct equals zero. We can easily see that $(A, B) \times (A,-B) = A*(-B) - B*A$

The C value of the perpendicular line to be created does not matter and can be set to zero or C for example.

So we have line $Ax + Bx + C = 0$ have a perpendicular line $Ax - By + C = 0$

-
i knew the perpindicular line slope and intercept but the problem is that i want it by a spicific length, so i guess i have to get the perpindicular line unit normal vector and multiply the length to get its end point :) – Sara Saeed Mar 9 '11 at 9:12
alright, that sounds like a line segment, ie a line between two points. Is this correct? – vidstige Mar 9 '11 at 9:21

here is a simplified explanation using algorithm logic. In a 2D space (like a screen), if you want to draw a segment of given length perpendicular to a segment you already know at a given point from the first segment :

• $\text{(Segment)}_1$ you know $[x_1,y_1][x_2,y_2]$
• Point where you want your new segment to cross the $\text{(Segment)}_1$ $[x,y]$
• Length of the new segment L

.

if (x2==x1) {
// Segment1 is vertical, draw an horizontal segment [x-L/2,y][x+L/2,y]
} else if (y2==y1) {
// Segment1 is horizontal, draw a vertical segment [x,y-L/2][x,y+L/2]
} else {
dy = y2-y1;
dx = x2-x1;
magnitude = sqrt(dx*dx + dy*dy);
xdecal = L/2 * -dy/magnitude;
ydecal = L/2 * dx/magnitude;
// Draw a line from [x-xDecal, y-yDecal] to [x+xDecal, y+yDecal]
}

-