Quantcast
Channel: VBForums - Maths Forum
Viewing all articles
Browse latest Browse all 112

[RESOLVED] [C#] Finding angles based on side lengths

$
0
0
I've been following solutions on various sites about the law of sines and law of cosines. However, I must be all mixed up or something because I can't figure out the formulas.

I know the lengths of all 3 sides of the triangle:
Code:

sideLength1 = Math.Sqrt(Math.Pow((Right.X - Middle.X), 2) + Math.Pow((Right.Y - Middle.Y), 2)); //length from bottom right length to paddle
                sideLength2 = Math.Sqrt(Math.Pow((e.X - Middle.X), 2) + Math.Pow((e.Y - Middle.Y), 2)); //length from paddle to mouse point
                sideLength3 = Math.Sqrt(Math.Pow((Right.X - e.X), 2) + Math.Pow((Right.Y - e.Y), 2)); //length from mouse point to bottom right


The middle point of the red rectangle only ever changes via its x-axis. The right bottom point against the yellow line will never change. I am looking to get the angle against the middle of the red rectangle, in every scenario. The top points of the pink lines will be from a mouse click, and since those vary, the lengths of the sides will vary. But, I calculated the side lengths above and debugged those as correct.

The reason for the test of the largest side is because of the law of cosines (I think). If the 3rd side is the longest, then the calculated angle is obviously the one against the middle of the red rectangle. However, in debugging, I've come across angle values calculated at 2 and 0 with only so much as adding a couple of parentheses.
Code:

if (sideLength1 > sideLength2 && sideLength1 > sideLength3)
                    largestSide = 1;
                else if (sideLength2 > sideLength1 && sideLength2 > sideLength3)
                    largestSide = 2;
                else
                    largestSide = 3;
                if (largestSide == 1)
                {
                    largestAngle = (int)Math.Acos((Math.Pow(sideLength1, 2) - (Math.Pow(sideLength2, 2) + Math.Pow(sideLength3, 2))) / (2 * sideLength2 * sideLength3));
                }
                else if (largestSide == 2)
                {
                    largestAngle = (int)Math.Acos((Math.Pow(sideLength2, 2) - (Math.Pow(sideLength1, 2) + Math.Pow(sideLength3, 2))) / (2 * sideLength1 * sideLength3));
                }
                else
                {
                    largestAngle = (int)Math.Acos((Math.Pow(sideLength3, 2) - (Math.Pow(sideLength1, 2) + Math.Pow(sideLength2, 2))) / (2 * sideLength1 * sideLength2));
                }

                if (largestSide == 3)
                {
                    b.Angle = largestAngle;
                    return;
                }

I'm also needing to calculate the other 2 angles in the case the largestSide isn't 3. But, I'd rather get one part of this working before adding more to it.

Viewing all articles
Browse latest Browse all 112

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>