【问题标题】:Finding Coordinates of Line-Line Intersection in C#在 C# 中查找线-线交点的坐标
【发布时间】:2016-11-24 21:44:18
【问题描述】:

首先,我编写了一个简单的代码,根据 x 和 y 坐标给出的 4 个点检查 2 条线是否发生碰撞。它检查两条线的角度(我的代码中的变量 k )是否相同,在这种情况下它们是平行的,否则它们会发生碰撞。角度 (k) 是根据数学方程 Click here [k = (y2-y1)/(x2-x1)] 计算得出的。现在我不知道如何理解它们的冲突点。如果你能帮助我,我将不胜感激。提前谢谢你。

我的代码:(我调用计算角度的方法)

static void MetodaTrazenjaPresjeka(Duzina d1, Duzina d2)
    {

        int k11 = d1.Krajy - d1.Pocy; //y2-y1 - first line
        int k12 = d1.Krajx - d1.Pocx; //x2-x1 - first line
        double k1 = (double)k11 / k12; //angle of the first line
        int k21 = d2.Krajy - d2.Pocy; //y2-y1 - second line
        int k22 = d2.Krajx - d2.Pocx; //x2-x1 - second line
        double k2 = (double)k21 / k22; //angle of the second line
        if (k1 == k2)
        {
            //they are parallel
            Console.WriteLine("MOJA METODA:");
            Console.WriteLine("-----------------------------------");
            Console.Write("Pravci zadani tockama su paralelni!");
        }
        else
        {
            //lines are colliding
            Console.WriteLine("MOJA METODA:");
            Console.WriteLine("-----------------------------------");
            Console.Write("Pravci zadani tockama se sijeku!");
        }
    }

Duzina 类中的代码:

class Duzina
{
    private int pocx, pocy, krajx, krajy;
    //read/write attribute for the x coordinate of the first point
    public int Pocx
    {
        get { return pocx; }
        set { pocx = value; }
    }
    //read/write attribute for the y coordinate of the first point
    public int Pocy
    {
        get { return pocy; }
        set { pocy = value; }
    }
    //read/write attribute for the x coordinate of the second point
    public int Krajx
    {
        get { return krajx; }
        set { krajx = value; }
    }
    //read/write attribute for the y coordinate of the second point
    public int Krajy
    {
        get { return krajy; }
        set { krajy = value; }
    }
    //method that will print out coordinates of the given points
    public void Ispis()
    {
        Console.Write("Pocetna tocka: ({0},{1})",Pocx,Pocy);
        Console.Write("Krajnja tocka: ({0},{1})", Krajx, Krajy);
    }
}

【问题讨论】:

  • 您不想使用自动属性来缩短您的代码吗?
  • 我在读高中,这是一种确保我不会忘记我们在课堂上学习的方法@ThomasWeller
  • @HR_Luka 我知道这不是您的要求,但也许构建 Duzina 类的更好方法是拥有一个具有 x 和 y 属性的点类,并且 Duzina 将为每个类都有一个属性点。

标签: c# line intersection line-intersection


【解决方案1】:

线方程:y = m * x + b

1) m_d1 = (d1.y2 - d1.y1) / (d1.x2 - d1.x1)
   b_d1 = d1.y1 - m_d1 * d1.x1
   (same for m_d2 and b_d2)

2) intersect.y = m_d1 * intersect.x + b_d1
   intersect.y = m_d2 * intersect.x + b_d2

3) m_d1 * intersect.x + b_d1 = m_d2 * intersect.x + b_d2

4) intersect.x = (b_d2 - b_d1) / (m_d1 - m_d2)

现在将从 4) 中获得的 intersect.x 插回 2) 中的任一方程以获得 intersection.y

【讨论】:

  • 这不是一个编码问题,而是一个数学问题,所以我提供了数学,你可以编写代码(这很简单)。
猜你喜欢
  • 2012-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多