Skip to content

Commit b19d093

Browse files
authored
Add Triangulation algorithm (#489)
1 parent 013a6b9 commit b19d093

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using Algorithms.Other;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Algorithms.Tests.Other
10+
{
11+
[TestFixture]
12+
public class TriangulatorTests
13+
{
14+
[Test]
15+
public void CalculatePosition_ValidCoordinatesAndDistances_ReturnsExpectedPosition()
16+
{
17+
var triangulator = new Triangulator();
18+
var baseLocations = new List<(double Latitude, double Longitude)>
19+
{
20+
(16.054407, 108.202167),
21+
(16.049807, 108.218991),
22+
(16.063597, 108.215553)
23+
};
24+
25+
var distances = new List<double> { 0.5, 0.7, 0.6 };
26+
27+
var expectedPosition = (Latitude: 16.054, Longitude: 108.210);
28+
var result = triangulator.CalculatePosition(baseLocations, distances);
29+
30+
Assert.That(result.Latitude, Is.EqualTo(expectedPosition.Latitude).Within(0.01));
31+
Assert.That(result.Longitude, Is.EqualTo(expectedPosition.Longitude).Within(0.01));
32+
}
33+
34+
[Test]
35+
public void CalculatePosition_InvalidBaseLocations_ThrowsArgumentException()
36+
{
37+
var triangulator = new Triangulator();
38+
var baseLocations = new List<(double Latitude, double Longitude)>
39+
{
40+
(10.762622, 106.660172)
41+
};
42+
var distances = new List<double> { 1.0 };
43+
44+
Assert.That(() => triangulator.CalculatePosition(baseLocations, distances), Throws.ArgumentException);
45+
}
46+
47+
[Test]
48+
public void CalculatePosition_InvalidDistances_ThrowsArgumentException()
49+
{
50+
var triangulator = new Triangulator();
51+
var baseLocations = new List<(double Latitude, double Longitude)>
52+
{
53+
(10.762622, 106.660172),
54+
(10.774981, 106.665504),
55+
(10.771817, 106.681179)
56+
};
57+
var distances = new List<double> { 1.0 };
58+
59+
Assert.That(() => triangulator.CalculatePosition(baseLocations, distances), Throws.ArgumentException);
60+
}
61+
}
62+
}

Algorithms/Other/Triangulator.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Algorithms.Other
8+
{
9+
public class Triangulator
10+
{
11+
public (double Latitude, double Longitude) CalculatePosition(List<(double Latitude, double Longitude)> baseLocations, List<double> distances)
12+
{
13+
if (baseLocations.Count < 3 || distances.Count < 3)
14+
{
15+
throw new ArgumentException("At least three points and corresponding distances are required.");
16+
}
17+
18+
// Get the coordinates of the three base stations
19+
double lat1 = baseLocations[0].Latitude;
20+
double lon1 = baseLocations[0].Longitude;
21+
double lat2 = baseLocations[1].Latitude;
22+
double lon2 = baseLocations[1].Longitude;
23+
double lat3 = baseLocations[2].Latitude;
24+
double lon3 = baseLocations[2].Longitude;
25+
26+
// Convert coordinates to radians
27+
lat1 = ToRadians(lat1);
28+
lon1 = ToRadians(lon1);
29+
lat2 = ToRadians(lat2);
30+
lon2 = ToRadians(lon2);
31+
lat3 = ToRadians(lat3);
32+
lon3 = ToRadians(lon3);
33+
34+
// Calculate the center point
35+
double centerLat = (lat1 + lat2 + lat3) / 3;
36+
double centerLon = (lon1 + lon2 + lon3) / 3;
37+
38+
// Convert back to degrees
39+
centerLat = ToDegrees(centerLat);
40+
centerLon = ToDegrees(centerLon);
41+
42+
return (centerLat, centerLon);
43+
}
44+
45+
private double ToRadians(double degrees)
46+
{
47+
return degrees * Math.PI / 180;
48+
}
49+
50+
private double ToDegrees(double radians)
51+
{
52+
return radians * 180 / Math.PI;
53+
}
54+
}
55+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ find more than one implementation for the same objective but using different alg
223223
* [Julian Easter](./Algorithms/Other/JulianEaster.cs)
224224
* [Pollard's Rho](./Algorithms/Other/PollardsRhoFactorizing.cs)
225225
* [GeoLocation Hash](./Algorithms/Other/Geohash.cs)
226+
* [Triangulation Algorithm](./Algorithms/Other/Triangulator.cs)
226227
* [Problems](./Algorithms/Problems)
227228
* [Stable Marriage](./Algorithms/Problems/StableMarriage)
228229
* [Gale-Shapley](./Algorithms/Problems/StableMarriage/GaleShapley.cs)

0 commit comments

Comments
 (0)