locked
C# CODE ABOUT DRAW LINES RRS feed

  • Question

  • hi

    i want to make a windows form c# program

    please if u can help me to write code

    in my software there are 2 lines (first line between A & B points  and second line between C & D points)

    same image 1

    i need a code that

    software should automatically find (coordinate of) center point (between A & C point) (for example point of (E))

    and   find (coordinate of) center point (between B & D point) (for example point of (F))

    and automatically draw a line between that new points (E & F)

    (same image 2)

    and 

    software should automatically find (coordinate of) center point (between A & E point) (for example point of (G))

    and   find (coordinate of) center point (between B & F point) (for example point of (H))

    and automatically draw a line between that new points (G & H)

    (same image 3)

    now there are 2 new lines

    i need that software repeat this work to make 10 new lines

    this time new line between (A&G   AND  B&H) AND...

    THANKS PLEASE HELP ME 


    • Edited by PRLED Saturday, July 18, 2020 3:58 PM
    Saturday, July 18, 2020 3:55 PM

Answers

  • Hi PRLED,

    I wrote a simple example.

    I added the initial two lines to panel1_Paint.

    In button1_Click, I call the two lines the start line and the end line.

    According to the coordinates of their two ends, I get the coordinates of the two ends of the line at their center, and then set this new line as the end line, and then perform the same operation until the capacity of the saved line list reaches the target number you set.

    To make it easier to observe, I let the thread sleep for one second in each loop.

            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                panel1.BackColor = Color.LightGray;
            }
            List<Line> lines = new List<Line>();
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                Line line1 = new Line() { StartPoint = new Point(10, 20) ,EndPoint = new Point(30, 250) };
                Line line2 = new Line() { StartPoint = new Point(300, 5), EndPoint = new Point(350, 300) };
    
                lines.Add(line1);
                lines.Add(line2);
                var g = e.Graphics;
                g.DrawLine(new Pen(Color.Red), line1.StartPoint, line1.EndPoint);
                g.DrawLine(new Pen(Color.Blue), line2.StartPoint, line2.EndPoint);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Line startLine = lines[0];
                Line endLine = lines[1];
    
                while (lines.Count < 10)
                {
                    Point centerLineStart = new Point((startLine.StartPoint.X+endLine.StartPoint.X)/2, (startLine.StartPoint.Y+endLine.StartPoint.Y)/2);
                    Point centerLineEnd = new Point((startLine.EndPoint.X+endLine.EndPoint.X)/2, (startLine.EndPoint.Y+endLine.EndPoint.Y)/2);
                    
                    Line center = new Line() { StartPoint = centerLineStart, EndPoint = centerLineEnd };
                    
                    panel1.CreateGraphics().DrawLine(new Pen(Color.Red), center.StartPoint, center.EndPoint);
                    lines.Add(center);
                    endLine = center;
    
                    Thread.Sleep(1000);
                }
            }
        }
        class Line
        {
            public Point StartPoint { get; set; }
            public Point EndPoint { get; set; }
        }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 23, 2020 8:36 AM

All replies

  • Check these formulae:

       xE = (xA + xC) / 2

       yE = (yA + yC) / 2

    where (xA, yA) — the coordinates of A, etc.


    Saturday, July 18, 2020 5:44 PM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 22, 2020 7:38 AM
  • hi

    thanks

    i  could not solve problem

    i need to complete code

    the reply was only a math formula

    this was good

     xE = (xA + xC) / 2

       yE = (yA + yC) / 2

    but i need to a complete c# code for this

    please if you can write a code for me

    thanks


    • Edited by PRLED Wednesday, July 22, 2020 3:42 PM
    Wednesday, July 22, 2020 3:40 PM
  • Hi PRLED,

    I wrote a simple example.

    I added the initial two lines to panel1_Paint.

    In button1_Click, I call the two lines the start line and the end line.

    According to the coordinates of their two ends, I get the coordinates of the two ends of the line at their center, and then set this new line as the end line, and then perform the same operation until the capacity of the saved line list reaches the target number you set.

    To make it easier to observe, I let the thread sleep for one second in each loop.

            public Form1()
            {
                InitializeComponent();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                panel1.BackColor = Color.LightGray;
            }
            List<Line> lines = new List<Line>();
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
                Line line1 = new Line() { StartPoint = new Point(10, 20) ,EndPoint = new Point(30, 250) };
                Line line2 = new Line() { StartPoint = new Point(300, 5), EndPoint = new Point(350, 300) };
    
                lines.Add(line1);
                lines.Add(line2);
                var g = e.Graphics;
                g.DrawLine(new Pen(Color.Red), line1.StartPoint, line1.EndPoint);
                g.DrawLine(new Pen(Color.Blue), line2.StartPoint, line2.EndPoint);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Line startLine = lines[0];
                Line endLine = lines[1];
    
                while (lines.Count < 10)
                {
                    Point centerLineStart = new Point((startLine.StartPoint.X+endLine.StartPoint.X)/2, (startLine.StartPoint.Y+endLine.StartPoint.Y)/2);
                    Point centerLineEnd = new Point((startLine.EndPoint.X+endLine.EndPoint.X)/2, (startLine.EndPoint.Y+endLine.EndPoint.Y)/2);
                    
                    Line center = new Line() { StartPoint = centerLineStart, EndPoint = centerLineEnd };
                    
                    panel1.CreateGraphics().DrawLine(new Pen(Color.Red), center.StartPoint, center.EndPoint);
                    lines.Add(center);
                    endLine = center;
    
                    Thread.Sleep(1000);
                }
            }
        }
        class Line
        {
            public Point StartPoint { get; set; }
            public Point EndPoint { get; set; }
        }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 23, 2020 8:36 AM
  • thanks .solved
    Thursday, July 23, 2020 10:44 AM
  • Hi PELED,

    Since your question has been resolved, please click "Mark as Answer" under the correct response so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 24, 2020 1:17 AM