Calculating the perpendicular bisector of a line?

Started by
5 comments, last by acid2 19 years, 1 month ago
Hi, If i have a line made from 2 points, how can I calculate the perpendicular bisector - to indicate the "Normal" of the line? I can calculate where the bisector starts, by just getting the absolute difference of the x and y of the two points, but I don't know how to do any more... Image Hosted by ImageShack.us Thanks, - aCiD2
Ollie"It is better to ask some of the questions than to know all the answers." ~ James Thurber[ mdxinfo | An iridescent tentacle | Game design patterns ]
Advertisement
Jusat normalize the line, and rotate it 90 degrees (PI/2 radians) by swapping the x and y components. Mathematically, you'd take a cross product of the normalized line and z-axis, but it boils down to what I said.

EDIT: Of course, this assumes that the line is 2D [smile]

Niko Suni

But what vector am I normalizing? The line is composed of 2 points so the lines origin is not necissarily 0, 0, 0... Sorry if your post covered this, but I didn't quite understand. Mind running through it again, possibly with a pseudo code sample?
Ollie"It is better to ask some of the questions than to know all the answers." ~ James Thurber[ mdxinfo | An iridescent tentacle | Game design patterns ]
The vector composed between the two points, so p1 - p2, like acid i presume you are talking about a 2d line?
Let the line run from (x1, y1) to (x2, y2). The centre of the line is thus ((x1+x2)/2, (y1+y2)/2).

The gradient of your line is (y2-y1)/(x2-x1), and the gradient of the perpendicular line is thus (x1-x2)/(y2-y1). So, your perpendicular bisector runs from ((x1+x2)/2, (y1+y2)/2) to (x1+x2)/2, (y1+y2)/2) + n * ((x1-x2)/(y2-y1).
IMO I would avoid the gradiant or slope, and use the vector representation instead, as it is valid for all lines, horizontal, vertical or otherwise.

So basically what Nik02 said. If you're still unsure about that, here's a little more detail. To find the normal to a line represented by points p1, p2:

Vector2 dir = p2 - p1;
Vector2 normal(-dir.y, dir.x);
normal.Normalize();

Note that if you want the normal to point the other way, just use:

Vector2 normal(dir.y, -dir.x);

The normal by itself is just a direction - it doesn't know anything about 'bisecting' the segment as such. If for some reason you need a segment that is perpendicular to and bisects the original segment, it can then be constructed from the segment midpoint and the normal.

'Hope that clears things up a bit.
Wow, many thanks to all of you. I wasn't expecting such an in depth set of answers - saddly me giving you a positive rating doesn't do much, but a huge thank you to you all! I got it working using normal vectors, but tried the gradient aswell (didn't work with vertical lines and I felt normals was a 'more accurate' way).

Good ol' GDNET ^_^


Again, many thanks
- aCiD2
Ollie"It is better to ask some of the questions than to know all the answers." ~ James Thurber[ mdxinfo | An iridescent tentacle | Game design patterns ]

This topic is closed to new replies.

Advertisement