public static PlaceOfInterest FindTheCenterPoint(PlaceOfInterest pt1, PlaceOfInterest pt2, PlaceOfInterest pt3) { double A1, A2, B1, B2, C1, C2, temp; A1 = pt1.Longitude - pt2.Longitude; B1 = pt1.Latitude - pt2.Latitude; C1 = (Math.Pow(pt1.Longitude, 2) - Math.Pow(pt2.Longitude, 2) + Math.Pow(pt1.Latitude, 2) - Math.Pow(pt2.Latitude, 2)) / 2; A2 = pt3.Longitude - pt2.Longitude; B2 = pt3.Latitude - pt2.Latitude; C2 = (Math.Pow(pt3.Longitude, 2) - Math.Pow(pt2.Longitude, 2) + Math.Pow(pt3.Latitude, 2) - Math.Pow(pt2.Latitude, 2)) / 2; temp = A1 * B2 - A2 * B1; PlaceOfInterest PC; if (temp == 0) { PC = new PlaceOfInterest(0, pt1.Latitude, pt1.Longitude, "circle centre"); } else { PC = new PlaceOfInterest(0, (A1 * C2 - A2 * C1) / temp, (C1 * B2 - C2 * B1) / temp, "circle centre"); } return(PC); }
public static List <PlaceOfInterest> GrahmConvexHull(List <PlaceOfInterest> pointList) { List <PlaceOfInterest> hull = new List <PlaceOfInterest>(); List <PlaceOfInterest> noDuplicates = RemoveDuplicate(pointList); noDuplicates.Sort(); PlaceOfInterest pivot = noDuplicates[0]; noDuplicates.RemoveAt(0); noDuplicates.Sort(new SortRadial(pivot)); hull.Add(pivot); hull.Add(noDuplicates[0]); noDuplicates.RemoveAt(0); hull.Add(noDuplicates[0]); noDuplicates.RemoveAt(0); while (noDuplicates.Count > 0) { hull.Add(noDuplicates[0]); noDuplicates.RemoveAt(0); while (!Validate(hull)) { hull.RemoveAt(hull.Count - 2); } } hull.Add(pivot); return(hull); }
public static List <PlaceOfInterest> MonoStoneConvexHull(List <PlaceOfInterest> points) { points.Sort(); if (points.Count <= 3) { return(new List <PlaceOfInterest>(points)); } List <PlaceOfInterest> upperHull = new List <PlaceOfInterest>(); foreach (PlaceOfInterest point in points) { PlaceOfInterest p2 = point; while (upperHull.Count >= 2) { PlaceOfInterest pivot = upperHull[upperHull.Count - 2]; PlaceOfInterest p1 = upperHull[upperHull.Count - 1]; if (Calculation.SignedArea(pivot, p1, p2) <= 0) { upperHull.RemoveAt(upperHull.Count - 1); } else { break; } } upperHull.Add(p2); } upperHull.RemoveAt(upperHull.Count - 1); List <PlaceOfInterest> lowerHull = new List <PlaceOfInterest>(); for (int i = points.Count - 1; i >= 0; i--) { PlaceOfInterest p2 = points[i]; while (lowerHull.Count >= 2) { PlaceOfInterest pivot = lowerHull[lowerHull.Count - 2]; PlaceOfInterest p1 = lowerHull[lowerHull.Count - 1]; if (Calculation.SignedArea(pivot, p1, p2) <= 0) { lowerHull.RemoveAt(lowerHull.Count - 1); } else { break; } } lowerHull.Add(p2); } lowerHull.RemoveAt(lowerHull.Count - 1); if (!(Enumerable.SequenceEqual(upperHull, lowerHull))) { upperHull.AddRange(lowerHull); } return(upperHull); }
public static double CalculateAngle(PlaceOfInterest a, PlaceOfInterest b, PlaceOfInterest c) { double bc = PythagorasDistance(b, c); double ac = PythagorasDistance(a, c); double ab = PythagorasDistance(b, a); return(Math.Acos((Math.Pow(bc, 2) - Math.Pow(ac, 2) + Math.Pow(ab, 2)) / (2 * bc * ab)) * 180 / Math.PI); //double a = Math.Pow(p2.Latitude - p1.Latitude, 2) + Math.Pow(p2.Longitude - p1.Longitude, 2); //double b = Math.Pow(p2.Latitude - p3.Latitude, 2) + Math.Pow(p2.Longitude - p3.Longitude, 2); //double c = Math.Pow(p3.Latitude - p1.Latitude, 2) + Math.Pow(p3.Longitude - p1.Longitude, 2); //return Math.Acos((a + b - c) / Math.Sqrt(4 * a * b)); }
private bool CheckDuplicacy(PlaceOfInterest poi, HashSet <PlaceOfInterest> initialHashset) { bool duplicate = false; foreach (PlaceOfInterest p in initialHashset) { if (p.Equals(poi)) { duplicate = true; } } return(duplicate); }
private static void FindSmallestAngle(PlaceOfInterest p1, PlaceOfInterest p2, List<PlaceOfInterest> hullPoints) { PlaceOfInterest k = null; foreach (PlaceOfInterest p in hullPoints) { if (p != p1 && p != p2) { k = p; break; } } double angle = Calculation.CalculateAngle(p1, k, p2); foreach (PlaceOfInterest h in hullPoints) { if (!h.Equals(p1) && !h.Equals(p2) && !h.Equals(k) && angle > Calculation.CalculateAngle(p1, h, p2)) { k = h; angle = Calculation.CalculateAngle(p1, k, p2); } } if (angle >= 90) { centerPoint = new PlaceOfInterest(1, (p1.Latitude + p2.Latitude )/ 2, (p1.Longitude + p2.Longitude) / 2, ""); radius = Calculation.PythagorasDistance(centerPoint, p1); pointOne = p1; pointTwo = k; pointThree = p2; } else if (angle < 90 && Calculation.CalculateAngle(p1, p2, k) < 90 && Calculation.CalculateAngle(p2, p1, k) < 90) { //define the circle centerPoint = Calculation.FindTheCenterPoint(p1, k, p2); radius = Calculation.PythagorasDistance(centerPoint, p2); pointOne = p1; pointTwo = k; pointThree = p2; } else if (Calculation.CalculateAngle(p1, p2, k) > 90) { FindSmallestAngle(p1, k, hullPoints); } else if (Calculation.CalculateAngle(p2, p1, k) > 90) { FindSmallestAngle(p2, k, hullPoints); } }
public static List <PlaceOfInterest> RemoveDuplicate(List <PlaceOfInterest> pointList) { List <PlaceOfInterest> noDuplicate = new List <PlaceOfInterest>(); PlaceOfInterest compareValue = new PlaceOfInterest(0, 0, 0, ""); foreach (PlaceOfInterest poi in pointList.ToList()) { if (!poi.Equals(compareValue)) { noDuplicate.Add(poi); compareValue = poi; } } return(noDuplicate); }
private List <PlaceOfInterest> GetLocationTestDat() { PlaceOfInterest p1 = new PlaceOfInterest(1, 1, 1, ""); PlaceOfInterest p2 = new PlaceOfInterest(1, 1, -1, ""); PlaceOfInterest p3 = new PlaceOfInterest(1, -1, 1, ""); PlaceOfInterest p4 = new PlaceOfInterest(1, -1, -1, ""); PlaceOfInterest p5 = new PlaceOfInterest(1, 0, 0, ""); PlaceOfInterest p6 = new PlaceOfInterest(1, 2, 2, ""); List <PlaceOfInterest> list = new List <PlaceOfInterest>(); locationList.Add(p1); locationList.Add(p2); locationList.Add(p3); locationList.Add(p4); locationList.Add(p5); locationList.Add(p6); return(locationList); }
private List <PlaceOfInterest> GetLocationData() { string url = @"http://developer.kensnz.com/getlocdata"; using (WebClient client = new WebClient()) { var json = client.DownloadString(url); JavaScriptSerializer ser = new JavaScriptSerializer(); var jsonArray = ser.Deserialize <Dictionary <string, string>[]>(json); richTextBox1.AppendText(json); foreach (Dictionary <string, string> map in jsonArray) { int userid = int.Parse(map["userid"]); int id = int.Parse(map["id"]); double latittude = double.Parse(map["latitude"]); double longitude = double.Parse(map["longitude"]); string description = map["description"]; if (latittude > -47 && latittude < -45 && longitude > 164 && longitude < 170) { PlaceOfInterest poi = new PlaceOfInterest(userid, latittude, longitude, description); if (CheckDuplicacy(poi, initialHashset)) { break; } else { initialHashset.Add(poi); } } } } locationList = initialHashset.ToList(); return(locationList); }
public static double PythagorasDistance(PlaceOfInterest p1, PlaceOfInterest p2) { return(Math.Sqrt(Math.Pow(p2.Latitude - p1.Latitude, 2) + Math.Pow(p2.Longitude - p1.Longitude, 2))); }
public static double SignedArea(PlaceOfInterest a, PlaceOfInterest b, PlaceOfInterest c) { return((a.Latitude * b.Longitude) - (b.Latitude * a.Longitude) + (b.Latitude * c.Longitude) - (c.Latitude * b.Longitude) + (c.Latitude * a.Longitude) - (a.Latitude * c.Longitude)); }