| | 1 | | using System; |
| | 2 | |
|
| | 3 | | namespace Itinero.Geo.Directions; |
| | 4 | |
|
| | 5 | | internal static class DirectionCalculator |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Calculates the angle in radians at coordinate2. |
| | 9 | | /// </summary> |
| | 10 | | public static double Angle((double longitude, double latitude, float? e) coordinate1, |
| | 11 | | (double longitude, double latitude, float? e) coordinate2, |
| | 12 | | (double longitude, double latitude, float? e) coordinate3) |
| 5 | 13 | | { |
| 5 | 14 | | var v11 = coordinate1.latitude - coordinate2.latitude; |
| 5 | 15 | | var v10 = coordinate1.longitude - coordinate2.longitude; |
| | 16 | |
|
| 5 | 17 | | var v21 = coordinate3.latitude - coordinate2.latitude; |
| 5 | 18 | | var v20 = coordinate3.longitude - coordinate2.longitude; |
| | 19 | |
|
| 5 | 20 | | var v1Size = Math.Sqrt((v11 * v11) + (v10 * v10)); |
| 5 | 21 | | var v2Size = Math.Sqrt((v21 * v21) + (v20 * v20)); |
| | 22 | |
|
| 5 | 23 | | if (v1Size == 0 || v2Size == 0) |
| 0 | 24 | | { |
| 0 | 25 | | return double.NaN; |
| | 26 | | } |
| | 27 | |
|
| 5 | 28 | | var dot = (double)((v11 * v21) + (v10 * v20)); |
| 5 | 29 | | var cross = (double)((v10 * v21) - (v11 * v20)); |
| | 30 | |
|
| | 31 | | // split per quadrant. |
| | 32 | | double angle; |
| 5 | 33 | | if (dot > 0) |
| 0 | 34 | | { // dot > 0 |
| 0 | 35 | | if (cross > 0) |
| 0 | 36 | | { // dot > 0 and cross > 0 |
| | 37 | | // Quadrant 1 |
| 0 | 38 | | angle = (double)Math.Asin(cross / (v1Size * v2Size)); |
| 0 | 39 | | if (angle < Math.PI / 4f) |
| 0 | 40 | | { // use cosine. |
| 0 | 41 | | angle = (double)Math.Acos(dot / (v1Size * v2Size)); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | // angle is ok here for quadrant 1. |
| 0 | 45 | | } |
| | 46 | | else |
| 0 | 47 | | { // dot > 0 and cross <= 0 |
| | 48 | | // Quadrant 4 |
| 0 | 49 | | angle = (double)(Math.PI * 2.0f) + (double)Math.Asin(cross / (v1Size * v2Size)); |
| 0 | 50 | | if (angle > (double)(Math.PI * 2.0f) - (Math.PI / 4f)) |
| 0 | 51 | | { // use cosine. |
| 0 | 52 | | angle = (double)(Math.PI * 2.0f) - (double)Math.Acos(dot / (v1Size * v2Size)); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | // angle is ok here for quadrant 1. |
| 0 | 56 | | } |
| 0 | 57 | | } |
| | 58 | | else |
| 5 | 59 | | { // dot <= 0 |
| 5 | 60 | | if (cross > 0) |
| 5 | 61 | | { // dot > 0 and cross > 0 |
| | 62 | | // Quadrant 2 |
| 5 | 63 | | angle = (double)Math.PI - (double)Math.Asin(cross / (v1Size * v2Size)); |
| 5 | 64 | | if (angle > (Math.PI / 2f) + (Math.PI / 4f)) |
| 0 | 65 | | { // use cosine. |
| 0 | 66 | | angle = (double)Math.Acos(dot / (v1Size * v2Size)); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | // angle is ok here for quadrant 2. |
| 5 | 70 | | } |
| | 71 | | else |
| 0 | 72 | | { // dot > 0 and cross <= 0 |
| | 73 | | // Quadrant 3 |
| 0 | 74 | | angle = -(-(double)Math.PI + (double)Math.Asin(cross / (v1Size * v2Size))); |
| 0 | 75 | | if (angle < Math.PI + (Math.PI / 4f)) |
| 0 | 76 | | { // use cosine. |
| 0 | 77 | | angle = (double)(Math.PI * 2.0f) - (double)Math.Acos(dot / (v1Size * v2Size)); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | // angle is ok here for quadrant 3. |
| 0 | 81 | | } |
| 5 | 82 | | } |
| | 83 | |
|
| 5 | 84 | | return angle; |
| 5 | 85 | | } |
| | 86 | |
|
| | 87 | | public static RelativeDirection Calculate((double longitude, double latitude, float? e) coordinate1, |
| | 88 | | (double longitude, double latitude, float? e) coordinate2, |
| | 89 | | (double longitude, double latitude, float? e) coordinate3) |
| 0 | 90 | | { |
| 0 | 91 | | var direction = new RelativeDirection(); |
| | 92 | |
|
| | 93 | | const double margin = 65.0; |
| | 94 | | const double straightOn = 10.0; |
| | 95 | | const double turnBack = 5.0; |
| | 96 | |
|
| 0 | 97 | | var angle = Angle(coordinate1, coordinate2, coordinate3).ToDegrees(); |
| | 98 | |
|
| 0 | 99 | | angle = angle.NormalizeDegrees(); |
| | 100 | |
|
| 0 | 101 | | if (angle >= 360 - turnBack || angle < turnBack) |
| 0 | 102 | | { |
| 0 | 103 | | direction.Direction = RelativeDirectionEnum.TurnBack; |
| 0 | 104 | | } |
| 0 | 105 | | else if (angle >= turnBack && angle < 90 - margin) |
| 0 | 106 | | { |
| 0 | 107 | | direction.Direction = RelativeDirectionEnum.SharpRight; |
| 0 | 108 | | } |
| 0 | 109 | | else if (angle >= 90 - margin && angle < 90 + margin) |
| 0 | 110 | | { |
| 0 | 111 | | direction.Direction = RelativeDirectionEnum.Right; |
| 0 | 112 | | } |
| 0 | 113 | | else if (angle >= 90 + margin && angle < 180 - straightOn) |
| 0 | 114 | | { |
| 0 | 115 | | direction.Direction = RelativeDirectionEnum.SlightlyRight; |
| 0 | 116 | | } |
| 0 | 117 | | else if (angle >= 180 - straightOn && angle < 180 + straightOn) |
| 0 | 118 | | { |
| 0 | 119 | | direction.Direction = RelativeDirectionEnum.StraightOn; |
| 0 | 120 | | } |
| 0 | 121 | | else if (angle >= 180 + straightOn && angle < 270 - margin) |
| 0 | 122 | | { |
| 0 | 123 | | direction.Direction = RelativeDirectionEnum.SlightlyLeft; |
| 0 | 124 | | } |
| 0 | 125 | | else if (angle >= 270 - margin && angle < 270 + margin) |
| 0 | 126 | | { |
| 0 | 127 | | direction.Direction = RelativeDirectionEnum.Left; |
| 0 | 128 | | } |
| 0 | 129 | | else if (angle >= 270 + margin && angle < 360 - turnBack) |
| 0 | 130 | | { |
| 0 | 131 | | direction.Direction = RelativeDirectionEnum.SharpLeft; |
| 0 | 132 | | } |
| | 133 | |
|
| 0 | 134 | | direction.Angle = angle; |
| | 135 | |
|
| 0 | 136 | | return direction; |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// Calculates the direction of a segment. |
| | 141 | | /// </summary> |
| | 142 | | public static DirectionEnum Calculate((double longitude, double latitude, float? e) coordinate1, |
| | 143 | | (double longitude, double latitude, float? e) coordinate2) |
| 0 | 144 | | { |
| 0 | 145 | | var angle = (double)Angle((coordinate1.latitude + 0.01f, coordinate1.longitude, null), |
| 0 | 146 | | coordinate1, coordinate2); |
| | 147 | |
|
| 0 | 148 | | angle = angle.ToDegrees(); |
| 0 | 149 | | angle = angle.NormalizeDegrees(); |
| | 150 | |
|
| 0 | 151 | | if (angle < 22.5 || angle >= 360 - 22.5) |
| 0 | 152 | | { // north |
| 0 | 153 | | return DirectionEnum.North; |
| | 154 | | } |
| 0 | 155 | | else if (angle >= 22.5 && angle < 90 - 22.5) |
| 0 | 156 | | { // north-east. |
| 0 | 157 | | return DirectionEnum.NorthWest; |
| | 158 | | } |
| 0 | 159 | | else if (angle >= 90 - 22.5 && angle < 90 + 22.5) |
| 0 | 160 | | { // east. |
| 0 | 161 | | return DirectionEnum.West; |
| | 162 | | } |
| 0 | 163 | | else if (angle >= 90 + 22.5 && angle < 180 - 22.5) |
| 0 | 164 | | { // south-east. |
| 0 | 165 | | return DirectionEnum.SouthWest; |
| | 166 | | } |
| 0 | 167 | | else if (angle >= 180 - 22.5 && angle < 180 + 22.5) |
| 0 | 168 | | { // south |
| 0 | 169 | | return DirectionEnum.South; |
| | 170 | | } |
| 0 | 171 | | else if (angle >= 180 + 22.5 && angle < 270 - 22.5) |
| 0 | 172 | | { // south-west. |
| 0 | 173 | | return DirectionEnum.SouthEast; |
| | 174 | | } |
| 0 | 175 | | else if (angle >= 270 - 22.5 && angle < 270 + 22.5) |
| 0 | 176 | | { // south-west. |
| 0 | 177 | | return DirectionEnum.East; |
| | 178 | | } |
| 0 | 179 | | else if (angle >= 270 + 22.5 && angle < 360 - 22.5) |
| 0 | 180 | | { // south-west. |
| 0 | 181 | | return DirectionEnum.NorthEast; |
| | 182 | | } |
| | 183 | |
|
| 0 | 184 | | return DirectionEnum.North; |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | internal static double ToDegrees(this double radians) |
| 5 | 188 | | { |
| 5 | 189 | | return radians / Math.PI * 180d; |
| 5 | 190 | | } |
| | 191 | |
|
| | 192 | | internal static double NormalizeDegrees(this double degrees) |
| 5 | 193 | | { |
| 5 | 194 | | if (degrees >= 360) |
| 0 | 195 | | { |
| 0 | 196 | | var count = Math.Floor(degrees / 360.0); |
| 0 | 197 | | degrees -= 360.0 * count; |
| 0 | 198 | | } |
| 5 | 199 | | else if (degrees < 0) |
| 0 | 200 | | { |
| 0 | 201 | | var count = Math.Floor(-degrees / 360.0) + 1; |
| 0 | 202 | | degrees += 360.0 * count; |
| 0 | 203 | | } |
| | 204 | |
|
| 5 | 205 | | return degrees; |
| 5 | 206 | | } |
| | 207 | | } |