5 // Created by Ruben Beltran del Rio on 2/2/21.
10 struct MapEdges: View {
12 @Environment(\.colorScheme) var colorScheme
15 let lineWidth: CGFloat
16 let vertexSize: CGSize
19 let arrowheadSize = CGFloat(10.0)
22 MapColor.colorForScheme(colorScheme).foreground
26 ForEach(edges, id: \.id) { edge in
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))
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
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))
46 path.move(to: offsetOrigin)
47 path.addLine(to: offsetDestination)
50 path.move(to: offsetDestination)
53 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
55 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
57 path.move(to: offsetDestination)
60 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
62 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
64 path.move(to: offsetDestination)
67 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
68 ).stroke(color, lineWidth: lineWidth)
72 func h(_ dimension: CGFloat) -> CGFloat {
73 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
76 func w(_ dimension: CGFloat) -> CGFloat {
77 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
81 struct MapEdges_Previews: PreviewProvider {
82 static var previews: some View {
84 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
85 vertexSize: CGSize(width: 25.0, height: 25.0),
88 id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2),