Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Draw multiple Lines

Tags:

c#

draw

line

I'm drawing multiple lines by using this code, but I feel there is a better way of doing this.
E.g. by using a multidimensional array? or a list?

    private void drawLines()
    {
        int[] x1 = {   0,   0, 112, 222,   0, 333,   0,   1};
        int[] x2 = { 334, 334, 112, 222, 334, 333, 334,   1 };
        int[] y1 = { 100, 200, 300, 300,   1, 300, 300, 300 };
        int[] y2 = { 100, 200,   0,   0,   1,   0, 300,   0 };

        for (int i = 0; i < x1.Length; i++)
        {
            Line line = new Line();
            Grid myGrid = gg;
            line.Stroke = Brushes.Black;
            line.X1 = x1[i];
            line.X2 = x2[i];
            line.Y1 = y1[i];
            line.Y2 = y2[i];
            line.StrokeThickness = 2;
            myGrid.Children.Add(line);
        }

    }
like image 527
sadfiesch Avatar asked Mar 16 '23 20:03

sadfiesch


1 Answers

I would make a Line class having start and end point of the line in struct Point and make list of that class instead of having four arrays.

public class MyLine
{
     public Point StartPoint {get; set;}
     public Point EndPoint {get; set;}

     public void DrawLine()
     {
         //Draw line code goes here
     }
}

Now you have line class with required field and method to draw line. You drawLines method that might be in some other class will create list of MyLine class and can draw that list of Lines using Line class method DrawLine

private void DrawLines()
{
    List<MyLine> listMyLines = new  List<MyLine>();
    listMyLines.Add(new MyLine{StartPoint = new Point(0, 100), EndPoint = new Point(334, 100)});       

    for (int i = 0; i < listMyLines.Count; i++)
    {
         listMyLines[i].DrawLine();
    }
}
like image 169
Adil Avatar answered Mar 18 '23 10:03

Adil