public bool IsPointInsideCircumcircle(DelaunayPoint point) { var d_squared = (point.X - Circumcenter.X) * (point.X - Circumcenter.X) + (point.Y - Circumcenter.Y) * (point.Y - Circumcenter.Y); return(d_squared < RadiusSquared); }
private bool IsCounterClockwise(DelaunayPoint point1, DelaunayPoint point2, DelaunayPoint point3) { var result = (point2.X - point1.X) * (point3.Y - point1.Y) - (point3.X - point1.X) * (point2.Y - point1.Y); return(result > 0); }
private void UpdateCircumcircle() { // https://codefound.wordpress.com/2013/02/21/how-to-compute-a-circumcircle/#more-58 // https://en.wikipedia.org/wiki/Circumscribed_circle var p0 = Vertices[0]; var p1 = Vertices[1]; var p2 = Vertices[2]; var dA = p0.X * p0.X + p0.Y * p0.Y; var dB = p1.X * p1.X + p1.Y * p1.Y; var dC = p2.X * p2.X + p2.Y * p2.Y; var aux1 = (dA * (p2.Y - p1.Y) + dB * (p0.Y - p2.Y) + dC * (p1.Y - p0.Y)); var aux2 = -(dA * (p2.X - p1.X) + dB * (p0.X - p2.X) + dC * (p1.X - p0.X)); var div = (2 * (p0.X * (p2.Y - p1.Y) + p1.X * (p0.Y - p2.Y) + p2.X * (p1.Y - p0.Y))); if (div == 0) { throw new System.Exception(); } var center = new DelaunayPoint(aux1 / div, aux2 / div); Circumcenter = center; RadiusSquared = (center.X - p0.X) * (center.X - p0.X) + (center.Y - p0.Y) * (center.Y - p0.Y); }
public Triangle(DelaunayPoint point1, DelaunayPoint point2, DelaunayPoint point3) { if (!IsCounterClockwise(point1, point2, point3)) { Vertices[0] = point1; Vertices[1] = point3; Vertices[2] = point2; } else { Vertices[0] = point1; Vertices[1] = point2; Vertices[2] = point3; } Vertices[0].AdjacentTriangles.Add(this); Vertices[1].AdjacentTriangles.Add(this); Vertices[2].AdjacentTriangles.Add(this); UpdateCircumcircle(); }
public Edge(DelaunayPoint point1, DelaunayPoint point2) { Point1 = point1; Point2 = point2; }
//private Triangle GenerateSupraTriangle() //{ // // 1 -> maxX // // / \ // // 2---3 // // | // // v maxY // var margin = 500; // var point1 = new DelaunayPoint(0.5 * MaxX, -2 * MaxX - margin); // var point2 = new DelaunayPoint(-2 * MaxY - margin, 2 * MaxY + margin); // var point3 = new DelaunayPoint(2 * MaxX + MaxY + margin, 2 * MaxY + margin); // return new Triangle(point1, point2, point3); //} private ISet <Triangle> FindBadTriangles(DelaunayPoint point, HashSet <Triangle> triangles) { var badTriangles = triangles.Where(o => o.IsPointInsideCircumcircle(point)); return(new HashSet <Triangle>(badTriangles)); }
//----------------------- private void openFileToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { pictureBox1.Load(openFileDialog1.FileName); bmp = (Bitmap)pictureBox1.Image.Clone(); width = bmp.Width; height = bmp.Height; Polys1.init(ref bmp); MakePoints Makepoints1 = new MakePoints(ref bmp); Makepoints1.doit(); var point0 = new DelaunayPoint(0, 0); var point1 = new DelaunayPoint(0, height - 1); var point2 = new DelaunayPoint(width - 1, height - 1); var point3 = new DelaunayPoint(width - 1, 0); var points = new List <DelaunayPoint>() { point0, point1, point2, point3 }; var tri1 = new Triangle(point0, point1, point2); var tri2 = new Triangle(point0, point2, point3); var border = new List <Triangle>() { tri1, tri2 }; DelaunayTriangulator delaunay = new DelaunayTriangulator(); foreach (Point pnt in Makepoints1.PointsLst) { bmp.SetPixel(pnt.X, pnt.Y, Color.Black); points.Add(new DelaunayPoint(pnt.X + 0.2, pnt.Y + 0.1)); } IEnumerable <Triangle> tmp = delaunay.BowyerWatson(points, border); Graphics grp = pictureBox1.CreateGraphics(); List <Point> pntlist = new List <Point>(); List <string> strlist = new List <string>(); foreach (var triangle in tmp) { string key = triangle.Vertices[0].Key(); if (!strlist.Contains(key)) { strlist.Add(key); pntlist.Add(triangle.Vertices[0].GetPoint()); } key = triangle.Vertices[1].Key(); if (!strlist.Contains(key)) { strlist.Add(key); pntlist.Add(triangle.Vertices[1].GetPoint()); } key = triangle.Vertices[2].Key(); if (!strlist.Contains(key)) { strlist.Add(key); pntlist.Add(triangle.Vertices[2].GetPoint()); } } Polys1.PointsLst.Clear(); Polys1.PointsLst_cpy.Clear(); Polys1.Items.Clear(); foreach (Point pnt in pntlist) { Polys1.PointsLst.Add(pnt); Polys1.PointsLst_cpy.Add(pnt); } foreach (var triangle in tmp) { string key1 = triangle.Vertices[0].Key(); string key2 = triangle.Vertices[1].Key(); string key3 = triangle.Vertices[2].Key(); strlist.IndexOf(key1); Poly pol = new Poly(strlist.IndexOf(key1), strlist.IndexOf(key2), strlist.IndexOf(key3), Polys1.PointsLst); if (pol.IsCounterClockwise) { int kk = pol.p2; pol.p2 = pol.p1; pol.p1 = kk; } Polys1.Items.Add(pol); } binImg = new byte[width * 3 * height]; refreshtriagles(false); BuStart.Enabled = true; BuEnd.Enabled = true; } }