private void OnPaint(object sender, PaintEventArgs e) { e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; TPoint lastPoint = null; foreach (TPoint point in points) { if (point == selectedPoint) { e.Graphics.FillRectangle(Brushes.Red, point.X - 2, point.Y - 2, 5, 5); } else { e.Graphics.DrawRectangle(Pens.Black, point.X - 1, point.Y - 1, 2, 2); } if (clipEdgesBox.Checked && lastPoint != null) { e.Graphics.DrawLine(Pens.Black, lastPoint.X, lastPoint.Y, point.X, point.Y); } e.Graphics.DrawString(point.Index.ToString(), SystemFonts.DefaultFont, Brushes.Black, new PointF(point.X, point.Y)); lastPoint = point; } TPoint first = points.FirstOrDefault(); if (clipEdgesBox.Checked && first != null && lastPoint != null) { e.Graphics.DrawLine(Pens.Black, lastPoint.X, lastPoint.Y, first.X, first.Y); } if (tris != null) { foreach (Triangle t in tris) { DrawEdge(e.Graphics, t.Edge1); DrawEdge(e.Graphics, t.Edge2); DrawEdge(e.Graphics, t.Edge3); if (showCircleBox.Checked) { int radius2 = (int)t.Circumcircle.Radius * 2; Rectangle rect = new Rectangle( (int)(t.Circumcircle.Center.X - t.Circumcircle.Radius), (int)(t.Circumcircle.Center.Y - t.Circumcircle.Radius), radius2, radius2); e.Graphics.DrawEllipse(Pens.Tomato, rect); } } } if (!goButton.Enabled) { e.Graphics.DrawLine(Pens.LightBlue, curX, 0, curX, canvas.Height); } }
private void OnGoClick(object sender, EventArgs e) { logBox.Items.Clear(); goButton.Enabled = false; clearButton.Enabled = false; var triangulator = new Delaunay(points); triangulator.PointSelected += (o, pe) => { selectedPoint = pe.SelectedPoint; BeginInvoke(new Action(() => { curX = (int)pe.SelectedPoint.X; logBox.Items.Insert(0, String.Format( "Point {0} selected ({1:#},{2:#})", pe.SelectedPoint.Index, pe.SelectedPoint.X, pe.SelectedPoint.Y )); canvas.Invalidate(); })); Thread.Sleep(100); }; EventHandler <TrianglesModifiedArgs> triEvent = (o, te) => { tris = te.NewTriangles.ToList(); BeginInvoke(new Action(() => { logBox.Items.Insert(0, String.Format( "Triangles {0}: New count: {1}", te.EventType, te.NewTriangles.Count() )); canvas.Invalidate(); })); Thread.Sleep(100); }; triangulator.CreatedTriangles += triEvent; triangulator.RemovedTriangles += triEvent; EventHandler <EdgeArgs> edgeEvent = (o, ee) => { selectedEdge = ee.Edge; BeginInvoke(new Action(() => { logBox.Items.Insert(0, String.Format( "Edge {0} between {1} and {2}", ee.EventType, ee.Edge.First.Index, ee.Edge.Second.Index )); canvas.Invalidate(); })); Thread.Sleep(50); }; triangulator.EdgeAdded += edgeEvent; triangulator.EdgeRemoved += edgeEvent; ThreadPool.QueueUserWorkItem(o => { tris = triangulator.CalculateTriangles().ToList(); BeginInvoke(new Action(() => { selectedEdge = null; selectedPoint = null; goButton.Enabled = true; clearButton.Enabled = true; canvas.Invalidate(); })); }); }