Click here to Skip to main content
15,122,003 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
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 1:27am
v2
Comments
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
   
I have added my code

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
Comments
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

Please help me to complete this task
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.
   
Comments
Rob Grainger 28-Feb-14 15:35pm
   
Sorry it seems I misunderstood your question. From your questions and comments above I'm still unsure exactly what you're asking for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900