0

How can I draw a line and C# convert it into a straight line? panel A its my paint, and panel b its result

I want the program to understand that I have drawn a line and then convert it to a straight line in panel B. Or if I draw a square, it becomes a perfect square

like this

Code

public partial class frmPaint : Form
    {
        public Point x = new();
        public Point y = new();
        public Pen penA = new(Color.Red, 2);
        public Pen Eraser = new(Color.White, 10);
        public Graphics graphics;
        public frmPaint()
        {
            InitializeComponent();
            graphics = panelDraw.CreateGraphics();
        }

        private void panelDraw_MouseDown(object sender, MouseEventArgs e)
        {
            y = e.Location;

            if (rbLineWidth5.Checked)
                penA.Width = 5;
            if (rbLineWidth10.Checked)
                penA.Width = 10;
            if (rbLineWidth15.Checked)
                penA.Width = 15;
        }

        private void panelDraw_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = e.Location;
                graphics.DrawLine(penA, x, y);
                y = e.Location;
            }

            if (e.Button == MouseButtons.Right)
            {
                x = e.Location;
                graphics.DrawLine(Eraser, x, y);
                y = e.Location;
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new();
            if (colorDialog.ShowDialog() == DialogResult.OK)
                penA.Color = colorDialog.Color;
        }
    }
Mardibak
  • 61
  • 1
  • 7
  • https://stackoverflow.com/a/16154369/14171304 Or just select a drawing tool? Line, Rectangle, Circle ....etc. So when you draw a rectangle for example, take the minimum and maximum `x` and `y` values to draw the rectangle on the second panel? – dr.null Apr 23 '23 at 16:45

1 Answers1

1

This code uses the MouseLeave event to determine the end of the drawing, it also has a sizeDiff variable to determine the distance between the point that started to be drawn and the end point, thus determining whether it is a square or a line. After determining, if it is a line, it draws a straight line between the initial point and the final point, if it is a square, a square is drawn that starts at the lowest point of the drawing and extends to the highest point of the drawing.

Hope this helps, let me know if you have any questions.

    bool drawing = false; // Drawing Var

    Point xGlobal = new Point(); 
    Point yGlobal = new Point();

    int sizeDiff = 50; //Var pixels to difference square to line  

private void panelDraw_MouseUp(object sender, MouseEventArgs e)
    {
        xGlobal = e.Location; // X Point for draw in the MouseLeave

        int XDiff = (yGlobal.X - xGlobal.X); // Difference start point and end point int width
        int YDiff = (yGlobal.Y - xGlobal.Y); // Difference start point and end point int height

        // Convert difference to positive integer
        if (XDiff < 0)
        {
            XDiff = XDiff * -1;
        }
        if (YDiff < 0)
        {
            YDiff = YDiff * -1;
        }

        bool isSquare = false; // isSquare Var

        // Verify start point and end point 
        if (XDiff < sizeDiff)
        {
            if (YDiff < sizeDiff)
            {
                isSquare = true; // defines whether it is a square or not based on the start and end point
            }
        }

        if (drawing == true)
        {
            if (isSquare)
            {
                //Draw Square in Panel B
                graphicsPanelB.DrawRectangle(penA, new Rectangle(upPoint, new Size(downPoint.X - upPoint.X, downPoint.Y - upPoint.Y))); //Graphics panel B
            } else
            {
                //Draw Line in Panel B
                graphicsPanelB.DrawLine(penA, xGlobal, yGlobal); //Graphics panel B
            }
            

            drawing = false;
        }

    }

    Point upPoint = new Point(); //Point A for Square
    Point downPoint = new Point(); //Point B for Square


    private void panelDraw_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            drawing = true; //On drawing var to MouseLeave event
        }
        y = e.Location;
        yGlobal = y; // Y Point for draw in the MouseLeave
        upPoint = y; //Reference point for Square
        downPoint = y; //Reference point for Square

        if (rbLineWidth5.Checked)
            penA.Width = 5;
        if (rbLineWidth10.Checked)
            penA.Width = 10;
        if (rbLineWidth15.Checked)
            penA.Width = 15;
    }

    private void panelDraw_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            x = e.Location;
            graphics.DrawLine(penA, x, y);
            y = e.Location;

            //Verify to decrease upPoint
            if (x.X < upPoint.X)
            {
                upPoint.X = x.X;
            }
            if (x.Y < upPoint.Y)
            {
                upPoint.Y = x.Y;
            }

            //Verify to increment downPoint
            if (x.X > downPoint.X)
            {
                downPoint.X = x.X;
            }
            if (x.Y > downPoint.Y)
            {
                downPoint.Y = x.Y;
            }
        }

        if (e.Button == MouseButtons.Right)
        {
            x = e.Location;
            graphics.DrawLine(Eraser, x, y);
            y = e.Location;
        }
    }
  • thank you, what can i do for Triangle and circle? – Mardibak Apr 24 '23 at 10:19
  • 1
    try to create a list of points, and add a keyboard click event and at each click it adds the Y point to the list of points, in the mouse Down event add a starting point to the list and in the mouse leave add another point. After the mouse leave event, differentiate the figure using the list of points for example 4 points = triangle, 5 points = square, 2 points line or circle. which will be defined by the current variable isSquare. then just draw the figure. – Arthur Correa Apr 24 '23 at 16:24
  • I was not successful in implementing the code to produce other shapes, can you help me more? – Mardibak Apr 25 '23 at 22:18
  • 1
    Yes I can, follow me here: https://github.com/Correa0/WinFormPainter – Arthur Correa Apr 25 '23 at 23:20