3 struct MapVertices: View {
5 @Environment(\.colorScheme) var colorScheme
10 let padding = CGFloat(4.0)
13 MapColor.colorForScheme(colorScheme)
17 ForEach(vertices, id: \.id) { vertex in
18 getVertexShape(vertex).fill(color.foreground)
19 Text(vertex.label).foregroundColor(color.secondary).offset(
21 width: w(vertex.position.x) + vertexSize.width + padding,
22 height: h(vertex.position.y)))
26 func h(_ dimension: CGFloat) -> CGFloat {
27 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
30 func w(_ dimension: CGFloat) -> CGFloat {
31 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
34 func getVertexShape(_ vertex: Vertex) -> Path {
40 origin: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)), size: vertexSize
47 x: w(vertex.position.x), y: h(vertex.position.y), width: vertexSize.width,
48 height: vertexSize.height
53 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
56 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
59 to: CGPoint(x: w(vertex.position.x) + vertexSize.width / 2.0, y: h(vertex.position.y)))
61 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
66 path.move(to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y)))
69 x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y) + vertexSize.height)
72 path.move(to: CGPoint(x: w(vertex.position.x) + vertexSize.width, y: h(vertex.position.y)))
74 to: CGPoint(x: w(vertex.position.x), y: h(vertex.position.y) + vertexSize.height))
76 }.strokedPath(StrokeStyle(lineWidth: 5.0, lineCap: .butt))
81 struct MapVertices_Previews: PreviewProvider {
82 static var previews: some View {
84 mapSize: CGSize(width: 400.0, height: 400.0), vertexSize: CGSize(width: 25.0, height: 25.0),
86 Vertex(id: 0, label: "A", position: CGPoint(x: 50.0, y: 50.0)),
87 Vertex(id: 0, label: "A", position: CGPoint(x: 10.0, y: 20.0)),