在C#中查找线 - 线交点的坐标

时间:2016-11-24 21:44:18

标签: c# line intersection line-intersection

首先,我制作了一个简单的代码,用于检查2条线是否根据x和y坐标给出的4个点发生碰撞。它检查两条线的角度(我的代码中的变量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);
    }
}

1 个答案:

答案 0 :(得分:0)

线方程:(?<==)[\w.]++

y = m * x + b

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