How to resolve the algorithm Law of cosines - triples step by step in the C# programming language
How to resolve the algorithm Law of cosines - triples step by step in the C# programming language
Table of Contents
Problem Statement
The Law of cosines states that for an angle γ, (gamma) of any triangle, if the sides adjacent to the angle are A and B and the side opposite is C; then the lengths of the sides are related by this formula: For an angle of of 90º this becomes the more familiar "Pythagoras equation": For an angle of 60º this becomes the less familiar equation: And finally for an angle of 120º this becomes the equation:
Note: Triangles with the same length sides but different order are to be treated as the same.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Law of cosines - triples step by step in the C# programming language
Overview: This C# code demonstrates how to find triples of integers (a, b, c) that represent the sides of a triangle given an angle (degrees) and a maximum side length. The code uses the Law of Cosines to determine if the triples satisfy the triangle inequality theorem.
Key Functions:
- Main2(): The entry point of the program. It invokes other functions to print examples of triangle triples.
- PrintTriples(): Prints the triples that satisfy the given criteria. It includes optional parameters for specifying the range of side lengths and whether the sides should be distinct.
- FindTriples(): Generates triples of integers that meet the Law of Cosines and have a maximum side length. It uses a formula derived from the Law of Cosines to calculate the third side (c) and filters out triples where c is not an integer.
- NotAllTheSameLength(): A lambda function that checks if all sides of a triple are not equal. This is used for filtering triples when the
notAllTheSameLength
parameter is set to true.
Key Concepts:
- Law of Cosines: A mathematical formula that relates the side lengths and angles of a triangle.
- Enumerables (from, Range): Language features used to generate sequences of integers.
- Linq (Where): Language feature for filtering sequences of values.
- Tuple: A data type that represents a small collection of values.
Usage:
- Set the angle and maximum side length in the
PrintTriples()
calls. - Specify any additional criteria, such as not allowing sides of equal length, by setting the corresponding parameters.
- Run the program to generate and display the triangle triples.
Example Output:
60 degree triangles in range 1..13
6 13 15
7 13 14
8 13 13
9 13 12
10 13 11
11 13 10
5 solutions
90 degree triangles in range 1..13
5 12 13
6 8 10
6 9 11
7 7 10
7 8 9
8 6 10
8 7 9
9 6 11
9 7 10
10 5 11
10 6 9
10 7 8
12 5 13
12 6 8
13 5 12
15 solutions
120 degree triangles in range 1..13
7 9 12
8 8 12
9 7 12
10 6 12
11 5 12
12 4 12
13 3 12
7 solutions
60 degree triangles in range 1..10000 where not all sides are the same
6 13 15
7 13 14
8 13 13
9 13 12
10 13 11
4 456 456
5 456 457
6 456 458
7 456 459
8 456 460
9 456 461
29 1980 1980
30 1980 1981
31 1980 1982
32 1980 1983
33 1980 1984
34 1980 1985
40 1631 1900
41 1631 1901
42 1631 1902
43 1631 1903
44 1631 1904
45 1631 1905
192 688 959
...
1087 solutions
Source code in the csharp programming language
using System;
using System.Collections.Generic;
using static System.Linq.Enumerable;
public static class LawOfCosinesTriples
{
public static void Main2() {
PrintTriples(60, 13);
PrintTriples(90, 13);
PrintTriples(120, 13);
PrintTriples(60, 10_000, true, false);
}
private static void PrintTriples(int degrees, int maxSideLength, bool notAllTheSameLength = false, bool print = true) {
string s = $"{degrees} degree triangles in range 1..{maxSideLength}";
if (notAllTheSameLength) s += " where not all sides are the same";
Console.WriteLine(s);
int count = 0;
var triples = FindTriples(degrees, maxSideLength);
if (notAllTheSameLength) triples = triples.Where(NotAllTheSameLength);
foreach (var triple in triples) {
count++;
if (print) Console.WriteLine(triple);
}
Console.WriteLine($"{count} solutions");
}
private static IEnumerable<(int a, int b, int c)> FindTriples(int degrees, int maxSideLength) {
double radians = degrees * Math.PI / 180;
int coefficient = (int)Math.Round(Math.Cos(radians) * -2, MidpointRounding.AwayFromZero);
int maxSideLengthSquared = maxSideLength * maxSideLength;
return
from a in Range(1, maxSideLength)
from b in Range(1, a)
let cc = a * a + b * b + a * b * coefficient
where cc <= maxSideLengthSquared
let c = (int)Math.Sqrt(cc)
where c * c == cc
select (a, b, c);
}
private static bool NotAllTheSameLength((int a, int b, int c) triple) => triple.a != triple.b || triple.a != triple.c;
}
You may also check:How to resolve the algorithm Empty program step by step in the Openscad programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Haxe programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the PL/I programming language
You may also check:How to resolve the algorithm Miller–Rabin primality test step by step in the Nim programming language