Find a line intersecting a known line at right angle, given a point

Viewed 4319

This is basic graphics geometry and/or trig, and I feel dumb for asking it, but I can't remember how this goes. So:

  1. I have a line defined by two points (x1, y1) and (x2, y2).
  2. I have a third point (xp, yp) which lies somewhere else.

I want to compute the point (x', y') that lies somewhere along the line in #1, such that, when joined with the point from #2, creates a new perpendicular line to the first line. enter image description here

Thanks.

6 Answers

To all those poor souls looking for a concrete example using vectors... here I build upon Gareth's answer.

You have a vector from p to r (from 0,0 to 50,-50) and another point, q, which is at (50, 0). The right-angle intersection of q and the vector from p to r is { x: 25. y: -25 } and is derived with the following code.

const p = [0, 0];
const r = [50, -50];
const q = [50, 0];
const l = math.add(p, r);
const m = math.dot(math.subtract(q, p), r) / math.dot(r, r);

console.log('intersecting point',  math.multiply(l, m));
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/5.1.2/math.js"></script>

Based on this article

let x1, x2, y1, y2, slope, xp, yp, m, x, y
x1 = 0
y1 = 0 
x2 = 50
y2 = -50
xp = 50
yp = 0

slope = (y1 - y2) / (x1 - x2)
m = -1 / slope
x = (m * xp - yp - slope * x1 + y1) / (m - slope)
y = m * x - m * xp + yp

console.log('x: ', x, ', y: ', y )

Related