示例#1
0
        public static Circle[] GeneratePoints(int Count, int w, int h, double rad = 0)
        {
            var points = new Point[Count];
            var radiuses = new double[Count];

            for (int i = 0; i < Count; i++)
                radiuses[i] = rad != 0 ? rad : seed.NextDouble(250.0 / Math.Pow(Count, 0.5), 250.0 / Math.Pow(Count, 0.25));

            for (int i = 0; i < Count; i++)
                points[i] = new Point(seed.NextDouble(radiuses[i], w - radiuses[i]), seed.NextDouble(radiuses[i], h - radiuses[i]));

            int iter = 0;
            while (points.Intersections(radiuses) != 0)
            {
                if (iter++ > 100000) return GeneratePoints(Count, w, h, 100);
                for (int i = 0; i < points.Length; i++)
                    for (int j = i + 1; j < points.Length; j++)
                    {
                        var d = points[i].DistanceTo(points[j]);
                        if (d > radiuses[i] + radiuses[j] + 30) continue;

                        points[i] = points[i].Move(points[i].To(points[j]), -5 / (d * d));
                        points[j] = points[j].Move(points[j].To(points[i]), -5 / (d * d));

                        if (points[i].X < radiuses[i]) points[i].X = radiuses[i];
                        if (points[i].Y < radiuses[i]) points[i].Y = radiuses[i];
                        if (points[i].X > w - radiuses[i]) points[i].X = w - radiuses[i];
                        if (points[i].Y > h - radiuses[i]) points[i].Y = h - radiuses[i];

                        if (points[j].X < radiuses[j]) points[j].X = radiuses[j];
                        if (points[j].Y < radiuses[j]) points[j].Y = radiuses[j];
                        if (points[j].X > w - radiuses[j]) points[j].X = w - radiuses[j];
                        if (points[j].Y > h - radiuses[j]) points[j].Y = h - radiuses[j];

                    }
            }

            var crc = new Circle[Count];
            for (int i = 0; i < Count; i++)
            {
                crc[i].X = points[i].X;
                crc[i].Y = points[i].Y;
                crc[i].Radius = radiuses[i];
            }
            return crc;
        }