# Calculating angle of triangle

Sorry, this is a math question more than a Processing one.

I’m trying to calculate an angle “a” that will always give a specified constant length “d” for any given position of p.
P can move, and the other two points are always intersecting with the square. So right now, as I move “p”, the length of all three sides change.

I’m terrible at trigonometry and I don’t even know if this is possible.

Ultimately this will be used to build boundaries for box2d.

Any help appreciated!

Sounds like a job for the Law of sines, and / or Law of cosines (and a bit of Pythagoras I guess).

You’d need to know some other variables though (like the two other angles, or side lengths), and it should be possible to re-arrange the equations to get an answer.

Btw any other constraints? Is side “d” always on opposite square sides like that?

2 Likes

Here’s a video that might make it a bit more clear:

Currently I set the position “p”, angle “a” and also a global offset angle manually, and it projects two points from “p” and gives me “d”. It would need to work in any orientation but “a” would never need to go beyond or even close to 180 degrees.

as @raron said, law of sines a cosines is required to calculate angles of non right angled triangles, and Pythagoras for right angled triangles.

As for how to get the angles, using pvectors you could make use of the heading function, to find the angle of direction of the lines, where one point would be the location and the distance between it and another would be the magnitude, or you could use atan2 when utilising two points. the function atan is also useable but will require a lot of extra math so best to stick to atan2. Then you can just take one line angle away from the other to figure it out.

Now this is assuming you know the location of the vertices.

Is this a working code you already have?

Yes, this is mine

I’m starting to think it’s impossible in its current form because I realised I can’t know the vertices until I have the angle I’m trying to solve, as that it what determines those vertices by projecting rays and finding the intersections.

If I’m defining the vertices then by definition I control the length of d

Here is a code snippet of angle calculation between two lines, that maybe helps.

``````// {(x1, y1), (x2, y2)}  {(x3, y3), (x4, y4)}.
dx1 = x2-x1;
dy1 = y2-y1;
dx2 = x4-x3;
dy2 = y4-y3;
d = dx1*dx2 + dy1*dy2;   // dot product of the 2 vectors
l2 = (dx1*dx1+dy1*dy1)*(dx2*dx2+dy2*dy2); // product of the squared lengths
angle = acos(d/sqrt(l2));
let deg = degrees(angle);
if (deg >= 90) deg = 180 - deg;
text(deg.toFixed(2)+' degrees', 120, 460);
``````

are you casting rays with the mouse?

Correct, with a separate variable that adjust the orientation of the whole triangle controlled by keyboard, and a third value that controls the angle “a” that I want to solve, also by keyboard currently

and d always has to be the same length?

are you looking for something like this by any chance?

2 Likes

Law of cosines:

`c^2 = a^2 + b^2 - 2*a*b*cos(C)`

But substituting for your variables a and d:

`d^2 = b^2 + c^2 - 2*b*c*cos(a)`
(I think).

Here b and c are the lengths of the other two sides going from point p to each endpoint of line d. If you know the endpoints and point p, it should be easy to calculate with pythagoras.

Re-arranging:

`cos(a) = (b^2 + c^2 - d^2) / (2*b*c)`

Then to get a:

`a = acos((b^2 + c^2 - d^2) / (2*b*c))`

EDIT: Grammar.

1 Like

Ha I like it! I built something similar but with java processing, not sure if there’s a way to post that? That’s actually where I took my ray class from. However that isn’t the problem. I have casting working - that’s how I find the intersections of the two rays emitted from the source point and the edge boundaries.

But that doesn’t help me solve the angle I’m looking for, as far as I can see. Great sketch though!

Thanks for the help! However I suspect I’m at a dead end since the lengths of all three sides of the triangle are themselves derived from the angle that I want to solve (which I currently set myself), and so therefore can’t be used in an equation since they don’t exist until I specify an angle.

I didn’t make that clear enough in my question as I didn’t realise it was so important! However I’ll call it a win because I’ve learned some math.

Ah ok.

One way would probably be what @paulgoux suggested, using PVectors and their methods.

Or another way is to calculate the points at which the lines going from point p intersects with the walls, then you’d have the endpoints of line d, and a means to get their lengths (b and c in the equation above).

Provided you have the points for those lines though (the ones emanating from point p)?

Here’s an example of that.
The text passage describes a problem where the user is trying to calculate an angle "a" that will always give a specified constant length "d" for any given position of point "p" in a geometric setup. The user is seeking help with the trigonometric calculations and has provided some context and a code snippet for calculating the angle between two lines.
single_question

the passage contains a single mathematical question about calculating an angle in a geometric setup. however, no specific answer to this question is provided in the text. the discussion revolves around potential methods and formulas that could be used to solve the problem, but a definitive answer is not given.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# Calculating angle of triangle

