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{(line2\cdot x-m\cdot x)^2+(line2\cdot y-m\cdot y)^2}$, but I dont get the right result. Thanks in advance
|
migrated from stackoverflow.com Mar 9 '11 at 11:24
|
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 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) |
|||
|
|
The line l = (A,B,C) where lx = 0 has the normal (A,B). There are two perpendicular vectors to this normal namly (A, -B) and (-A, B). Two lines are perpendicular if their normals cross proudct equals zero. We can easily see that (A,B) x (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 |
|||||
|
|
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. :) |
|||
|
|
|
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 :
.
|
|||
|
|

