   3D Math Problem
  I've been trying to solve this seemingly simple math problem for a couple of days now without any luck. Any help would be appreciated.Given the user input of 2 points P1(x1, y1, z1) and P2(x2, y2, z2) and two line length L1 and L2, find a point P3 where the distance of P1 to P3 is L1 and the distance of P3 to P2 is L2.  y  |  .P3 *---- Find this Point  | /  \  |  L1 /    \  L2  |    / \  |  .P1  .P2  |x------|----------------  | \    |  z
  P3 isn't unique. There is a circle around the line that passes through p1 and p2 in which each point on the circle is L1 away from p1 and L2 away from p2.   ;  That is true. How would I find any point in that circle that is a P3 candidate?What I have so far is this:Vector3d * find_cross_point(float l1, float l2, Vector3d * p1, Vector3d * p2){Vector3d * crossPoint = NULL;float lineDistance = distance(p1, p2);if (lineDistance < l1 + l2){float percentage = (l1 / (l1 + l2));float a = percentage * lineDistance;float c = l1;float b = sqrt(pow(c, 2) - pow(a, 2));crossPoint = new Vector3d(p1->x + ((p2->x - p1->x) * percentage),p1->y + ((p2->y - p1->y) * percentage) + b, p1->z + ((p2->z - p1->z) * percentage));}return crossPoint;}But it doesn't work well if L1 != L2.   ;  If you take two vectors v1 and v2,v1 from point 3 to 1 and v2 from point 3 to 2.You can calculate their magnitudes which are L1 and L2.(x1-x3)^2 + (y1-y3)^2 = L1^2(x2-x3)^2 + (y2-y3)^2 = L2^2So you know x1,y1,x2,y2,L1 and L2.You have 2 equations and 2 unknowns x3 and y3.Lets say x1,y1 is (1,0) and x2,y2 is (7,0) for simplicity and L1=5 and L2=5.This gives :(1-x3)^2 + (0-y3)^2 = 5^2(7-x3)^2 + (0-y3)^2 = 5^2I will name x3=x and y3=y,1-2x+x^2   + y^2 = 2549-14x+x^2 + y^2 = 25Pull out y^2 and replace it in the other equation and you get49 - 14x+x^2 + 25 -1 +2x-x2=25Which is ;48 - 12x = 0So X is 4,if you put 4 in the equation you get.y^2 = 25 - 1 +8-16y^2 = 16This has to solutions 4 and -4.So there are two points that are 5 units away from p1 and p2 they are.(4,4) and (4,-4) since y has to solutions.y  |  .P3 *---- Find this Point  | /  \  |  L1 /    \  L2  |    / \  |  .P1  .P2  |    \  /L1 \/L2x------|----------------    \   /  |  \ / .P3'*---- the other point  |;  How do you want to return a point on the circle?The info you have is simply not enough.Do you want the topmost/bottommost? Or these in screen space? You have to say something about that.You can return the info about the circle: for example center point, normal, radius.It's pretty simple, wiki has an article about triangles, just have to take a look at that.Ahh, there is something: Heron's formula: you can calculate the area of the triangle from the 3 sides. "c" is simply the distance between the 2 given points. "a", "b" are the two given lengths. you know the area "A", and that So you have m_c which is the same as the radius of the circle.All you need is "x", Pythagoras will help here.So: [Edited by - szecs on May 12, 2010 1:18:00 AM];Quote:Original post by Black KnightIf you take two vectors v1 and v2,v1 from point 3 to 1 and v2 from point 3 to 2.You can calculate their magnitudes which are L1 and L2.(x1-x3)^2 + (y1-y3)^2 = L1^2(x2-x3)^2 + (y2-y3)^2 = L2^2Yeah, that's the 2D case which has two solutions in cases in which the problem isn't degenerate. In the 3D case the equations are (x1-x3)^2 + (y1-y3)^2 + (z1-z3)^2 = L1^2(x2-x3)^2 + (y2-y3)^2 + (z2-z3)^2 = L2^2and in non-degenerate cases the solution set will be a circle.   ;  I can get to the right point under P3 - lets call it P4 - that is on the P1,P2 Line by just moving the P1 vector towards P2 by the determined distance.Vector3d * find_cross_point(float l1, float l2, Vector3d * p1, Vector3d * p2){Vector3d * crossPoint = NULL;float lineDistance = distance(p1, p2);if (lineDistance < l1 + l2){float percentage = (l1 / (l1 + l2));float a = percentage * lineDistance;float c = l1;float b = sqrt(pow(c, 2) - pow(a, 2));crossPoint = new Vector3d(p2->x - p1->x, p2->y - p1->y, p2->z - p1->z);normalize(crossPoint);crossPoint->x *= a;crossPoint->y *= a;crossPoint->z *= a;crossPoint->x = p1->x + crossPoint->x;crossPoint->y = p1->y + crossPoint->y;crossPoint->z = p1->z + crossPoint->z;}return crossPoint;}Which gives me P4:  y   |  .P3 *---- Find this Point   | /  \   |  L1 /    \  L2   |    / \   |  .P1 --.- .P2   |  P4 x------|----------------   | \|  z But once I get that how would I get P3 in 3D? Adding to x, y, or z will not give me the right answer.   ;  What don't you understand man??You can't do that without additional information.Tell us more, so maybe we can help.Do you want the topmost point or the circle or what?   ;  Hehe, I do understand - I mentioned the answer to your question earlier. The position on the cicle does not matter. It could be any point that meets the requirements set by then segment lengths and the positions P1 and P2. But for the sake of argument, lets say it is the top-most point on the circle.   ;  So it doesn't matter which point?If so:take a random vector: randomthen: