Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to inset polygon vertices #106

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Sources/Polygon+CSG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,27 @@
return false
}
}

public extension Polygon {
func inset(by t: Double) -> Self? {
let v: [Vertex] = orderedEdges.indices.compactMap {
let e0 = orderedEdges[$0]
let e1 = orderedEdges[($0 + 1) % orderedEdges.count]

let p0 = edgePlane(for: e0)
let p1 = edgePlane(for: e1)

let lhs = p0.translated(by: -p0.normal * t)
let rhs = p1.translated(by: -p1.normal * t)

let vertex = vertices.first { $0.position.isEqual(to: e0.end) }

guard let vertex,
let intersection = lhs.intersection(with: rhs) else { return nil }

return Vertex(intersection.origin, vertex.normal, vertex.texcoord, vertex.color)
}

return Self(v)
}

Check warning on line 270 in Sources/Polygon+CSG.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Polygon+CSG.swift#L250-L270

Added lines #L250 - L270 were not covered by tests
}
Loading