Given the coordinates of the vertices of the polyline in the plane. Determine whether the polyline has self-intersections, and if so, write the coordinates of the self-intersection points to the G file. Implement on binary files using data structures (struct).

- @morf, According to the rules of the forum, questions should not be limited to the decision or the completion of student assignments. Please clarify what you have done yourself and what did not work out. - Specter
- I forgot to add, it is necessary to work with a binary file: 1) write a binary file with coordinates (there are no problems with this) 2) read these coordinates from a file (small problems arise) 3) data processing and counting of intersection points I get an error saying "division by 0" or thinks, but does not fulfill the condition) 4) write in binary form to file G (no problem with that) So I would if the exact program code for which the intersection point is considered correct and it would be everything is good, I can handle the rest ... And thanks for your answer oh, he explained a lot to me - morf

## 1 answer

- Create an array to store intersection points there.
create some data type for the segments that make up the polyline. sort of

struct Line {Point start; Point end; }

Create and fill an array of segments that make up the polyline.

create a loop in which you run on this array

create another nested loop in which you run through the same array, but not from the beginning, but from the current element from the previous cycle

in this nested loop, determine if the current segment of the outer loop intersects with the current segment of the inner one. To do this, solve something like this equation: (I could be wrong, but the details can be corrected) the coefficients are easy to know with the starting and ending point of the segment.

If x belongs to segments (it may not belong, because we only have segments, but not infinite straight lines), then add the intersection point to the array

On exit from both cycles, write the resulting array to a file.

The algorithm is clearly not perfect, it can (and probably should) be optimized, but that's another story