]> git.r.bdr.sh - rbdr/map/blame - Map/MapRenderComponents/MapEdges.swift
Let app store know we're about productivity
[rbdr/map] / Map / MapRenderComponents / MapEdges.swift
CommitLineData
5e8ff485
RBR
1//
2// MapEdges.swift
3// Map
4//
5// Created by Ruben Beltran del Rio on 2/2/21.
6//
7
8import SwiftUI
9
10struct MapEdges: View {
11
12 @Environment(\.colorScheme) var colorScheme
13
14 let mapSize: CGSize
15 let lineWidth: CGFloat
16 let vertexSize: CGSize
17 let edges: [MapEdge]
18
19 let arrowheadSize = CGFloat(10.0)
20
21 var color: Color {
22 MapColor.colorForScheme(colorScheme).foreground
23 }
24
25 var body: some View {
26 ForEach(edges, id: \.id) { edge in
27 Path { path in
28
29 // First we transform edges from percentage to map coordinates
30 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
31 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
32
33 let slope = (destination.y - origin.y) / (destination.x - origin.x)
34 let angle = atan(slope)
35 let multiplier = CGFloat(slope < 0 ? -1.0 : 1.0)
36 let upperAngle = angle - CGFloat.pi / 4.0
37 let lowerAngle = angle + CGFloat.pi / 4.0
38
39 let offsetOrigin = CGPoint(
40 x: origin.x + multiplier * (vertexSize.width / 2.0) * cos(angle),
41 y: origin.y + multiplier * (vertexSize.height / 2.0) * sin(angle))
42 let offsetDestination = CGPoint(
43 x: destination.x - multiplier * (vertexSize.width / 2.0) * cos(angle),
44 y: destination.y - multiplier * (vertexSize.height / 2.0) * sin(angle))
45
46 path.move(to: offsetOrigin)
47 path.addLine(to: offsetDestination)
48
49 if edge.arrowhead {
50 path.move(to: offsetDestination)
51 path.addLine(
52 to: CGPoint(
53 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
54 y:
55 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
56
57 path.move(to: offsetDestination)
58 path.addLine(
59 to: CGPoint(
60 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
61 y:
62 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
63 }
64 path.move(to: offsetDestination)
65 path.closeSubpath()
66 }.applying(
67 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
68 ).stroke(color, lineWidth: lineWidth)
69 }
70 }
71
72 func h(_ dimension: CGFloat) -> CGFloat {
73 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
74 }
75
76 func w(_ dimension: CGFloat) -> CGFloat {
77 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
78 }
79}
80
81struct MapEdges_Previews: PreviewProvider {
82 static var previews: some View {
83 MapEdges(
84 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
85 vertexSize: CGSize(width: 25.0, height: 25.0),
86 edges: [
87 MapEdge(
88 id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2),
89 arrowhead: true)
90 ])
91 }
92}