From 93febae9b1746ef70ab58e5ffaec20b439034dff Mon Sep 17 00:00:00 2001
From: Duc Nguyen Thanh <58723762+ngtduc693@users.noreply.github.com>
Date: Sat, 26 Oct 2024 03:21:26 +0700
Subject: [PATCH] Add Geofencing (#487)
---
Algorithms.Tests/Other/GeofenceTests.cs | 66 +++++++++++++++++++++++++
Algorithms/Other/Geofence.cs | 37 ++++++++++++++
README.md | 1 +
3 files changed, 104 insertions(+)
create mode 100644 Algorithms.Tests/Other/GeofenceTests.cs
create mode 100644 Algorithms/Other/Geofence.cs
diff --git a/Algorithms.Tests/Other/GeofenceTests.cs b/Algorithms.Tests/Other/GeofenceTests.cs
new file mode 100644
index 00000000..30dcfff4
--- /dev/null
+++ b/Algorithms.Tests/Other/GeofenceTests.cs
@@ -0,0 +1,66 @@
+using Algorithms.Other;
+using NUnit.Framework;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Algorithms.Tests.Other
+{
+ [TestFixture]
+ public class GeofenceTests
+ {
+ private Geofence? geofence;
+
+ [SetUp]
+ public void Setup()
+ {
+ geofence = new Geofence(10.8231, 106.6297, 500);
+ }
+
+ [Test]
+ public void IsInside_ShouldReturnTrue_WhenUserIsInsideGeofence()
+ {
+ double userLat = 10.8221;
+ double userLon = 106.6289;
+
+ bool? result = geofence?.IsInside(userLat, userLon);
+
+ Assert.That(result, Is.True);
+ }
+
+ [Test]
+ public void IsInside_ShouldReturnFalse_WhenUserIsOutsideGeofence()
+ {
+ double userLat = 10.8300;
+ double userLon = 106.6400;
+
+ bool? result = geofence?.IsInside(userLat, userLon);
+
+ Assert.That(result, Is.False);
+ }
+
+ [Test]
+ public void IsInside_ShouldReturnTrue_WhenUserIsExactlyOnGeofenceBoundary()
+ {
+ double userLat = 10.8231;
+ double userLon = 106.6297;
+
+ bool? result = geofence?.IsInside(userLat, userLon);
+
+ Assert.That(result, Is.True);
+ }
+
+ [Test]
+ public void IsInside_ShouldReturnFalse_WhenUserIsFarFromGeofence()
+ {
+ double userLat = 20.0000;
+ double userLon = 100.0000;
+
+ bool? result = geofence?.IsInside(userLat, userLon);
+
+ Assert.That(result, Is.False);
+ }
+ }
+}
diff --git a/Algorithms/Other/Geofence.cs b/Algorithms/Other/Geofence.cs
new file mode 100644
index 00000000..90fb9626
--- /dev/null
+++ b/Algorithms/Other/Geofence.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Algorithms.Other
+{
+ public class Geofence
+ {
+ public double Latitude { get; set; }
+
+ public double Longitude { get; set; }
+
+ public double RadiusInMeters { get; set; }
+
+ public Geofence(double latitude, double longitude, double radiusInMeters)
+ {
+ Latitude = latitude;
+ Longitude = longitude;
+ RadiusInMeters = radiusInMeters;
+ }
+
+ ///
+ /// Checks whether the provided user location (latitude and longitude) is within the geofence boundary.
+ /// The geofence is defined by a center point (latitude, longitude) and a radius in meters.
+ ///
+ /// The latitude of the user's current location.
+ /// The longitude of the user's current location.
+ /// Returns true if the user is inside the geofence, otherwise returns false.
+ public bool IsInside(double userLatitude, double userLongitude)
+ {
+ double distance = GeoLocation.CalculateDistanceFromLatLng(Latitude, Longitude, userLatitude, userLongitude);
+ return distance <= RadiusInMeters;
+ }
+ }
+}
diff --git a/README.md b/README.md
index a4d4663f..8085ccab 100644
--- a/README.md
+++ b/README.md
@@ -223,6 +223,7 @@ find more than one implementation for the same objective but using different alg
* [Julian Easter](./Algorithms/Other/JulianEaster.cs)
* [Pollard's Rho](./Algorithms/Other/PollardsRhoFactorizing.cs)
* [GeoLocation Hash](./Algorithms/Other/Geohash.cs)
+ * [Geofencing](./Algorithms/Other/Geofence.cs)
* [Triangulation Algorithm](./Algorithms/Other/Triangulator.cs)
* [Problems](./Algorithms/Problems)
* [Stable Marriage](./Algorithms/Problems/StableMarriage)