-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCoordinateDelta.cs
More file actions
72 lines (62 loc) · 2.95 KB
/
CoordinateDelta.cs
File metadata and controls
72 lines (62 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// Copyright © Pete Sramek. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace PolylineAlgorithm.Internal;
using System.Diagnostics;
using System.Runtime.InteropServices;
/// <summary>
/// Represents the difference (delta) in latitude and longitude between consecutive geographic coordinates.
/// </summary>
/// <remarks>
/// This struct computes and stores the change in coordinate values as integer deltas between successive coordinates.
/// </remarks>
[DebuggerDisplay("{ToString(),nq}")]
[StructLayout(LayoutKind.Auto)]
internal struct CoordinateDelta {
private (int Latitude, int Longitude) _current;
/// <summary>
/// Initializes a new instance of the <see cref="CoordinateDelta"/> struct with the default latitude and longitude deltas.
/// </summary>
public CoordinateDelta() {
_current = (default, default);
}
/// <summary>
/// Gets the current delta in latitude between the most recent and previous coordinate.
/// </summary>
public int Latitude { get; private set; }
/// <summary>
/// Gets the current delta in longitude between the most recent and previous coordinate.
/// </summary>
public int Longitude { get; private set; }
/// <summary>
/// Updates the delta values based on the next latitude and longitude, and sets the current coordinate as next delta baseline.
/// </summary>
/// <param name="latitude">The next latitude value.</param>
/// <param name="longitude">The next longitude value.</param>
public void Next(int latitude, int longitude) {
Latitude = Delta(_current.Latitude, latitude);
Longitude = Delta(_current.Longitude, longitude);
_current.Latitude = latitude;
_current.Longitude = longitude;
}
/// <summary>
/// Calculates the delta between two coordinate values.
/// </summary>
/// <remarks>
/// This method computes the difference between two integer coordinate values, handling cases where the values may be positive or negative.
/// </remarks>
/// <param name="initial">The previous coordinate value.</param>
/// <param name="next">The next coordinate value.</param>
/// <returns>The computed delta between <paramref name="initial"/> and <paramref name="next"/>.</returns>
private static int Delta(int initial, int next) => next - initial;
/// <summary>
/// Returns a string representation of the current coordinate delta.
/// </summary>
/// <returns>
/// A string in the format <c>{ Coordinate: { Latitude: [int], Longitude: [int] }, Delta: { Latitude: [int], Longitude: [int] } }</c> representing the current coordinate and deltas to previous coordinate.
/// </returns>
public override readonly string ToString() =>
$"{{ Coordinate: {{ Latitude: {_current.Latitude}, Longitude: {_current.Longitude} }}, " +
$"Delta: {{ Latitude: {Latitude}, Longitude: {Longitude} }} }}";
}