From 605468a995a53ea79602574e2c092ffe91f74d52 Mon Sep 17 00:00:00 2001 From: Zack Brown Date: Wed, 20 Sep 2023 11:50:40 +0100 Subject: [PATCH] Add method to inset polygon vertices --- Sources/Polygon+CSG.swift | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Sources/Polygon+CSG.swift b/Sources/Polygon+CSG.swift index 58b4b7ed..dadc67ad 100644 --- a/Sources/Polygon+CSG.swift +++ b/Sources/Polygon+CSG.swift @@ -245,3 +245,31 @@ extension Polygon { return false } } + +public extension Polygon { + func inset(by t: Double) -> Self? { + var v: [Vertex] = [] + + for i in orderedEdges.indices { + let e0 = orderedEdges[i] + let e1 = orderedEdges[(i + 1) % orderedEdges.count] + + let p0 = edgePlane(for: e0) + let p1 = edgePlane(for: e1) + + let normal = -(p0.normal + p1.normal).normalized() * t + + let p2 = p0.translated(by: normal) + let p3 = p1.translated(by: normal) + + let vector = vertices.first { $0.position.isEqual(to: e0.end) } + + guard let vector, + let intersection = p2.intersection(with: p3) else { return nil } + + v.append(Vertex(intersection.origin, vector.normal, vector.texcoord, vector.color)) + } + + return Self(v) + } +}