0

I think there is a pretty straightForward answer to this, but i cant find it. My geometry lessons are too far away for this. the problem is: Given 2 points A and B (coordinates Ax Ay Bx and By), I want to find the coordinates of points C and D so that [AB] and [CD] segments intersect at their center and [CD] has a length of d (a variable). I want to find the equation giving me Cx,Cy,Dx and Dy from Ax,Ay,Bx,By and d. Here is a little schema of the problem:

enter image description here

and an image of the intended result:

enter image description here

I already know how to find the center point of [AB] (Ax+Bx/2, Ay+By/2), how to find the slope of the [AB] segment (By-Ay/Bx-Ax) and then one of the [CD] segment (Ax-Bx/By-Ay). But then i get stuck on how to get my two points. I thought i could calculate the angle from the slope, then use it with some trigonometry to get the coordinates but it sounds like a quite heavy, ugly and unnecessary calculation... It feels so close, but i still cant get it.

I also found this post, which is almost perfect, but the length cannot be defined: it must be the same as the first segment.

I dont think this is language-dependent, but if you must know, i'm doing a mini prototype on processing and will probably get it on javascript later.

Thanks for any help.

Community
  • 1
  • 1
bastien girschig
  • 663
  • 6
  • 26

1 Answers1

1

The basic trick here is that, in 2d, the perpendicular to a vector (x, y) is merely ± (-y, x). (One gets this by computing the cross product with the (0, 0, 1) vector in 3d, and projecting to 2d.) So what you need to do is:

  1. Get the midpoint between A and B (you have done that).

  2. Get the vector from A to B, which is B - A = (x, y) = (bx - ax, by - ay).

  3. Get the perpendicular vector: (-y, x).

  4. Normalize it. Let length = sqrt(y*y + x*x), then norm = (-y/length, x/length).

  5. Multiply the normalized perpendicular by your desired distance ± d/2 (since you want the distance between C and D to be d), and add to the center point.

No slopes or trig functions are required.

dbc
  • 104,963
  • 20
  • 228
  • 340