]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapGroup.swift
9df333815c213871e175d0333ba2c68545ef4760
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapGroup.swift
1 import ConcaveHull
2 import SwiftUI
3
4 struct MapGroup: View {
5
6 let mapSize: CGSize
7 let vertexSize: CGSize
8 let group: [Vertex]
9 let color: Color
10
11 let cornerSize = CGSize(width: 2.0, height: 2.0)
12 var strokeSize: CGFloat { 1.75 * vertexSize.width }
13
14 var hull: [CGPoint] {
15 let groupList = group.map({ vertex in
16 return [Double(vertex.position.x), Double(vertex.position.y)]
17 })
18 let hull = Hull()
19 let hullPoints = hull.hull(groupList, nil)
20 return hullPoints.compactMap({ object in
21 if let point = object as? [Double] {
22 return CGPoint(x: point[0], y: point[1])
23 }
24 return nil
25 })
26 }
27
28 var body: some View {
29 Path { path in
30 var initialMove: CGPoint?
31
32 for point in hull {
33 let offsetPoint = CGPoint(x: w(point.x), y: h(point.y))
34
35 if initialMove == nil {
36 path.move(to: offsetPoint)
37 initialMove = offsetPoint
38 } else {
39 path.addLine(to: offsetPoint)
40 }
41 }
42
43 if let initialMove = initialMove {
44 path.addLine(to: initialMove)
45 }
46
47 }
48 .applying(
49 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
50 )
51 .fill(color)
52 .stroke(
53 color,
54 style: StrokeStyle(
55 lineWidth: strokeSize,
56 lineCap: .round,
57 lineJoin: .round,
58 miterLimit: 0,
59 dash: [],
60 dashPhase: 0
61 )
62 )
63 }
64
65 func h(_ dimension: CGFloat) -> CGFloat {
66 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
67 }
68
69 func w(_ dimension: CGFloat) -> CGFloat {
70 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
71 }
72 }
73
74 #Preview {
75 MapGroup(
76 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
77 group: [
78 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
79 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
80 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
81 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),
82 ], color: .red)
83 }