private void DelaunaySetup(Point[] points) { triangles = null; triangle2d_delaunay_position = 0; Del_Point2d[] del_points = new Del_Point2d[points.Length]; for (int loop = 0; loop < points.Length; loop++) { del_points[loop] = new Del_Point2d(); Point pt = points[loop]; del_points[loop].x = (double)(pt.X); del_points[loop].y = (double)(pt.Y); } //Sort points and drop matching coordinates Array.Sort(del_points); int num_points = points.Length; int data_shift = 0; int deduplicate_loop; for (deduplicate_loop = 0; deduplicate_loop < num_points - 1 - data_shift; deduplicate_loop++) { if (data_shift > 0) { del_points[deduplicate_loop] = del_points[deduplicate_loop + data_shift]; } //Use while loop to check for multiple point copies. while (deduplicate_loop < num_points - 1 - data_shift) { if (del_points[deduplicate_loop].CompareTo(del_points[deduplicate_loop + 1 + data_shift]) == 0) { data_shift++; } else { break; } } if (deduplicate_loop >= num_points - 1 - data_shift) //Preserve variable { break; } } if (data_shift > 0) { del_points[deduplicate_loop] = del_points[deduplicate_loop + data_shift]; del_points[deduplicate_loop + 1] = null; } num_points -= data_shift; Delaunay2d delaunay2d = delaunay2d_from(del_points, num_points); if (num_points >= 3) { triangles = tri_delaunay2d_from(delaunay2d); } }
private tri_delaunay2d tri_delaunay2d_from(Delaunay2d del) { int v_offset = del.faces[0] + 1; /* ignore external face */ int dst_offset = 0; int i; tri_delaunay2d tdel = new tri_delaunay2d(); tdel.num_triangles = 0; /* count the number of triangles */ if (1 == del.num_faces) { /* degenerate case: only external face exists */ int nv = del.faces[0]; tdel.num_triangles += nv - 2; } else { for (i = 1; i < del.num_faces; ++i) { int nv = del.faces[v_offset]; tdel.num_triangles += nv - 2; v_offset += nv + 1; } } /* copy points */ tdel.num_points = del.num_points; tdel.points = new Del_Point2d[del.num_points]; Debug.Assert(null != tdel.points); for (int loop = 0; loop < del.num_points; loop++) { tdel.points[loop] = (Del_Point2d)del.points[loop].Clone(); } /* build the triangles */ tdel.tris = new int[3 * tdel.num_triangles]; Debug.Assert(null != tdel.tris); v_offset = del.faces[0] + 1; /* ignore external face */ if (1 == del.num_faces) { /* handle the degenerated case where only the external face exists */ int nv = del.faces[0]; int j = 0; v_offset = 1; for (; j < nv - 2; ++j) { tdel.tris[dst_offset] = del.faces[v_offset + j]; tdel.tris[dst_offset + 1] = del.faces[(v_offset + j + 1) % nv]; tdel.tris[dst_offset + 2] = del.faces[v_offset + j]; dst_offset += 3; } } else { for (i = 1; i < del.num_faces; ++i) { int nv = del.faces[v_offset]; int j = 0; int first = del.faces[v_offset + 1]; for (; j < nv - 2; ++j) { tdel.tris[dst_offset] = first; tdel.tris[dst_offset + 1] = del.faces[v_offset + j + 2]; tdel.tris[dst_offset + 2] = del.faces[v_offset + j + 3]; dst_offset += 3; } v_offset += nv + 1; } } return(tdel); }