]> git.r.bdr.sh - rbdr/map/blob - Map/Presentation/Base Components/MapRender/MapGroup.swift
852c536f74dde3404f0671b957a5ca12d642ca2f
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapGroup.swift
1 /*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
16 */
17 import ConcaveHull
18 import SwiftUI
19
20 struct MapGroup: View {
21
22 let mapSize: CGSize
23 let vertexSize: CGSize
24 let group: [Vertex]
25 let color: Color
26
27 let cornerSize = CGSize(width: 2.0, height: 2.0)
28 var strokeSize: CGFloat { 1.75 * vertexSize.width }
29
30 var hull: [CGPoint] {
31 let groupList = group.map({ vertex in
32 return [Double(vertex.position.x), Double(vertex.position.y)]
33 })
34 let hull = Hull()
35 let hullPoints = hull.hull(groupList, nil)
36 return hullPoints.compactMap({ object in
37 if let point = object as? [Double] {
38 return CGPoint(x: point[0], y: point[1])
39 }
40 return nil
41 })
42 }
43
44 var body: some View {
45 Path { path in
46 var initialMove: CGPoint?
47
48 for point in hull {
49 let offsetPoint = CGPoint(x: w(point.x), y: h(point.y))
50
51 if initialMove == nil {
52 path.move(to: offsetPoint)
53 initialMove = offsetPoint
54 } else {
55 path.addLine(to: offsetPoint)
56 }
57 }
58
59 if let initialMove = initialMove {
60 path.addLine(to: initialMove)
61 }
62
63 }
64 .applying(
65 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
66 )
67 .fill(color)
68 .stroke(
69 color,
70 style: StrokeStyle(
71 lineWidth: strokeSize,
72 lineCap: .round,
73 lineJoin: .round,
74 miterLimit: 0,
75 dash: [],
76 dashPhase: 0
77 )
78 )
79 }
80
81 func h(_ dimension: CGFloat) -> CGFloat {
82 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
83 }
84
85 func w(_ dimension: CGFloat) -> CGFloat {
86 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
87 }
88 }
89
90 #Preview {
91 MapGroup(
92 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
93 group: [
94 Vertex(id: 0, label: "A Circle", position: CGPoint(x: 50.0, y: 50.0)),
95 Vertex(id: 1, label: "A Square", position: CGPoint(x: 10.0, y: 20.0), shape: .square),
96 Vertex(id: 2, label: "A triangle", position: CGPoint(x: 25, y: 32.0), shape: .triangle),
97 Vertex(id: 3, label: "An X", position: CGPoint(x: 70.0, y: 70.0), shape: .x),
98 ], color: .red)
99 }