Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Algorithms/Numeric/AdditionWithoutArithmetic.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Numerics;

namespace Algorithms.Numeric;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions Algorithms/Numeric/KrishnamurthyNumberChecker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;

namespace Algorithms.Numeric;

/// <summary>
Expand Down
51 changes: 22 additions & 29 deletions Algorithms/Other/Geofence.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Other;

namespace Algorithms.Other
public class Geofence
{
public class Geofence
{
public double Latitude { get; set; }
public double Latitude { get; set; }

public double Longitude { get; set; }
public double Longitude { get; set; }

public double RadiusInMeters { get; set; }
public double RadiusInMeters { get; set; }

public Geofence(double latitude, double longitude, double radiusInMeters)
{
Latitude = latitude;
Longitude = longitude;
RadiusInMeters = radiusInMeters;
}
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;
}
/// <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;
}
}
117 changes: 56 additions & 61 deletions Algorithms/Other/Geohash.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Other
namespace Algorithms.Other;

public static class Geohash
{
public static class Geohash
private const string Base32Characters = "0123456789bcdefghjkmnpqrstuvwxyz"; // Convert latitude and longitude coordinates into a concise string
private const int GeohashLength = 12; // ± 1.86 cm

/// <summary>
/// Encodes the provided latitude and longitude coordinates into a Geohash string.
/// Geohashing is a method to encode geographic coordinates (latitude, longitude).
/// into a short string of letters and digits. Each character in the resulting Geohash .
/// string adds more precision to the location. The longer the Geohash, the smaller the area.
/// </summary>
/// <param name="latitude">The latitude of the location to encode. It must be a value between -90 and 90.</param>
/// <param name="longitude">The longitude of the location to encode. It must be a value between -180 and 180.</param>
/// <returns>
/// A Geohash string of length 12 representing the location with high precision.
/// A longer Geohash provides higher precision in terms of geographic area.
/// and a 12-character Geohash can be accurate down to around 1.86 cm.
/// </returns>
public static string Encode(double latitude, double longitude)
{
private const string Base32Characters = "0123456789bcdefghjkmnpqrstuvwxyz"; // Convert latitude and longitude coordinates into a concise string
private const int GeohashLength = 12; // ± 1.86 cm
double[] latitudeRange = new[] { -90.0, 90.0 };
double[] longitudeRange = new[] { -180.0, 180.0 };
bool isEncodingLongitude = true;
int currentBit = 0;
int base32Index = 0;
StringBuilder geohashResult = new StringBuilder();

/// <summary>
/// Encodes the provided latitude and longitude coordinates into a Geohash string.
/// Geohashing is a method to encode geographic coordinates (latitude, longitude).
/// into a short string of letters and digits. Each character in the resulting Geohash .
/// string adds more precision to the location. The longer the Geohash, the smaller the area.
/// </summary>
/// <param name="latitude">The latitude of the location to encode. It must be a value between -90 and 90.</param>
/// <param name="longitude">The longitude of the location to encode. It must be a value between -180 and 180.</param>
/// <returns>
/// A Geohash string of length 12 representing the location with high precision.
/// A longer Geohash provides higher precision in terms of geographic area.
/// and a 12-character Geohash can be accurate down to around 1.86 cm.
/// </returns>
public static string Encode(double latitude, double longitude)
while (geohashResult.Length < GeohashLength)
{
double[] latitudeRange = new[] { -90.0, 90.0 };
double[] longitudeRange = new[] { -180.0, 180.0 };
bool isEncodingLongitude = true;
int currentBit = 0;
int base32Index = 0;
StringBuilder geohashResult = new StringBuilder();
double midpoint;

while (geohashResult.Length < GeohashLength)
if (isEncodingLongitude)
{
double midpoint;

if (isEncodingLongitude)
midpoint = (longitudeRange[0] + longitudeRange[1]) / 2;
if (longitude > midpoint)
{
midpoint = (longitudeRange[0] + longitudeRange[1]) / 2;
if (longitude > midpoint)
{
base32Index |= 1 << (4 - currentBit);
longitudeRange[0] = midpoint;
}
else
{
longitudeRange[1] = midpoint;
}
base32Index |= 1 << (4 - currentBit);
longitudeRange[0] = midpoint;
}
else
{
midpoint = (latitudeRange[0] + latitudeRange[1]) / 2;
if (latitude > midpoint)
{
base32Index |= 1 << (4 - currentBit);
latitudeRange[0] = midpoint;
}
else
{
latitudeRange[1] = midpoint;
}
longitudeRange[1] = midpoint;
}

isEncodingLongitude = !isEncodingLongitude;

if (currentBit < 4)
}
else
{
midpoint = (latitudeRange[0] + latitudeRange[1]) / 2;
if (latitude > midpoint)
{
currentBit++;
base32Index |= 1 << (4 - currentBit);
latitudeRange[0] = midpoint;
}
else
{
geohashResult.Append(Base32Characters[base32Index]);
currentBit = 0;
base32Index = 0;
latitudeRange[1] = midpoint;
}
}

return geohashResult.ToString();
isEncodingLongitude = !isEncodingLongitude;

if (currentBit < 4)
{
currentBit++;
}
else
{
geohashResult.Append(Base32Characters[base32Index]);
currentBit = 0;
base32Index = 0;
}
}

return geohashResult.ToString();
}
}
1 change: 0 additions & 1 deletion Algorithms/Other/JulianEaster.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Globalization;

