61 lines
No EOL
2.6 KiB
C#
61 lines
No EOL
2.6 KiB
C#
namespace Interlinked.Shared;
|
|
|
|
public class Geographical{
|
|
const double km = 111.1;//one degree in kilometres
|
|
public static bool isInArea(Coordinate Center, Coordinate Check, double radius){//longlat given in degrees, radius given in Kilometres
|
|
//(Check.x-Center.long)^2+(Check.lat-Center.lat) = (radius/km)^2
|
|
double opSign = (Center.Longtitude/(Math.Abs(Center.Longtitude)));
|
|
double radiusSquared1 = Math.Pow((Check.Longtitude - Center.Longtitude), 2) +
|
|
Math.Pow((Check.Latitude - Center.Latitude), 2);
|
|
double radiusSquared2 = Math.Pow(((Check.Longtitude) - Center.Longtitude + (opSign * 180)), 2) +
|
|
Math.Pow((Check.Latitude - Center.Latitude), 2);
|
|
if(radiusSquared1 <= Math.Pow((radius/km), 2) || radiusSquared2 <= Math.Pow((radius/km), 2)){
|
|
return true;
|
|
}
|
|
else{
|
|
return false;
|
|
}
|
|
}
|
|
public static Coordinate[] GetInclusionZone(Coordinate center, double maxInclusion = 20)//maximum distance in kilometres//just use this, the circle is cool but useless.
|
|
{
|
|
double maxDistance = KilometreToDegree(maxInclusion);
|
|
double opSign = (center.Longtitude/(Math.Abs(center.Longtitude)));
|
|
return new Coordinate[]
|
|
{
|
|
new Coordinate(center.Longtitude-maxDistance, center.Latitude-maxDistance),
|
|
new Coordinate(center.Longtitude+maxDistance, center.Latitude+maxDistance),
|
|
new Coordinate(-(center.Longtitude-maxDistance+(opSign * 180)), center.Latitude-maxDistance),
|
|
new Coordinate(-(center.Longtitude+maxDistance+(opSign * 180)), center.Latitude+maxDistance)
|
|
};//returns positive and negative inclusion boundaries for either side of the globe
|
|
}
|
|
public static double DegreeToKilometre(double degree){
|
|
return degree * km;
|
|
}
|
|
public static double KilometreToDegree(double kilometre){
|
|
return kilometre/km;
|
|
}
|
|
public Coordinate ParseStringToCoord(string input){
|
|
string Longtitude = "";
|
|
string Latitude = "";
|
|
bool comma = false;
|
|
char c;
|
|
for(int i = 0; i < input.Length; i++){
|
|
c = input[i];
|
|
if(c == ','){comma = true; i++;}
|
|
if(comma){Latitude+=c;}
|
|
else{Longtitude+=c;}
|
|
}
|
|
return new Coordinate(Convert.ToDouble(Longtitude), Convert.ToDouble(Latitude));
|
|
}
|
|
}
|
|
|
|
public struct Coordinate{
|
|
public double Longtitude{get; init;}
|
|
public double Latitude{get; init;}
|
|
|
|
public Coordinate(double longt, double lat)
|
|
{
|
|
Longtitude = longt;
|
|
Latitude = lat;
|
|
}
|
|
} |