![](/img/trans.png)
[英]Finding equally spaced points given a set of points with line segments in between
[英]Ordering line segments consisting of two points
如何对由起点和终点组成的线段实施operator <。 我想将线段插入地图中,因此顺序不必是语义上的,但它适用于所有情况。
按字典顺序订购所有内容:
struct Point { int x; int y; };
bool operator<(Point const & a, Point const & b)
{
return (a.x < b.x) || (!(b.x < a.x) && (a.y < b.y));
}
或使用tuple
现成的比较器:
#include <tuple>
// ...
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
或者实际上将std::tuple<int, int>
用于您的观点,什么也不做!
然后,对各行执行相同的操作:
struct LineSegment { Point x; Point y; };
// repeat same code as above, e.g.
bool operator<(LineSegment const & a, LineSegment const & b)
{
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
}
再说一遍,“一劳永逸”的解决方案仅使用元组:
typedef std::tuple<int, int> Point;
typedef std::tuple<Point, Point> LineSegment;
// everything "just works"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.