-1

to start with I made a simple code that checks if 2 lines collide based on 4 points that are given by their x and y coordinates. It checks if the angle (variable k in my code) of both lines is the same, in that case they are parallel, otherwise they collide. The angle (k) was calculated based on the math equasion Click here [k = (y2-y1)/(x2-x1)]. Now I am not sure how to get the point they are colliding in. I would appreciate it very much if you could help me. Thank you in advance.

My Code: (method I call to calculate angle)

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

Code in class 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);
    }
}
HR_Luka
  • 1
  • 1
  • 4
  • Possible duplicate of [How do you detect where two line segments intersect?](http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect) – EAX Nov 24 '16 at 21:50
  • Don't you want to use auto-properties to make your code shorter? – Thomas Weller Nov 24 '16 at 21:50
  • I am in high school and this is a way to make sure i do not forget what we are learning at class @ThomasWeller – HR_Luka Nov 24 '16 at 21:57
  • @HR_Luka I know this isn't what your asking but perhaps a better way to structure the Duzina class would be to have a point class that has x and y properties and Duzina would have a property for each point. – Danny Nov 25 '16 at 02:06

1 Answers1

0

line equation: 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)

now plug intersect.x that was obtained from 4) back into either equation in 2) to get intersection.y

Danny
  • 541
  • 2
  • 11
  • This is not a coding question but a math question so I provided the math and you can write the code (which is trivial). – Danny Nov 25 '16 at 00:05