I need a solution for a specific situation. I have an algorithm which basically works on line segments. I only know the line segments end and start points. (Points have 2 coordinates x,y. These are pixel points on image. Also I know the sizes of image.) I want to examine these line segments and do something on them. However, I also need to keep track of examined line segments so that, I wont examine the same line segment again. I’m using C++, so I want to use stl set container.
My question is how can I store these line segments according to their end and start points? I need to produce unique number for end and start points. (Also I’m open for any other advice other than using stl set :))
One possible solution is that I produced index numbers for these two pixels by: (y*image->Witdh) + x. Then I get two index numbers (by the way they are integers.) Then I concatenate these numbers by: (indexStart << 32) + indexEnd.(I get double). Now I have unique number and I can easily store in set. BUT the problem is while on my search, once a start point of a line segment can be the end point of the same line segment. If I came across the same line segment from endpoint then the concatenated line segments unique number become (indexEnd << 32) + indexStart. Then I will add the same line segment to the my set container which I need to avoid.
Thanks for any advice.
You can keep your solution or use an
operator<. Your has the advantage that switching to some hashset implementation is trivial, the other one should be better practice overall (much easier to understand, probably faster, can be used with 64bit coordinates, etc).Just solve the problem you describe (you’ll have to do this for both apporaches) by not distinguishing
startandendbutloweranduppercoordinates. Always using the smaller coordinate in the same place is an easy fix to getting false-positive different segments when you discover them the other way round