Summit
Summit

Reputation: 2268

How can i calculate points perpendicular to a line

I have two points PointA and Point B denoted by red points.

enter image description here

i can calculate the Perpendicaular points denoted by green points ( Top Perpendicular point , Bottom Perpendicular point) when the line is horizontal.

When line has a slope how would i calculate the perpendicular points.

enter image description here

Upvotes: 0

Views: 283

Answers (1)

MBo
MBo

Reputation: 80187

Calculate direction vector of AB

dx = b.x-a.x
dy = b.y-a.y

Normalize it (make unit length)

len = sqrt(dx*dx+dy*dy)
udx = dx / len
udy = dy / len

Make perpendicular vector

px = -udy
py = udx

Calculate green points

g1.x = b.x + px*distance
g1.y = b.y + py*distance

g2.x = b.x - px*distance
g2.y = b.y - py*distance

Upvotes: 2

Related Questions