【问题标题】:Draw Line between two points and extend the line on both sides在两点之间画线并在两侧延长线
【发布时间】:2019-02-22 13:21:34
【问题描述】:

我有下面的代码,它允许我在两个给定点之间画一条线。我需要做的是双向延伸这些线,以便线以相同的角度延伸到线的两侧

private void Form1_Load(object sender, EventArgs e)
{
    this.Controls.Add(new Panel{Left = 10, Top = 10,Width = 50,Height = 50, BackColor = Color.Blue});
    this.Controls.Add(new Panel {Left = 100, Top = 100,Width = 50,Height = 50, BackColor = Color.Blue});


}
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Graphics g;
    g = e.Graphics;
    Pen myPen = new Pen(Color.Red);
    myPen.Width = 1;
    g.DrawLine(myPen, 12, 12, 45, 65);
    g.DrawLine(myPen, 100, 100, 45, 65);
}

【问题讨论】:

  • 不相关,但我想知道为什么Graphics g; g = e.Graphics;,您正在使用C#,您不必先声明然后分配,正常是在一个语句中同时完成
  • @CamiloTerevinto 没关系,我想念你的意思:D
  • 这个问题有点不清楚。你试过什么,什么不起作用?您需要将线延伸到两点之外多远? Panels 是你的点吗(对于 50x50 像素的点来说它们相当大)?您是否正在寻求确定点的数学帮助,而不是仅仅尝试不同的坐标,直到得到您想要的?您可以edit your question提供更多详细信息。
  • 我不确定你所说的“到最后”是什么意思,但是你可以使用斜率截距公式来找到一条线上的任何点,所以@987654322 @ 找到结束slope-intercept formula 解决坐标。
  • 给我们画一张带有标签的图片,向我们展示您想要什么。例如,您可以在 Paint 或 PowerPoint 中使用它。

标签: c# winforms


【解决方案1】:

尝试使用方法:

startPoint 和 endPoint 是著名的点。 p1 和 p2 是新的起点和终点。

 public static void FindSimmetricPointsOnLine(Point startPoint, Point endPoint, double distance, out Point p1, out Point p2)
        {
            p1 = new Point();
            p2 = new Point();
            var xa = startPoint.X;
            var ya = startPoint.Y;
            var xb = endPoint.X;
            var yb = endPoint.Y;
            var l = distance;
            double x1 = 0;
            double x2 = 0;
            double y1 = 0;
            double y2 = 0;


            if (xa == xb)
            {
                x1 = x2 = xa;
                y1 = ya + l;
                y2 = ya - l;
            }
            else
            {
                if (ya == yb)
                {
                    y1 = y2 = ya;
                    x1 = xa + l;
                    x2 = xa - l;
                }
                else
                {
                    var K = (ya - yb)/(xa - xb);
                    var B = ya - K*xa;
                    var A1 = K*K + 1;
                    var B1 = -2*(xa + K*(ya - B));
                    var C1 = xa*xa + (ya - B)*(ya - B) - l*l;
                    var D = B1*B1 - 4*A1*C1;

                    if (D >= 0)
                    {
                        x1 = (-B1 + Math.Sqrt(D))/(2*A1);
                        x2 = (-B1 - Math.Sqrt(D))/(2*A1);


                        y1 = K*x1 + B;
                        y2 = K*x2 + B;

                        p1 = new Point(x1, y1);
                        p2 = new Point(x2, y2);
                    }
                }
            }
        }

【讨论】:

    【解决方案2】:

    这里有一些代码可以满足你的需要。

    public void ExtendLine(Point p1, Point p2, double distance, out Point start, out Point end)
    {
        //first find the vector that represents the direction of the line
        Vector direction = p2 - p1;
    
        //normalize the vector to a distance of one point
        direction.Normalize();
    
        //multiply the direction by to total distance to find the offset amount
        Vector offset = (direction * distance);
    
        //subtract the offset from the start point to find the new start point
        start = p1 - offset;
    
        //add the offset to the end point to find the new end point
        end = p2 + offset;
    }
    

    这是一个展示如何使用该方法的示例。

    Point p1 = new Point(Line1.X1, Line1.Y1), p2 = new Point(Line1.X2, Line1.Y2);
    ExtendLine(p1, p2, 10, out Point p3, out Point p4);
    Line2.X1 = p3.X;
    Line2.Y1 = p3.Y;
    Line2.X2 = p4.X;
    Line2.Y2 = p4.Y;
    

    【讨论】: