PowerArgs.Cli.ConsoleBitmap.DrawLine C# (CSharp) Method

DrawLine() public method

public DrawLine ( int x1, int y1, int x2, int y2 ) : void
x1 int
y1 int
x2 int
y2 int
return void
        public void DrawLine(int x1, int y1, int x2, int y2)
        {
            x1 = scope.X + x1;
            y1 = scope.Y + y1;

            x2 = scope.X + x2;
            y2 = scope.Y + y2;

            if (x1 == x2)
            {
                int yMin = y1 >= y2 ? y2 : y1;
                int yMax = y1 >= y2 ? y1 : y2;
                for (int y = yMin; y < yMax; y++)
                {
                    if (IsInScope(x1, y))
                    {
                        pixels[x1][y].Value = Pen;
                    }
                }
            }
            else if (y1 == y2)
            {
                int xMin = x1 >= x2 ? x2 : x1;
                int xMax = x1 >= x2 ? x1 : x2;
                for (int x = xMin; x < xMax; x++)
                {
                    if (IsInScope(x, y1))
                    {
                        pixels[x][y1].Value = Pen;
                    }
                }
                return;
            }
            else
            {
                double slope = ((double)y2 - y1) / ((double)x2 - x1);

                int dx = Math.Abs(x1 - x2);
                int dy = Math.Abs(y1 - y2);

                if (dy > dx)
                {
                    for (double x = x1; x < x2; x += DrawPrecision)
                    {
                        double y = slope + (x - x1) + y1;
                        int xInt = (int)x;
                        int yInt = (int)y;
                        if (IsInScope(xInt, yInt))
                        {
                            pixels[xInt][yInt].Value = Pen;
                        }
                    }

                    for (double x = x2; x < x1; x += DrawPrecision)
                    {
                        double y = slope + (x - x1) + y1;
                        int xInt = (int)x;
                        int yInt = (int)y;
                        if (IsInScope(xInt, yInt))
                        {
                            pixels[xInt][yInt].Value = Pen;
                        }
                    }
                }
                else
                {
                    for (double y = y1; y < y2; y += DrawPrecision)
                    {
                        double x = ((y - y1) / slope) + x1;
                        int xInt = (int)x;
                        int yInt = (int)y;
                        if (IsInScope(xInt, yInt))
                        {
                            pixels[xInt][yInt].Value = Pen;
                        }
                    }

                    for (double y = y2; y < y1; y += DrawPrecision)
                    {
                        double x = ((y - y1) / slope) + x1;
                        int xInt = (int)x;
                        int yInt = (int)y;
                        if (IsInScope(xInt, yInt))
                        {
                            pixels[xInt][yInt].Value = Pen;
                        }
                    }
                }
            }
        }

Usage Example

Example #1
0
 protected override void OnPaint(ConsoleBitmap context)
 {
     context.Pen = new ConsoleCharacter(' ', null, myFocusStackDepth == Application.FocusManager.StackDepth ? Theme.DefaultTheme.H1Color : Theme.DefaultTheme.DisabledColor);
     context.DrawLine(0, 0, Width, 0);
     context.DrawLine(0, Height - 1, Width, Height - 1);
     base.OnPaint(context);
 }
All Usage Examples Of PowerArgs.Cli.ConsoleBitmap::DrawLine