Skip to content

Commit 93febae

Browse files
authored
Add Geofencing (#487)
1 parent b19d093 commit 93febae

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 GeofenceTests
13+
{
14+
private Geofence? geofence;
15+
16+
[SetUp]
17+
public void Setup()
18+
{
19+
geofence = new Geofence(10.8231, 106.6297, 500);
20+
}
21+
22+
[Test]
23+
public void IsInside_ShouldReturnTrue_WhenUserIsInsideGeofence()
24+
{
25+
double userLat = 10.8221;
26+
double userLon = 106.6289;
27+
28+
bool? result = geofence?.IsInside(userLat, userLon);
29+
30+
Assert.That(result, Is.True);
31+
}
32+
33+
[Test]
34+
public void IsInside_ShouldReturnFalse_WhenUserIsOutsideGeofence()
35+
{
36+
double userLat = 10.8300;
37+
double userLon = 106.6400;
38+
39+
bool? result = geofence?.IsInside(userLat, userLon);
40+
41+
Assert.That(result, Is.False);
42+
}
43+
44+
[Test]
45+
public void IsInside_ShouldReturnTrue_WhenUserIsExactlyOnGeofenceBoundary()
46+
{
47+
double userLat = 10.8231;
48+
double userLon = 106.6297;
49+
50+
bool? result = geofence?.IsInside(userLat, userLon);
51+
52+
Assert.That(result, Is.True);
53+
}
54+
55+
[Test]
56+
public void IsInside_ShouldReturnFalse_WhenUserIsFarFromGeofence()
57+
{
58+
double userLat = 20.0000;
59+
double userLon = 100.0000;
60+
61+
bool? result = geofence?.IsInside(userLat, userLon);
62+
63+
Assert.That(result, Is.False);
64+
}
65+
}
66+
}

Algorithms/Other/Geofence.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 Geofence
10+
{
11+
public double Latitude { get; set; }
12+
13+
public double Longitude { get; set; }
14+
15+
public double RadiusInMeters { get; set; }
16+
17+
public Geofence(double latitude, double longitude, double radiusInMeters)
18+
{
19+
Latitude = latitude;
20+
Longitude = longitude;
21+
RadiusInMeters = radiusInMeters;
22+
}
23+
24+
/// <summary>
25+
/// Checks whether the provided user location (latitude and longitude) is within the geofence boundary.
26+
/// The geofence is defined by a center point (latitude, longitude) and a radius in meters.
27+
/// </summary>
28+
/// <param name="userLatitude">The latitude of the user's current location.</param>
29+
/// <param name="userLongitude">The longitude of the user's current location.</param>
30+
/// <returns>Returns true if the user is inside the geofence, otherwise returns false.</returns>
31+
public bool IsInside(double userLatitude, double userLongitude)
32+
{
33+
double distance = GeoLocation.CalculateDistanceFromLatLng(Latitude, Longitude, userLatitude, userLongitude);
34+
return distance <= RadiusInMeters;
35+
}
36+
}
37+
}

README.md

Lines changed: 1 addition & 0 deletions
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+
* [Geofencing](./Algorithms/Other/Geofence.cs)
226227
* [Triangulation Algorithm](./Algorithms/Other/Triangulator.cs)
227228
* [Problems](./Algorithms/Problems)
228229
* [Stable Marriage](./Algorithms/Problems/StableMarriage)

0 commit comments

Comments
 (0)