diff options
Diffstat (limited to 'Map/Presentation/Base Components/MapRender/MapGroup.swift')
| -rw-r--r-- | Map/Presentation/Base Components/MapRender/MapGroup.swift | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/Map/Presentation/Base Components/MapRender/MapGroup.swift b/Map/Presentation/Base Components/MapRender/MapGroup.swift new file mode 100644 index 0000000..9df3338 --- /dev/null +++ b/Map/Presentation/Base Components/MapRender/MapGroup.swift @@ -0,0 +1,83 @@ +import ConcaveHull +import SwiftUI + +struct MapGroup: View { + + let mapSize: CGSize + let vertexSize: CGSize + let group: [Vertex] + let color: Color + + let cornerSize = CGSize(width: 2.0, height: 2.0) + var strokeSize: CGFloat { 1.75 * vertexSize.width } + + var hull: [CGPoint] { + let groupList = group.map({ vertex in + return [Double(vertex.position.x), Double(vertex.position.y)] + }) + let hull = Hull() + let hullPoints = hull.hull(groupList, nil) + return hullPoints.compactMap({ object in + if let point = object as? [Double] { + return CGPoint(x: point[0], y: point[1]) + } + return nil + }) + } + + var body: some View { + Path { path in + var initialMove: CGPoint? + + for point in hull { + let offsetPoint = CGPoint(x: w(point.x), y: h(point.y)) + + if initialMove == nil { + path.move(to: offsetPoint) + initialMove = offsetPoint + } else { + path.addLine(to: offsetPoint) + } + } + + if let initialMove = initialMove { + path.addLine(to: initialMove) + } + + } + .applying( + CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0) + ) + .fill(color) + .stroke( + color, + style: StrokeStyle( + lineWidth: strokeSize, + lineCap: .round, + lineJoin: .round, + miterLimit: 0, + dash: [], + dashPhase: 0 + ) + ) + } + + func h(_ dimension: CGFloat) -> CGFloat { + max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0)) + } + + func w(_ dimension: CGFloat) -> CGFloat { + max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0)) + } +} + +#Preview { + MapGroup( + mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0), + group: [ + Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)), + Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square), + Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle), + Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x), + ], color: .red) +} |