5 // Created by Ruben Beltran del Rio on 2/2/21.
10 struct MapOpportunities: View {
12 @Environment(\.colorScheme) var colorScheme
15 let lineWidth: CGFloat
16 let vertexSize: CGSize
17 let opportunities: [Opportunity]
19 let arrowheadSize = CGFloat(10.0)
22 MapColor.colorForScheme(colorScheme).opportunity
26 ForEach(opportunities, 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 multiplier = CGFloat(edge.destination.x > edge.origin.x ? 1.0 : -1.0)
34 let upperAngle = -CGFloat.pi / 4.0
35 let lowerAngle = CGFloat.pi / 4.0
37 let offsetOrigin = CGPoint(x: origin.x + multiplier * (vertexSize.width / 2.0), y: origin.y)
38 let offsetDestination = CGPoint(
39 x: destination.x - multiplier * (vertexSize.width / 2.0), y: destination.y)
41 path.move(to: offsetOrigin)
42 path.addLine(to: offsetDestination)
44 path.move(to: offsetDestination)
47 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
49 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
51 path.move(to: offsetDestination)
54 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
56 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
58 path.move(to: offsetDestination)
61 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
62 ).strokedPath(StrokeStyle(lineWidth: lineWidth * 2, dash: [10.0])).stroke(color)
66 func h(_ dimension: CGFloat) -> CGFloat {
67 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
70 func w(_ dimension: CGFloat) -> CGFloat {
71 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
75 struct MapOpportunities_Previews: PreviewProvider {
76 static var previews: some View {
78 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
79 vertexSize: CGSize(width: 25.0, height: 25.0),
81 Opportunity(id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2))