I may be missing something (for example, I don't understand why you'd want to do this - maybe you are drawing things in different colors or something? If you are just trying to optimize a few write operations, I'm not sure that you are going to really gain anything by this).
But, assuming there is a good reason to do this, I'd think that the following algorithm would work:
- Determine all horizontal line segments, and order by y position descending and segment length descending
- Draw the first line segment
- Compare the second line segment's y-position to all previous lines in the list (in this case, just the first) that have the same y position. If you don't get an exact y-position match, draw the segment, and repeat step 3 for subsequent segments
- If you do get an exact y position match, compare the end point of the shorter segment to see if it's x position is between the x position of the two end points of the longer segment. If it is, then you have overlap. If it doesn't, check the other end point.
I'm assuming that the layout of the segments is such that you can't have two segments that partially overlap each other, like this (segments are aA and bB):
a=====b===A=========B
If you do have that possibility, then you'll have to decide how to resolve that.
PS - please definitely add a brief description of why you want to eliminate those segments. I'm curious!