Пример #1
0
        public static Color FromHsv(Angle h, double s, double v, int a)
        {
            var H = h.Degrees / 60;

            s = GMath.Median(0, s, 1);
            v = GMath.Median(0, v, 1);
            var C = v * s;
            var X = C * (1 - GMath.Abs(H % 2 - 1));
            var m = v - C;

            double R, G, B;

            switch ((int)H)
            {
            case 0: R = C; G = X; B = 0; break;

            case 1: R = X; G = C; B = 0; break;

            case 2: R = 0; G = C; B = X; break;

            case 3: R = 0; G = X; B = C; break;

            case 4: R = X; G = 0; B = C; break;

            default: R = C; G = 0; B = X; break;
            }

            return(FromRgba((int)(255 * (R + m)), (int)(255 * (G + m)), (int)(255 * (B + m)), a));
        }
Пример #2
0
        public static Polygon Ellipse(Point center, double xRadius, double yRadius)
        {
            int precision = (int)GMath.Ceiling(GMath.Pi * GMath.Abs(xRadius + yRadius));

            if (precision <= 0)
            {
                return(new Polygon(new[] { center }));
            }
            double dt = GMath.Tau / precision;
            double c = GMath.Cos(dt), s = GMath.Sin(dt);

            double x = 1, y = 0, tmp;

            var pts = new Point[precision];

            for (int i = 0; i < precision; i++)
            {
                pts[i] = new Point(center.X + x * xRadius, center.Y + y * yRadius);

                tmp = x;
                x   = c * x - s * y;
                y   = s * tmp + c * y;
            }

            return(new Polygon(pts));
        }
Пример #3
0
        public static Polygon Circle(Point center, double radius)
        {
            if (radius == 0)
            {
                return(new Polygon(new[] { center }));
            }

            int precision = (int)GMath.Ceiling(GMath.Tau * GMath.Abs(radius));

            if (precision < 2)
            {
                precision = 2;
            }

            return(Regular(precision, radius, center));
        }
Пример #4
0
        public Polygon(IEnumerable <Point> pts)
        {
            _pts = pts.ToArray();

            if (_pts.Length <= 2)
            {
                return;
            }

            double sum = 0;
            Angle  a;
            Vector previous, next;

            previous = Edge(-1).Offset;
            next     = Edge(0).Offset;

            a = next.Direction - previous.Direction;
            if (a.Degrees > 180)
            {
                Array.Reverse(_pts);
                previous = Edge(-1).Offset;
                next     = Edge(0).Offset;
                a        = next.Direction - previous.Direction;
            }
            sum += a.Degrees;

            for (int i = 1; i < _pts.Length; i++)
            {
                previous = next;
                next     = Edge(i).Offset;
                a        = next.Direction - previous.Direction;
                if (a.Degrees > 180)
                {
                    throw new ArgumentException("The points must specify a convex polygon.");
                }
                sum += a.Degrees;
            }

            if (GMath.Abs(sum - 360) > GMath.DefaultDelta)
            {
                throw new ArgumentException($"The points must specify a convex polygon with winding number equal to 1. (Winding is {sum} degrees)");
            }
        }