0

I have a math problem. I have location (x,y) of point A, B, and a number (x). I want to calculate the location of point C, D. CD is perpendicular with AB and AC = AD = x.

This is the picture describe my problem:

my problem

Can anyone help me on this?

Thanks

maroux
  • 3,764
  • 3
  • 23
  • 32
haisergeant
  • 1,111
  • 2
  • 13
  • 32

2 Answers2

2

You did not specify a programming language, but from your previous questions it seems that you have some experience with (Objective-)C and Core Graphics on iOS, so this would be a solution using C and Core Graphics data structures:

// Your input data:
CGPoint A = ...
CGPoint B = ...
CGFloat x = ...

// Vector from A to B:
CGPoint vecAB = CGPointMake(B.x - A.x, B.y - A.y);
// Length of that vector:
CGFloat lenAB = hypotf(vecAB.x, vecAB.y);
// Perpendicular vector, normalized to length 1:
CGPoint perp = CGPointMake(-vecAB.y/lenAB, vecAB.x/lenAB);

CGPoint C = CGPointMake(A.x + x * perp.x, A.y + x * perp.y);
CGPoint D = CGPointMake(A.x - x * perp.x, A.y - x * perp.y);
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
1

Your question is similar to How do you find a point at a given perpendicular distance from a line? . If you want further mathemetical details, you can refer: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html

Community
  • 1
  • 1
rahulserver
  • 10,411
  • 24
  • 90
  • 164