Daniel Hamutel
Daniel Hamutel

Reputation: 653

How do I draw a line between all the drawn points?

In a paint event I did:

List<Point> drawPoints = GetDrawPoints();

if (drawPoints.Count > 1)
{
    foreach (Point p in drawPoints)
    {
        e.Graphics.DrawLine(pen, p.X - 2, p.Y - 2, 4, 4);
    }
}

But instead of drawing lines between the subsequential points, it's drawing two lines from same place to each point.

I want to connect all point with a single line.

Upvotes: 3

Views: 2509

Answers (2)

Mekap
Mekap

Reputation: 2085

You answered your own question in a way. To draw a line you need at the very least two points. But you keep drawing your news lines between the sames locations. (your variable point and a point located in (4;4)). You need to keep updating two points in your foreach and print them properly.

Better yet, use the DrawLines function, and give your list of points away. with something like :

 e.Graphics.DrawLines(pen, drawPoints.ToArray());

Upvotes: 2

Emond
Emond

Reputation: 50672

Use DrawLines and pass the List as an array:

if (drawPoints.Count > 1)
{
    e.Graphics.DrawLines(pen, drawPoints.ToArray());
}

Upvotes: 7

Related Questions