Advertisement Jump to content
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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)

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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 this post

Link to post
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 this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!