// Return true if the three points form an ear. private static bool FormsEar(LatLng[] pts, int a, int b, int c) { // See if the angle ABC is concave. if (GetAngle(pts[a].Latitude, pts[a].Longitude, pts[b].Latitude, pts[b].Longitude, pts[c].Latitude, pts[c].Longitude) > 0) { // This is a concave corner so the triangle // cannot be an ear. return(false); } // Make the triangle A, B, C. Game.Triangle triangle = AddTriangle(pts[a], pts[b], pts[c]); // Check the other points to see // if they lie in triangle A, B, C. for (int i = 0; i < pts.Length; i++) { if ((i != a) && (i != b) && (i != c)) { if (PointInTriangle(triangle, pts[i])) { // This point is in the triangle // do this is not an ear. return(false); } } } // This is an ear. return(true); }
public static LatLng FindRandomPoint(Game.GameActivity gameActivity) { Random random = new Random(); List <Game.Triangle> triangles = Triangulate(gameActivity.gamePlayArea.vertices); Game.Triangle triangle = triangles[random.Next(0, triangles.Count)]; LatLng v1 = new LatLng(triangle.vertices[1].Latitude - triangle.vertices[0].Latitude, triangle.vertices[1].Longitude - triangle.vertices[0].Longitude); LatLng v2 = new LatLng(triangle.vertices[2].Latitude - triangle.vertices[0].Latitude, triangle.vertices[2].Longitude - triangle.vertices[0].Longitude); double a1 = random.NextDouble(); double a2 = random.NextDouble(); v1.Latitude = v1.Latitude * a1; v1.Longitude = v1.Longitude * a1; v2.Latitude = v2.Latitude * a2; v2.Longitude = v2.Longitude * a2; LatLng x = new LatLng(v1.Latitude + v2.Latitude, v1.Longitude + v2.Longitude); x.Latitude += triangle.vertices[0].Latitude; x.Longitude += triangle.vertices[0].Longitude; return(x); }
private static Game.Triangle AddTriangle(LatLng a, LatLng b, LatLng c) { Game.Triangle triangle = new Game.Triangle(); triangle.vertices = new LatLng[3]; triangle.vertices[0] = a; triangle.vertices[1] = b; triangle.vertices[2] = c; return(triangle); }
// Return true if the point is in the polygon. private static bool PointInTriangle(Game.Triangle triangle, LatLng point) { // Get the angle between the point and the // first and last vertices. int max_point = triangle.vertices.Length - 1; double total_angle = GetAngle(triangle.vertices[max_point].Latitude, triangle.vertices[max_point].Longitude, point.Latitude, point.Longitude, triangle.vertices[0].Latitude, triangle.vertices[0].Longitude); // Add the angles from the point // to each other pair of vertices. for (int i = 0; i < max_point; i++) { total_angle += GetAngle(triangle.vertices[i].Latitude, triangle.vertices[i].Longitude, point.Latitude, point.Longitude, triangle.vertices[i + 1].Latitude, triangle.vertices[i + 1].Longitude); } // The total angle should be 2 * PI or -2 * PI if // the point is in the polygon and close to zero // if the point is outside the polygon. return(Math.Abs(total_angle) > 0.000001); }