public static bool CheckPosition(Game.GameActivity gameActivity, int playerPosition) { // Get the angle between the point and the // first and last vertices. double total_angle = GetAngle(gameActivity.gamePlayArea.vertices.Last.Value.Latitude, gameActivity.gamePlayArea.vertices.Last.Value.Longitude, gameActivity.playerArray[playerPosition].currentPosition.Latitude, gameActivity.playerArray[playerPosition].currentPosition.Longitude, gameActivity.gamePlayArea.vertices.First.Value.Latitude, gameActivity.gamePlayArea.vertices.First.Value.Longitude); // Add the angles from the point // to each other pair of vertices. gameActivity.gamePlayArea.verticesNode = gameActivity.gamePlayArea.vertices.First.Next; while (true) { total_angle += GetAngle(gameActivity.gamePlayArea.verticesNode.Previous.Value.Latitude, gameActivity.gamePlayArea.verticesNode.Previous.Value.Longitude, gameActivity.playerArray[playerPosition].currentPosition.Latitude, gameActivity.playerArray[playerPosition].currentPosition.Longitude, gameActivity.gamePlayArea.verticesNode.Value.Latitude, gameActivity.gamePlayArea.verticesNode.Value.Longitude); if (gameActivity.gamePlayArea.verticesNode.Next != null) { gameActivity.gamePlayArea.verticesNode = gameActivity.gamePlayArea.verticesNode.Next; } else { break; } } // 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); }
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); }
// Find the polygon's centroid. public static LatLng FindCentroid(Game.GameActivity gameActivity) { // Add the first point to the end. int num_points = gameActivity.gamePlayArea.vertices.Count; LatLng[] pts = new LatLng[num_points + 1]; gameActivity.gamePlayArea.vertices.CopyTo(pts, 0); pts[num_points] = gameActivity.gamePlayArea.vertices.First.Value; // Find the centroid. double x = 0; double y = 0; double second_factor; for (int i = 0; i < num_points; i++) { second_factor = pts[i].Latitude * pts[i + 1].Longitude - pts[i + 1].Latitude * pts[i].Longitude; x += (pts[i].Latitude + pts[i + 1].Latitude) * second_factor; y += (pts[i].Longitude + pts[i + 1].Longitude) * second_factor; } // Divide by 6 times the polygon's area. double polygon_area = PolygonArea(gameActivity.gamePlayArea.vertices); x /= (6 * polygon_area); y /= (6 * polygon_area); // If the values are negative, the polygon is // oriented counterclockwise so reverse the signs. if (x < 0) { x = -x; y = -y; } return(new LatLng(x, y)); }
public static LatLng ExtendLineSegment(Game.GameActivity gameActivity, LatLng firstPoint, LatLng secondPoint) { double lenAB = Math.Sqrt(Math.Pow(secondPoint.Latitude - firstPoint.Latitude, 2.0) + Math.Pow(secondPoint.Longitude - firstPoint.Longitude, 2.0)); return(new LatLng(firstPoint.Latitude + (firstPoint.Latitude - secondPoint.Latitude) / lenAB * (ShortestLineSegment(gameActivity.gamePlayArea.vertices) / 1000), firstPoint.Longitude + (firstPoint.Longitude - secondPoint.Longitude) / lenAB * (ShortestLineSegment(gameActivity.gamePlayArea.vertices) / 1000))); }