Any questions do not hesitate to contact.
/**
* Author: Ulf Lundstrom
* Date: 2009-03-21
* License: CC0
* Source:
* Description:\\
\begin{minipage}{75mm}
Returns a vector with the vertices of a polygon with everything to the left of the line going from s to e cut away.
\end{minipage}
\begin{minipage}{15mm}
\vspace{-6mm}
\includegraphics[width=\textwidth]{../content/geometry/PolygonCut}
\vspace{-6mm}
\end{minipage}
* Status: tested but not extensively
* Usage:
* vector<P> p = ...;
* p = polygonCut(p, P(0,0), P(1,0));
*/
#pragma once
#include "Point.h"
#include "lineIntersection.h"
typedef Point<double> P;
vector<P> polygonCut(const vector<P>& poly, P s, P e) {
vector<P> res;
FOR(i,0,sz(poly)) {
P cur = poly[i], prev = i ? poly[i-1] : poly.back();
bool side = s.cross(e, cur) < 0;
if (side != (s.cross(e, prev) < 0)) {
res.emplace_back();
lineIntersection(s, e, cur, prev, res.back());
}
if (side)
res.push_back(cur);
}
return res;
}
Keep in touch with Isaac Lozano Osorio!