So I have the following problem that is puzzling me and I can't quite figure out how to do it:
I have two vectors that intersect at one point. The vectors could come in from all kind of different angles like the following two images:
The intersection point is also known, its calculated using the cross product and I got the math behind it from this. Both the start and the end points of the vectors are known aswell.
Now I have a certain line with length X, and I would like to know where between these two vectors this line fits exactly. And then know the coordiates of those points from the vectors. I think this image describes it better:
Ofcourse there are many different ways a line with length X can fit between two vectors, for example the following two images shows different positions of line X between the Vectors A and B, where the line is the same length but the position and angle is different:
If it's possible, I would like that difference in position is determened by the length of the vectors. So if Vector B is five times as long as Vector A, the distance between S and where the line touches Vector B should be 5 times as long as the distance between S and where the line touches Vector A. Like on the above right picture where the distance between S and where the line touches Vector B is far bigger than the distance between S and where the line touches Vector A.
What would be the best way to find out the position of this line? So where on vector A the line starts and where on Vector B it ends? I want to implement this in C++, however calculating the distance between every point on both Vectors and checking if thats equals X seems very intensive and impossible with floats.
Edit: Solution found. Below I'll give a small example.
- Let's say angle c (the angle between vector A and vector B) is 90 degrees.
- The ratio is 1:2(so B is twice as long as A).
- And last the side C is 30.
What you want to do is make up numbers for the length of vector A and B. What I discovered using this site, is that wether you fill in 4 and 8 for sides A and B, or 8 and 16 or anything else having the 1:2 ratio, angles a was the same in all scenarios and so was angle b. So to calculate angles a and b just use for example 5 and 10. What you want to do first(or atleast this is how I did it) is to calculate side C using angle c and side A and B using the following formula:
sqrt(sideA * sideA + sideB * sideB - 2 * sideA * sideB * cos(degreesToRadian(angleC)));
. Note that this is not the same side C and the one given, but just one used to calculate the angles.
After that you can calculate angle a using the following formula:
radianToDegrees(acos((sideB * sideB + sideC * sideC - sideA * sideA) / (2 * sideB * sideC)))
Now that you've found angle a you will have all angles in the triangle. Because you already know angle c, you just calculated a and b = 180 - a - c. The last thing to do is calculate side A and side B using angle c, angle a and the given side C using the following formula:
sideB * sin(degreesToRadian(angleA)) / sin(degreesToRadian(angleB))
When you put it all together in one function that takes in as parameters: (float angleC, float ratioB, float sideC). In our case that is (90, 0.5, 30). Then the calculations are done as following:
float fakeSideA = 10;
float fakeSideB = fakeSideA * ratioB;
float fakeC = sqrt(fakeSideA * fakeSideA + fakeSideB * fakeSideB - 2 * fakeSideA * fakeSideB * cos(degreesToRadian(angleC)));
float angleA = radianToDegrees(acos((fakeSideB * fakeSideB + fakeC * fakeC - fakeSideA * fakeSideA) / (2 * fakeSideB * fakeC)));
float sideA = sideC * sin(degreesToRadian(angleA)) / sin(degreesToRadian(angleC));
std::cout << "SideA: " << sideA << ", AngleA: " << angleA << ", SideC: " << sideC << ", AngleC: " << angleC << std::endl;`
Output should look like: SideA: 26.8328, AngleA: 63.435, SideC: 30, AngleC: 90
. Which is correct. Knowing sideA is the length between S and where the line with length X touches vector A, you can calculate the coordinates for that.