# Line equations

This topic is 4878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need some help on this. Say I have a line (x1, y1) to (x2, y2). I calculate the half way point for this line using ((x2 + x1)/2, (y2 + y1)/2). Now I want to find a point a specific distance from my line that lies on the line perpendicular to my line and passing through the half way point, and I want the line to be pointing 'out', so if I was to rotate counter-clockwise from point 2 to point 1 the point I calculated would be in that area. Anyone have any idea how I would go about doing this?
eg.  1
'\'    * (pt to calculate)
'\'
'\'
2


##### Share on other sites
Normalized vector for your line:

length = sqrt( (x2 - x1)^2 + (y2 - y1)^2 )
norm_x = (x2 - x1) / length
norm_y = (y2 - y1) / length

Vector for perpendicular line:
perpen_x = -norm_y
perpen_y = norm_x

The point you want:
pt_to_calculate_x = halfway_pt_x + perpen_x * distance_to_point
pt_to_calculate_y = halfway_pt_y + perpen_y * distance_to_point

##### Share on other sites
It's simple:

calculate normal vector: let dx = x2 - x1, dy = y2 - y1. So, the normal is N = (-dy, dx), that is the normal pointing the direction you need. Normalize it (N_normalized = N / length(N) = N / sqrt((-dy)^2 + (dx)^2) = (-dy / sqrt((-dy)^2 + (dx)^2), dx / sqrt((-dy)^2 + (dx)^2))). So, now it is unit length.

Now, your desired point P (distanced in a distance D from half-way point) is

half_way + N_normalized * d (in vector terms)

[EDIT] : sorry, while I was printing, Anonimous Poster already gave the answer ))

##### Share on other sites
Thanks a lot you guys. That was very helpful.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 13
• 9
• 15
• 14
• 46
• ### Forum Statistics

• Total Topics
634058
• Total Posts
3015288
×