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.
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?
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.
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
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
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.
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.
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)?