I have one line segment formed by two vectors, let’s say v1 and v2, a vector v3 and an angle a. How do I write a method in Java (I’m also using Apache Commons Math to represent a vector) which gives me a vector v4, so that the line segments v1-v2 and v3-v4 are at angle a? There are infinite v4 elements, it would even be better if I could give a size to that method so that the line segment v3-v4 has that size. (all in 2d space, angle can be radians or degrees, doesn’t matter)
Edit: as promised I have included an image of the problem I’m trying to solve. I have a line segment defined by 2 vectors (the line is a bit longer but that doesn’t matter), an angle, and a third point. I need to draw the second line which intersects the first one at angle a. Since all lines in Javafx (which I’m using here) are drawn by defining two points, I needed to find the red point (or any of the possible ones).

Edit: Using Ali’s answer I got the following method which does what I need:
public Pair<Vector2D, Vector2D> calculateFourthPoint(Vector2D v1, Vector2D v2, Vector2D v3, double angleInDegrees) {
Vector2D r = v1.subtract(v2);
double rx = r.getX();
double ry = r.getY();
double angle = toRadians(angleInDegrees);
double a = pow(rx, 2) + pow(ry, 2);
double b = 2 * sqrt(pow(rx, 2) + pow(ry, 2)) * cos(angle) * rx;
double c = pow(rx, 2) * pow(cos(angle), 2) + pow(ry, 2) * pow(cos(angle), 2) - pow(ry, 2);
double discriminant = sqrt(pow(b, 2) - (4 * a * c));
double sx1 = (-b + discriminant) / (2 * a);
double sx2 = (-b - discriminant) / (2 * a);
double sy1 = sqrt(1 - pow(sx1, 2));
double sy2 = sqrt(1 - pow(sx2, 2));
Vector2D s1 = new Vector2D(sx1, sy1);
Vector2D s2 = new Vector2D(sx2, sy2);
Vector2D v4_1 = v3.subtract(s1);
Vector2D v4_2 = v3.subtract(s2);
return new Pair<Vector2D, Vector2D>(v4_1, v4_2);
}
I don’t know Apache Commons Math so I am writing in pseudo code. Let
vxandvydenote thexandycomponents of vectorv, respectively.Let
r=v1-v2ands=v3-v4. You have 2 unknowns (namelysxandsy; andv4=v3-s) so you need 2 equations. These should be:To spell it out, the above equations are:
The first equation is linear in both
sxandsy. Let’s eliminatesyfrom the first equation (assuming thatryis not zero)and substitute this
syinto the second equation. You get a quadratic equation insy(I don’t want to write it here because it is complicated) and that has 2 solutions. You get the correspondingsxby substituting thesyvalues into (assumingrxis not zero):Finally,
v4=v3-s. You get 2 solutions for v4, one for each of the solutions to the quadratic equation. (Degenerate cases, such asrbeing a null vector, are ignored in my answer.)