15,879,844 members
See more:
I have drawn few lines using windows forms.

But I want to join lines by extending those line.

Is there any way to extend lines using windows forms

Here my code

C#
```for (int count = 0; count < 1; count++)
{
int x1 = polyPt[0].X;
int y1 = polyPt[0].Y;
int x2 = polyPt[polyPt.Length-1].X;
int y2 = polyPt[polyPt.Length-1].Y;

var L = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

var offsetPixels = -15.0;

int x1p = Convert.ToInt32(x1 + offsetPixels * (y2 - y1) / L);
int x2p = Convert.ToInt32(x2 + offsetPixels * (y2 - y1) / L);
int y1p = Convert.ToInt32(y1 + offsetPixels * (x1 - x2) / L);
int y2p = Convert.ToInt32(y2 + offsetPixels * (x1 - x2) / L);

graphics.DrawLine(new Pen(Color.Red), new Point(x1p, y1p), new Point(x2p, y2p));
}
```
Posted
Updated 28-Feb-14 0:27am
v2
agent_kruger 28-Feb-14 6:18am
sir, what code have you tried till now? please post it here.
KUMAR619 28-Feb-14 6:29am

## Solution 2

Your example code only shows one line - so you can't "extend" it to meet anything.
But if you do have two lines, it's pretty simple. All you need is the point at which the two meet, which is easy to calculate: http://social.msdn.microsoft.com/Forums/vstudio/en-US/4eb3423e-eb81-4977-8ce5-5a568d53fd9b/get-the-intersection-point-of-two-lines?forum=vbgeneral[^] - the code is in VB, but it's pretty obvious.

v2
KUMAR619 28-Feb-14 6:49am
Since I have used for loop I can draw n number of line but joining the line which do not meet is
quit tedious for me.

how to use that code which you've have linked
lukeer 28-Feb-14 7:04am
The linked code helps you to calculate the intersection point of two lines. You then no longer draw from your first point to the second, but from the previous intersection to the next.
OriginalGriff 28-Feb-14 7:16am
Sorry about that - posted to the wrong person!
OriginalGriff 28-Feb-14 7:16am
You would have to decide which pair of the various lines you want to extend - you can't assume that all lines will intersect at the same point, because the vast majority of sets of three lines do not do that, and it becomes increasingly unlikely the move lines you add.

What are you trying to do this for? There may be a better way.
KUMAR619 28-Feb-14 7:40am
To offset a polygon I was asked to draw parallel lines over the polygon.

So in order to make them meet i gotta write a code thats why I asked you to extend the lines so that all the lines may meet

## Solution 1

The easiest way is to use the DrawLines mehtod. This expects an array of points rather than individual points. The first two represent the first line segment. Successive points add line segments from the last point to the new point.

The following would draw a square:

C#
```Pen pen = new Pen(Color.Black, 3);

Point [] points = {
new Point(100, 100),
new Point(200, 100),
new Point(200, 200),
new Point(100, 200),
new Point(100, 100)
};

graphics.DrawLines(pen, points);```

That should be enough info to apply to your original problem.