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 1

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

    struct Line {Point start; Point end; }

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

  4. create a loop in which you run on this array

  5. 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

  6. 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.

  7. 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

  8. 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