/// <summary> /// Create a grid and place the points to the appropriate grid cells /// </summary> /// <param name="points">Point list</param> /// <param name="iconSize">Icon size</param> /// <param name="zoomLevel">Zoom level</param> /// <param name="ne_lng">North east longitude of the box</param> /// <param name="ne_lat">North east latitude of the box</param> /// <param name="sw_lng">South west longitude of the box</param> /// <param name="sw_lat">South west latitude of the box</param> /// <param name="gridWidth">Grid witdh</param> /// <param name="gridHeight">Grid height</param> /// <param name="grid">Clustering grid</param> /// <param name="clusteredPoints">Clustered points</param> /// <returns>Clustered point list</returns> private void CreateGridAndPutPoints(List <Point> points, int iconSize, double zoomLevel, double?ne_lng, double?ne_lat, double?sw_lng, double?sw_lat, out int gridWidth, out int gridHeight, out ClusterCentroid[][] grid, out List <Point> clusteredPoints) { // In order to get grid x and y coordinates, Point upperRightPoint = new Point() { Geometry = new Geometry() { Coordinates = new double[] { ne_lng.Value, ne_lat.Value } }, }; Point lowerLeftPoint = new Point() { Geometry = new Geometry() { Coordinates = new double[] { sw_lng.Value, sw_lat.Value } }, }; double upperRightX, upperRightY; upperRightPoint.Get2DCoordinates(zoomLevel, out upperRightX, out upperRightY); double lowerLeftX, lowerLeftY; lowerLeftPoint.Get2DCoordinates(zoomLevel, out lowerLeftX, out lowerLeftY); gridWidth = (int)(400 / iconSize); gridHeight = (int)(300 / iconSize); grid = new ClusterCentroid[gridHeight][]; for (int i = 0; i < gridHeight; i++) { grid[i] = new ClusterCentroid[gridWidth]; } clusteredPoints = new List <Point>(); foreach (var p in points) { int x, y; FindGridCell(grid, p, zoomLevel, upperRightX, upperRightY, lowerLeftX, lowerLeftY, iconSize, out x, out y); if (grid[y][x] == null) { p.Label = y + "-" + x; grid[y][x] = new ClusterCentroid(p); clusteredPoints.Add(grid[y][x]); } else { grid[y][x].AddPoint(p); } } }
/// <summary> /// Calculates Euclidean Distance distance between a point and a cluster centroid /// </summary> /// <param name="p">Point</param> /// <param name="c">Centroid</param> /// <returns>Calculated distance</returns> private double CalculateEuclideanDistance(ClusterPoint p, ClusterCentroid c) { return Math.Sqrt(Math.Pow(p.PixelColor.R - c.PixelColor.R, 2.0) + Math.Pow(p.PixelColor.G - c.PixelColor.G, 2.0) + Math.Pow(p.PixelColor.B - c.PixelColor.B, 2.0)); }