namespace TriangleNet { using System; using System.Collections.Generic; using TriangleNet.Geometry; static class Generate { private static readonly Random random = new Random(63841); public static List RandomPoints(int n, Rectangle bounds) { var points = new List(n); var xmin = bounds.Left; var ymin = bounds.Bottom; var width = bounds.Width; var height = bounds.Height; for (int i = 0; i < n; i++) { double x = random.NextDouble(); double y = random.NextDouble(); points.Add(new Vertex(xmin + x * width, ymin + y * height)); } return points; } /// /// Creates a rectangle contour. /// public static Contour Rectangle(Rectangle rect, double size = 0d, int label = 0) { return Rectangle(rect.X, rect.Y, rect.Width, rect.Height, size, label); } /// /// Creates a rectangle contour. /// public static Contour Rectangle(double x, double y, double width, double height, double size = 0d, int label = 0) { // Horizontal and vertical step sizes. double stepH = 0d; double stepV = 0d; int nH = 1; int nV = 1; if (size > 0d) { size = Math.Min(size, Math.Min(width, height)); nH = (int)Math.Ceiling(width / size); nV = (int)Math.Ceiling(height / size); stepH = width / nH; stepV = height / nV; } var points = new List(2 * nH + 2 * nV); double right = x + width; double top = y + height; // Left box boundary points for (int i = 0; i < nV; i++) { points.Add(new Vertex(x, y + i * stepV, label)); } // Top box boundary points for (int i = 0; i < nH; i++) { points.Add(new Vertex(x + i * stepH, top, label)); } // Right box boundary points for (int i = 0; i < nV; i++) { points.Add(new Vertex(right, top - i * stepV, label)); } // Bottom box boundary points for (int i = 0; i < nH; i++) { points.Add(new Vertex(right - i * stepH, y, label)); } return new Contour(points, label, true); } /// /// Create a circular contour. /// /// The radius. /// The center point. /// The desired segment length. /// The boundary label. /// A circular contour. public static Contour Circle(double r, Point center, double h, int label = 0) { int n = (int)(2 * Math.PI * r / h); var points = new List(n); double x, y, dphi = 2 * Math.PI / n; for (int i = 0; i < n; i++) { x = center.X + r * Math.Cos(i * dphi); y = center.Y + r * Math.Sin(i * dphi); points.Add(new Vertex(x, y, label)); } return new Contour(points, label, true); } } }