Module: Geometry


Problem

5 /7


Pickup Master

Theory Click to read/hide

Intersection

Point of intersection of lines

a1, b1, c1 - coefficients of the first line,
a2, b2, c2 - coefficients of the second line,
x, y - intersection point.

\(x = {-(c1 \cdot b2 - c2 \cdot b1) \over (a1 \cdot b2 - a2 \cdot b1)} \\ y = {-(a1 \ cdot c2 - a2 \cdot c1) \over (a1 \cdot b2 - a2 \cdot b1)} \)

We already know how to check lines for intersection (they are not parallel), and find their point of intersection.

Now let's learn how to do this with segments

First, let's learn how to simply check them for intersection.

Segments intersect if the ends of one are on opposite sides of the other and vice versa (this is easily checked by the cross product). The only case when this will not work - the segments lie on one straight line. For it, you need to check for the intersection of the so-called. bounding box (bounding box of the segment) - check for the intersection of the projection of the segments on the X and Y.

axes.

Now that we know how to check segments for intersection, let's learn how to find the point (or segment) of their intersection:
- if they do not intersect, then it is clear that such a point does not exist;
- otherwise, we will construct straight lines on which these segments lie.

If they are parallel, then the segments lie on the same line, and we need to find the intersection segment - from the maximum of the left borders of the segments to the minimum of the right borders (the point is less than the other point, if it is to the left, in case of equality X-coordinates - if it is lower).

If the lines are not parallel, then find the point of their intersection and return it.

Problem

Venceslav recently read a new pickup book and now he wants to test his knowledge in the park. For simplicity, let's imagine the park as a set of paths, which are segments on a plane. Wenceslas has already walked in this park, and he knows which girl is walking along which path. The problem is that Wenceslas is very lazy and only walks along one path. And he is also too lazy to find out what kind of girls he can meet along the way, and therefore he asked You, his best friend, to help him in this difficult matter.
 
Input
The first line contains the coordinates of the ends of the path (X1, Y1) and ( X2, Y2), along which Wenceslas walks (\(- 20 <= X1, Y1, X2, Y2 <= 20\)).
The second line contains an integer N - the number of paths the girls walk along (\(0 <= N <= 5\)< /span>).
In the following N lines, enter the coordinates of the ends of the paths along which the girls walk (Xi1, Yi1< /sub>) and (Xi2, Yi2), by i -th path walking i-th girl (\(-20 <= X_{i1}, Y_{i1}, X_{i2 }, Y_{i2} <= 20\))
 
Output
In the first line print the number M - the number of girls whose paths will intersect with the path of Wenceslas (touching the paths is considered an intersection).
In the second line print M numbers - numbers of girls our hero will meet. Girls are numbered starting from one!
 
Examples
# Input Output
1
0 0 2 2
1
0 2 2 0
1
1