简体   繁体   中英

How to to compare two points in 2d space

I have a class for holding my Points in 2D space like this:

class Point{
public:
Point(double a, double b){ x = a; y = b; }

//some additional information

private:
    double x, y;
};

I like to have these Points in a std::set but I dont know how to write the compare struct

struct cCompare{
    bool operator()(const Point &p1, const Point &p2){
         //what should I write here??
    }
};

Two Points like p and q are equal if (p_1,p_2) = (q_1,q_2) . Do I have to store some additional information in my Point class? Something like index or any unique number for each Point ? And have something like this:

struct cCompare{
    bool operator()(const Point &p1, const Point &p2){
         return (p1.index < p2.index);
    }
};

If you need just any ordering relation for the sake of storing in a set, you could use the dictionary order:

P1 < P2 iff P1.x < P2.x || (P1.x == P2.x && P1.y < P2.y) P1.x < P2.x || (P1.x == P2.x && P1.y < P2.y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM