MCPcopy Create free account
hub / github.com/Stanko/offset-polygon / createPaddingPolygon

Function createPaddingPolygon

src/offset-polygon.ts:241–287  ·  view source on GitHub ↗
(
  polygon: Polygon,
  offset: number,
  arcSegments: number
)

Source from the content-addressed store, hash-verified

239}
240
241function createPaddingPolygon(
242 polygon: Polygon,
243 offset: number,
244 arcSegments: number
245): Polygon {
246 const offsetEdges: OffsetEdge[] = [];
247
248 for (let i = 0; i < polygon.edges.length; i++) {
249 const edge = polygon.edges[i];
250 const dx = edge.inwardNormal.x * offset;
251 const dy = edge.inwardNormal.y * offset;
252 offsetEdges.push(createOffsetEdge(edge, dx, dy));
253 }
254
255 const vertices: Vector[] = [];
256
257 for (let i = 0; i < offsetEdges.length; i++) {
258 const thisEdge = offsetEdges[i];
259 const prevEdge =
260 offsetEdges[(i + offsetEdges.length - 1) % offsetEdges.length];
261 const vertex = edgesIntersection(prevEdge, thisEdge);
262 if (vertex && (!vertex.isIntersectionOutside || arcSegments < 1)) {
263 vertices.push({
264 x: vertex.x,
265 y: vertex.y,
266 });
267 } else {
268 const arcCenter = polygon.edges[i].vertex1;
269
270 appendArc(
271 arcSegments,
272 vertices,
273 arcCenter,
274 offset,
275 prevEdge.vertex2,
276 thisEdge.vertex1,
277 true
278 );
279 }
280 }
281
282 const paddingPolygon = createPolygon(vertices);
283
284 paddingPolygon.offsetEdges = offsetEdges;
285
286 return paddingPolygon;
287}
288
289export default function offsetPolygon(
290 vertices: Vector[],

Callers 1

offsetPolygonFunction · 0.70

Calls 4

createOffsetEdgeFunction · 0.70
edgesIntersectionFunction · 0.70
appendArcFunction · 0.70
createPolygonFunction · 0.70

Tested by

no test coverage detected