如何使用C#扩展两行以满足彼此 [英] How to extend two lines to meet each other using C#

查看:126
本文介绍了如何使用C#扩展两行以满足彼此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Windows窗体绘制了几行。



但是我希望通过扩展这些行来加入行。



有没有办法用Windows窗体扩展线



这里我的代码



  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));
}

解决方案

您的示例代码只显示一行 - 因此您无法扩展它什么都可以。

但如果你有两条线,那就很简单了。您所需要的只是两者相遇的点,这很容易计算: http://social.msdn.microsoft.com/Forums/vstudio/en-US/4eb3423e-eb81-4977-8ce5- 5a568d53fd9b / get-the-intersection-of-two-lines?forum = vbgeneral [ ^ ] - 代码在VB,但很明显。


最简单的方法是使用DrawLines mehtod。这需要一组点而不是单个点。前两个代表第一个线段。连续点添加从最后一点到新点的线段。



以下将绘制一个正方形:



钢笔= 笔(Color.Black, 3  ); 

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

graphics.DrawLines(笔,分);





应该足够的信息适用于你的原件问题


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

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));
               }

解决方案

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.


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:

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.


这篇关于如何使用C#扩展两行以满足彼此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