Skip to content

Commit

Permalink
Add Geofencing (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtduc693 authored Oct 25, 2024
1 parent b19d093 commit 93febae
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Algorithms.Tests/Other/GeofenceTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
37 changes: 37 additions & 0 deletions Algorithms/Other/Geofence.cs
Original file line number Diff line number Diff line change
@@ -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;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="userLatitude">The latitude of the user's current location.</param>
/// <param name="userLongitude">The longitude of the user's current location.</param>
/// <returns>Returns true if the user is inside the geofence, otherwise returns false.</returns>
public bool IsInside(double userLatitude, double userLongitude)
{
double distance = GeoLocation.CalculateDistanceFromLatLng(Latitude, Longitude, userLatitude, userLongitude);
return distance <= RadiusInMeters;
}
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 93febae

Please sign in to comment.