public static void SetPixel(this BitmapData bitmapData, int x, int y, Color color)
        {
            if (x < 0 || x >= bitmapData.Width ||
                y < 0 || y >= bitmapData.Height)
            {
                return;
            }

            unsafe
            {
                int bytesPerPixel = System.Drawing.Bitmap.GetPixelFormatSize(PixelFormat.Format32bppArgb) >> 3;
                int widthInBytes  = bitmapData.Width * bytesPerPixel;

                y = bitmapData.Height - y - 1;

                byte *ptrFirstPixel = (byte *)bitmapData.Scan0;

                byte *row = ptrFirstPixel + (y * bitmapData.Stride);
                int   col = x * bytesPerPixel;

                row[col + 2] = color.R;
                row[col + 1] = color.G;
                row[col]     = color.B;
            }
        }
 public Circle(int x_center, int y_center, int r, Color color)
 {
     X_Center = x_center;
     Y_Center = y_center;
     R        = r;
     Color    = color;
 }
        /// <summary>
        /// Draws anti-aliased circle using Fast Antialiased Line Generation — Xiaolin Wu.
        /// </summary>
        public override void AntiAliasingRender(BitmapData bitmapData)
        {
            byte lineColor = 0;
            byte bgColor   = 255;

            int x_ceil = R;
            int y      = 0;

            while (x_ceil > y)
            {
                double x_real = Math.Sqrt(R * R - y * y);
                x_ceil = (int)Math.Ceiling(x_real);

                double error = Error(x_ceil, x_real);

                byte color_r = (byte)(lineColor * (1 - error) + bgColor * error);
                byte color_l = (byte)(lineColor * error + bgColor * (1 - error));

                var colorR = new Color()
                {
                    R = color_r, G = color_r, B = color_r
                };
                var colorL = new Color()
                {
                    R = color_l, G = color_l, B = color_l
                };

                bitmapData.SetPixel(x_ceil + X_Center, y + Y_Center, colorR);
                bitmapData.SetPixel(x_ceil - 1 + X_Center, y + Y_Center, colorL);

                bitmapData.SetPixel(y + X_Center, x_ceil + Y_Center, colorR);
                bitmapData.SetPixel(y + X_Center, x_ceil + Y_Center - 1, colorL);

                bitmapData.SetPixel(y + X_Center, -x_ceil + Y_Center, colorR);
                bitmapData.SetPixel(y + X_Center, -x_ceil + Y_Center + 1, colorL);

                bitmapData.SetPixel(x_ceil + X_Center, -y + Y_Center, colorR);
                bitmapData.SetPixel(x_ceil - 1 + X_Center, -y + Y_Center, colorL);

                bitmapData.SetPixel(-x_ceil + X_Center, -y + Y_Center, colorR);
                bitmapData.SetPixel(-x_ceil + 1 + X_Center, -y + Y_Center, colorL);

                bitmapData.SetPixel(-y + X_Center, -x_ceil + Y_Center, colorR);
                bitmapData.SetPixel(-y + X_Center, -x_ceil + Y_Center + 1, colorL);

                bitmapData.SetPixel(-y + X_Center, x_ceil + Y_Center, colorR);
                bitmapData.SetPixel(-y + X_Center, x_ceil + Y_Center - 1, colorL);

                bitmapData.SetPixel(-x_ceil + X_Center, y + Y_Center, colorR);
                bitmapData.SetPixel(-x_ceil + 1 + X_Center, y + Y_Center, colorL);

                y++;
            }
        }
Exemple #4
0
        public Line(double x0, double y0, double x1, double y1, Color color, int thickness = 5)
        {
            this.x0 = x0;
            this.y0 = y0;
            this.x1 = x1;
            this.y1 = y1;

            this.thickness = thickness;

            Color = color;
        }