namespace Algorithms.Other;

Expand Down
84 changes: 40 additions & 44 deletions Algorithms/Other/Triangulator.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Other
namespace Algorithms.Other;

public class Triangulator
{
public class Triangulator
public (double Latitude, double Longitude) CalculatePosition(List<(double Latitude, double Longitude)> baseLocations, List<double> distances)
{
public (double Latitude, double Longitude) CalculatePosition(List<(double Latitude, double Longitude)> baseLocations, List<double> distances)
if (baseLocations.Count < 3 || distances.Count < 3)
{
if (baseLocations.Count < 3 || distances.Count < 3)
{
throw new ArgumentException("At least three points and corresponding distances are required.");
}

// Get the coordinates of the three base stations
double lat1 = baseLocations[0].Latitude;
double lon1 = baseLocations[0].Longitude;
double lat2 = baseLocations[1].Latitude;
double lon2 = baseLocations[1].Longitude;
double lat3 = baseLocations[2].Latitude;
double lon3 = baseLocations[2].Longitude;

// Convert coordinates to radians
lat1 = ToRadians(lat1);
lon1 = ToRadians(lon1);
lat2 = ToRadians(lat2);
lon2 = ToRadians(lon2);
lat3 = ToRadians(lat3);
lon3 = ToRadians(lon3);

// Calculate the center point
double centerLat = (lat1 + lat2 + lat3) / 3;
double centerLon = (lon1 + lon2 + lon3) / 3;

// Convert back to degrees
centerLat = ToDegrees(centerLat);
centerLon = ToDegrees(centerLon);

return (centerLat, centerLon);
throw new ArgumentException("At least three points and corresponding distances are required.");
}

private double ToRadians(double degrees)
{
return degrees * Math.PI / 180;
}
// Get the coordinates of the three base stations
double lat1 = baseLocations[0].Latitude;
double lon1 = baseLocations[0].Longitude;
double lat2 = baseLocations[1].Latitude;
double lon2 = baseLocations[1].Longitude;
double lat3 = baseLocations[2].Latitude;
double lon3 = baseLocations[2].Longitude;

// Convert coordinates to radians
lat1 = ToRadians(lat1);
lon1 = ToRadians(lon1);
lat2 = ToRadians(lat2);
lon2 = ToRadians(lon2);
lat3 = ToRadians(lat3);
lon3 = ToRadians(lon3);

// Calculate the center point
double centerLat = (lat1 + lat2 + lat3) / 3;
double centerLon = (lon1 + lon2 + lon3) / 3;

// Convert back to degrees
centerLat = ToDegrees(centerLat);
centerLon = ToDegrees(centerLon);

return (centerLat, centerLon);
}

private double ToDegrees(double radians)
{
return radians * 180 / Math.PI;
}
private double ToRadians(double degrees)
{
return degrees * Math.PI / 180;
}

private double ToDegrees(double radians)
{
return radians * 180 / Math.PI;
}
}
4 changes: 0 additions & 4 deletions Algorithms/RecommenderSystem/ISimilarityCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.RecommenderSystem
{
Expand Down
38 changes: 17 additions & 21 deletions Algorithms/Shufflers/LINQShuffler.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Shufflers
namespace Algorithms.Shufflers;

/// <summary>
/// LINQ Shuffle is a simple shuffling algorithm,
/// where the elements within a collection are shuffled using
/// LINQ queries and lambda expressions in C#.
/// </summary>
/// <typeparam name="T">Type array input.</typeparam>
public class LinqShuffler<T>
{
/// <summary>
/// LINQ Shuffle is a simple shuffling algorithm,
/// where the elements within a collection are shuffled using
/// LINQ queries and lambda expressions in C#.
/// First, it will generate a random value for each element.
/// Next, it will sort the elements based on these generated
/// random numbers using OrderBy.
/// </summary>
/// <typeparam name="T">Type array input.</typeparam>
public class LinqShuffler<T>
/// <param name="array">Array to shuffle.</param>
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
public T[] Shuffle(T[] array, int? seed = null)
{
/// <summary>
/// First, it will generate a random value for each element.
/// Next, it will sort the elements based on these generated
/// random numbers using OrderBy.
/// </summary>
/// <param name="array">Array to shuffle.</param>
/// <param name="seed">Random generator seed. Used to repeat the shuffle.</param>
public T[] Shuffle(T[] array, int? seed = null)
{
var random = seed is null ? new Random() : new Random(seed.Value);
return array.OrderBy(x => random.Next()).ToArray();
}
var random = seed is null ? new Random() : new Random(seed.Value);
return array.OrderBy(x => random.Next()).ToArray();
}
}
Loading