How to find the coordinates of the point of intersection of two segments behind the coordinates of their beginnings and ends? (C / C ++). For example, the user enters:

  • ax1, ay1, ax2, ay2 - coordinates of the beginning and end of the first straight line;
  • bx1, by1, bx2, by2 - coordinates of the beginning and end of the second straight line;
  • cx, cy - coordinates of a certain point from which we will compare the intersection found;

That is, the coordinates of two segments and points are entered, you need to know - is the point the intersection of these segments?

  • Thank you, funny quest. - Sasha Gavrilov

1 answer 1

Well, I hope, remember that the straight line is described by a linear function y = ax + b.

Having coordinates of 2 points belonging to this line, x1, y1 x2, y2. you can calculate the values ​​of a and b

a = (y2-y1) / (x2-x1)

b = y1 - x1 * a

Having obtained these coefficients for 2 lines, we check

if ((a1*cx +b1) == cy && (a2*cx+ b2) == cy) puts ("Принадлежит"); 
  • mikillskegg, very grateful to you. Thank you, bearded (short). - Sasha Gavrilov
  • We put a plus sign - skegg
  • Ingvar (Ingvarr or Inglar) is a Scandinavian name. Ingvar - the god of fertility. Different nations named Igor correspond to the names: Igor → Gor → Iggor, Ingvar, Inglar, Ingmar, Ingemar, Yngwie, Yngvi, Yngve, Yngvin, Ingwine, Inguin, Igor and others. The source is yours and our internets. - Sasha Gavrilov
  • I'm talking about a nickname, not about the name. The name is too simple. - skegg
  • Big beard? - Sasha Gavrilov