-1

首先,我编写了一个简单的代码,根据 x 和 y 坐标给出的 4 个点检查 2 条线是否发生碰撞。它检查两条线的角度(我的代码中的变量 k )是否相同,在这种情况下它们是平行的,否则它们会发生碰撞。角度 (k) 是根据数学方程计算得出的,单击此处[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);
    }
}
4

1 回答 1

0

线方程: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) 中的任一方程以获得intersect.y

于 2016-11-25T00:04:38.780 回答