11_C++精灵库之polygon_offset.h头文件源代码(2026年1月15日版)

// polygon_offset.h
#ifndef POLYGON_OFFSET_H
#define POLYGON_OFFSET_H

#include <vector>

struct Point {
    double x, y;
    Point();
    Point(double x, double y);
    friend bool operator<(const Point& a, const Point& b);
    friend bool operator==(const Point& a, const Point& other) ;

};

// 重载运算符(必须在头文件中声明,以便被所有包含它的编译单元看到)
Point operator+(const Point& a, const Point& b);
Point operator-(const Point& a, const Point& b);
Point operator*(const Point& a, double t);
Point operator*(double t, const Point& a); // 支持 t * Point
Point operator/(const Point& a, double t);
 
// 向量运算函数声明
double dot(const Point& a, const Point& b);
double cross(const Point& a, const Point& b);
double norm(const Point& a);
Point normalize(const Point& a);
Point normal(const Point& v);
bool isClockwise(const std::vector<Point>& poly);

struct Segment {
    Point p1, p2;
    int index;
    Segment(Point a, Point b, int idx);
    double getYAt(double x) const;
    bool operator<(const Segment& other) const;
};
bool isSelfIntersectingRobust(const std::vector<Point>& polygon, double epsilon= 1e-10);
//bool isSelfIntersecting(const std::vector<Point>& polygon);

// 线段相交检测函数声明
bool segmentsIntersect(const Point& a, const Point& b, const Point& c, const Point& d);

// 注意:operator* 不支持 Point * Point(无意义)
std::vector<Point> miterOffsetPolygon(const std::vector<Point>& points, double offset_distance, double miter_limit=0.25);
std::vector<Point> miterOffsetPolygonRobust(const std::vector<Point>& points, double offset_distance, double miter_limit = 0.25);

#endif // POLYGON_OFFSET_H

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在C++分类目录。将固定链接加入收藏夹。

发表回复