static void Run() { // Config var rect = new Rectangle { XMin = -200, XMax = 200, YMin = -100, YMax = 100, MaxDistance = 20, }; rect.Validate(); var conf = new KnnConfiguration { K = 100 }; // Random points IPoints points = new Points(); var rand = new Random(); for (var i = 0; i < 500000; i++) { var x = rect.XMin + rand.NextDouble() * rect.Width; var y = rect.YMin + rand.NextDouble() * rect.Height; points.Data.Add(new P { X = x, Y = y, }); } points.Round(3); // Init algo IAlgorithm algo = new Algorithm(points, rect, StrategyType.Grid); // Use algo var origin = new P { X = 0, Y = 0 }; var duration = algo.UpdateKnn(origin, conf); // Print result WL(string.Format("{0} msec. {1}:", algo.Strategy.Name, duration)); WL("K Nearest Neighbors:"); WL(string.Format("Origin: {0}", origin)); WL(string.Format("Distance sum: {0}", algo.Knn.GetDistanceSum())); algo.Knn.NNs.OrderBy(i => i.Distance).ToList().ForEach(WL); // Update strategy algo.SetAlgorithmStrategy(new NaiveStrategy()); // Use algo duration = algo.UpdateKnn(origin, conf); // Print result WL(string.Format("\n{0} msec. {1}:", algo.Strategy.Name, duration)); WL("K Nearest Neighbors:"); WL(string.Format("Distance sum: {0}", algo.Knn.GetDistanceSum())); algo.Knn.NNs.OrderBy(i => i.Distance).ToList().ForEach(WL); }
public static void DrawDots(DrawingContext dc, ICollection<IP> dots, Rectangle rect, ShapeType t = ShapeType.Default) { if (!IsDrawEnabled) return; foreach (var p in dots.Where(p => p != null)) { dc.DrawRectangle(GetColor(p, t), null, GetShape(p, rect, t)); } }
static void Run() { // Config var rect = new Rectangle { XMin = -300, XMax = 300, YMin = -200, YMax = 200, MaxDistance = 10, }; rect.Validate(); // Random points IPoints points = new Points(); var rand = new Random(); for (var i = 0; i < 5000; i++) { var x = rect.XMin + rand.NextDouble() * rect.Width; var y = rect.YMin + rand.NextDouble() * rect.Height; points.Data.Add(new P { X = x, Y = y, }); } points.Round(3); // Init algo IAlgorithm algo = new Algorithm(points, rect, StrategyType.Grid); // Use algo var duration = algo.UpdateSingles(); // Print result WL(string.Format("{0} msec. {1}:", algo.Strategy.Name, duration)); WL("Singles:\n"); algo.Singles.OrderBy(i => i.Uid).ToList().ForEach(WL); // Update strategy algo.SetAlgorithmStrategy(new NaiveStrategy()); // Use algo duration = algo.UpdateSingles(); // Print result WL(string.Format("\n{0} msec. {1}:", algo.Strategy.Name, duration)); WL("Singles:\n"); algo.Singles.OrderBy(i => i.Uid).ToList().ForEach(WL); }
public static void DrawGrid(DrawingContext dc, bool showGrid, Rectangle rect, Pen color = null) { if (!IsDrawEnabled) return; var pen = showGrid ? (color ?? Pens.PenGrid) : Pens.PenBackground; double dw = rect.Width / rect.XGrid; // delta w for (var i = 0; i <= rect.XGrid; i++) { dc.DrawLine(pen, new Point(M.Round(i * dw), 0), new Point(M.Round(i * dw), rect.Height)); } var dh = rect.Height / rect.YGrid; // delta h for (var i = 0; i <= rect.YGrid; i++) { dc.DrawLine(pen, new Point(0, M.Round(i * dh)), new Point(rect.Width, M.Round(i * dh))); } }
private static int PSize = 2; // default, must be int #endregion Fields #region Methods public static void ClearBackground(DrawingContext dc, Rectangle rect) { if (!IsDrawEnabled) return; dc.DrawRectangle(Pens.BackgroundColor, null, new System.Windows.Rect(0, 0, (int)rect.Width, (int)rect.Height)); }
static System.Windows.Rect GetShape(IP p, Rectangle rect, ShapeType st = ShapeType.Default) { switch (st) { case ShapeType.Selected: return new System.Windows.Rect((int)rect.XO + p.X - 1 - PSize / 2, (int)rect.YO + p.Y - 1 - PSize / 2, PSize + 2, PSize + 2); case ShapeType.NearestNeighbor: return new System.Windows.Rect((int)rect.XO + p.X - 1 - PSize / 2, (int)rect.YO + p.Y - 1 - PSize / 2, PSize + 2, PSize + 2); case ShapeType.Default: case ShapeType.Single: default: return new System.Windows.Rect((int)rect.XO + p.X - PSize / 2, (int)rect.YO + p.Y - PSize / 2, PSize, PSize); } }
public Animation(IAlgorithm algorithm, Rectangle rect) { _algo = algorithm; _rect = rect; Moving = new HashSet<IP>(); }
/// <summary> /// Run fast and slow version with same test data and display the running time /// </summary> static void Run() { // Config, test data showing ability to use negative positions var rect = new Rectangle { XMin = -200, XMax = 200, YMin = -100, YMax = 100, MaxDistance = 20, }; rect.Validate(); var conf = new KnnConfiguration { K = 100, SameTypeOnly = false, MaxDistance = null }; // Random points IPoints points = new Points(); var rand = new Random(); for (var i = 0; i < 500000; i++) { var x = rect.XMin + rand.NextDouble() * rect.Width; var y = rect.YMin + rand.NextDouble() * rect.Height; points.Data.Add(new P { X = x, Y = y, }); } points.Round(3); // Init algo IAlgorithm algo = new Algorithm(points, rect, StrategyType.Grid); // Use algo var origin = new P { X = 0, Y = 0 }; var duration = algo.UpdateKnn(origin, conf); // Print result WL(string.Format("{0} msec. {1}:", algo.Strategy.Name, duration)); WL("K Nearest Neighbors:"); WL(string.Format("Origin: {0}", origin)); WL(string.Format("Distance sum: {0}", algo.Knn.GetDistanceSum())); algo.Knn.NNs.OrderBy(i => i.Distance).ToList().ForEach(WL); // Update strategy algo.SetAlgorithmStrategy(new NaiveStrategy()); //algo.SetAlgorithmStrategy(new KdTreeStrategy(points.Data)); // Use algo duration = algo.UpdateKnn(origin, conf); // Print result WL(string.Format("\n{0} msec. {1}:", algo.Strategy.Name, duration)); WL("K Nearest Neighbors:"); WL(string.Format("Distance sum: {0}", algo.Knn.GetDistanceSum())); algo.Knn.NNs.OrderBy(i => i.Distance).ToList().ForEach(WL); }