23 constexpr static std::vector<PointType>
clip(
const std::vector<PointType>& subject,
24 const std::vector<PointType>& boundary)
26 if(boundary.empty() || subject.empty())
30 std::vector<PointType> ring=subject;
31 PointType
a=boundary.back();
32 for(
const auto&
b : boundary)
38 const PointType edge=
b-
a;
39 if(edge.squaredNorm()==0.0)
44 const auto distance=[&](
const PointType& point)
46 const PointType relative=point-
a;
47 return edge(0)*relative(1)-edge(1)*relative(0);
49 std::vector<PointType> input;
51 PointType start=input.back();
52 double startDistance=distance(start);
53 for(
const auto&
end : input)
55 const double endDistance=distance(
end);
56 const bool startInside=startDistance>=0.0;
57 const bool endInside=endDistance>=0.0;
58 if(startInside!=endInside)
65 const double t=std::clamp(startDistance/(startDistance-endDistance),0.0,1.0);
66 ring.push_back(start+
t*(
end-start));
73 startDistance=endDistance;