Sorry, this is a math question more than a Processing one.

I’m trying to calculate an angle “a” that will always give a specified constant length “d” for any given position of p.
P can move, and the other two points are always intersecting with the square. So right now, as I move “p”, the length of all three sides change.

I’m terrible at trigonometry and I don’t even know if this is possible.

Ultimately this will be used to build boundaries for box2d.

Any help appreciated!

Sounds like a job for the Law of sines, and / or Law of cosines (and a bit of Pythagoras I guess).

You’d need to know some other variables though (like the two other angles, or side lengths), and it should be possible to re-arrange the equations to get an answer.

Btw any other constraints? Is side “d” always on opposite square sides like that?

2 Likes

Here’s a video that might make it a bit more clear:

Currently I set the position “p”, angle “a” and also a global offset angle manually, and it projects two points from “p” and gives me “d”. It would need to work in any orientation but “a” would never need to go beyond or even close to 180 degrees.

as @raron said, law of sines a cosines is required to calculate angles of non right angled triangles, and Pythagoras for right angled triangles.

As for how to get the angles, using pvectors you could make use of the heading function, to find the angle of direction of the lines, where one point would be the location and the distance between it and another would be the magnitude, or you could use atan2 when utilising two points. the function atan is also useable but will require a lot of extra math so best to stick to atan2. Then you can just take one line angle away from the other to figure it out.

Now this is assuming you know the location of the vertices.

Is this a working code you already have?

Yes, this is mine

I’m starting to think it’s impossible in its current form because I realised I can’t know the vertices until I have the angle I’m trying to solve, as that it what determines those vertices by projecting rays and finding the intersections.

If I’m defining the vertices then by definition I control the length of d

Here is a code snippet of angle calculation between two lines, that maybe helps.

``````// {(x1, y1), (x2, y2)}  {(x3, y3), (x4, y4)}.
dx1 = x2-x1;
dy1 = y2-y1;
dx2 = x4-x3;
dy2 = y4-y3;
d = dx1*dx2 + dy1*dy2;   // dot product of the 2 vectors
l2 = (dx1*dx1+dy1*dy1)*(dx2*dx2+dy2*dy2); // product of the squared lengths
angle = acos(d/sqrt(l2));
let deg = degrees(angle);
if (deg >= 90) deg = 180 - deg;
text(deg.toFixed(2)+' degrees', 120, 460);
``````

are you casting rays with the mouse?

Correct, with a separate variable that adjust the orientation of the whole triangle controlled by keyboard, and a third value that controls the angle “a” that I want to solve, also by keyboard currently

and d always has to be the same length?

are you looking for something like this by any chance?

2 Likes

Law of cosines:

`c^2 = a^2 + b^2 - 2*a*b*cos(C)`

But substituting for your variables a and d:

`d^2 = b^2 + c^2 - 2*b*c*cos(a)`
(I think).

Here b and c are the lengths of the other two sides going from point p to each endpoint of line d. If you know the endpoints and point p, it should be easy to calculate with pythagoras.

Re-arranging:

`cos(a) = (b^2 + c^2 - d^2) / (2*b*c)`

Then to get a:

`a = acos((b^2 + c^2 - d^2) / (2*b*c))`

EDIT: Grammar.

1 Like

Ha I like it! I built something similar but with java processing, not sure if there’s a way to post that? That’s actually where I took my ray class from. However that isn’t the problem. I have casting working - that’s how I find the intersections of the two rays emitted from the source point and the edge boundaries.

But that doesn’t help me solve the angle I’m looking for, as far as I can see. Great sketch though!

Thanks for the help! However I suspect I’m at a dead end since the lengths of all three sides of the triangle are themselves derived from the angle that I want to solve (which I currently set myself), and so therefore can’t be used in an equation since they don’t exist until I specify an angle.

I didn’t make that clear enough in my question as I didn’t realise it was so important! However I’ll call it a win because I’ve learned some math.

Ah ok.

One way would probably be what @paulgoux suggested, using PVectors and their methods.

Or another way is to calculate the points at which the lines going from point p intersects with the walls, then you’d have the endpoints of line d, and a means to get their lengths (b and c in the equation above).

Provided you have the points for those lines though (the ones emanating from point p)?

Here’s an example of that.
user
The text passage describes a problem where the user is trying to calculate an angle "a" that will always give a specified constant length "d" for any given position of point "p" in a geometric setup. The user is seeking help with the trigonometric calculations and has provided some context and a code snippet for calculating the angle between two lines.

TYPE: SINGLE_QUESTION

The passage contains a single mathematical question about calculating an angle in a geometric setup. However, no specific answer to this question is provided in the text. The discussion revolves around potential methods and formulas that could be used to solve the problem, but a definitive answer is not given.