-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramOld.cs
More file actions
677 lines (588 loc) · 24.8 KB
/
ProgramOld.cs
File metadata and controls
677 lines (588 loc) · 24.8 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/* * * ProgramOld.cs * * */
// ORIGINAL FILE for entry point
// This is Flattend SRC_CS from another github repo
using System; // System sauces
using System.Text; // Text stuffs
using System.Collections.Generic; // idk
using System.Runtime.CompilerServices; // Compiler optimization things
namespace OLD;
/// <summary>
/// Represents a geographical location with latitude, longitude, and associated metadata.
/// </summary>
public class Location
{
public enum Unit
{
Degree,
Radian
}
/*
Default latitude 0
Default longitude 0
Default name MyLocation
Default coords unit Degrees
*/
public double Lat = 0;
public double Lon = 0;
public string Name = "";
public Unit LUnit;
private string Symbol = "";
private List<double> Coords = new List<double>();
/// <summary>
/// Initializes a new instance of the Location class with default or specified values.
/// </summary>
/// <param name="Lat">Latitude of the location (default: 0).</param>
/// <param name="Lon">Longitude of the location (default: 0).</param>
/// <param name="Name">Name of the location (default: "MyLocation").</param>
/// <param name="isRadian">Indicates whether the coordinates are in radians (default: false).</param>
public Location(string Name = "MyLocation", double Lat = 0, double Lon = 0, bool isRadian = false)
{
if (isRadian)
{
LUnit = Unit.Radian;
Symbol = Symbols.RAD;
}
else if (!isRadian)
{
LUnit = Unit.Degree;
Symbol = Symbols.DEGREE;
}
this.Lat = Lat;
this.Lon = Lon;
this.Name = Name;
Coords.Add(Lat);
Coords.Add(Lon);
}
/// <summary>
/// Prints the location's details in a human-readable format.
/// </summary>
public void Printer()
{
Console.WriteLine($"{Name} Coords in {LUnit}");
Console.WriteLine($"{Symbols.PHI} = {Lat}{Symbol}");
Console.WriteLine($"{Symbols.LAMBDA} = {Lon}{Symbol}");
}
/// <summary>
/// Converts the location's coordinates to radians.
/// </summary>
/// <param name="SupressWarning">Suppresses warnings if the coordinates are already in radians (default: false).</param>
/// <param name="force">Forces conversion even if the coordinates are already in radians (default: false).</param>
/// <returns>A list containing the converted latitude and longitude in radians.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void toRadian(bool SupressWarning = false, bool force = false)
{
if (LUnit == Unit.Radian)
{
if (!SupressWarning && force)
{
Console.WriteLine("Warning: Already in Radians, but forced conversion is enabled.");
}
else if (!SupressWarning)
{
Console.WriteLine("Warning: Already in Radians.");
// return Coords;
}
}
// Perform the actual conversion
Lat = Lat * Math.PI / 180;
Lon = Lon * Math.PI / 180;
LUnit = Unit.Radian;
Symbol = Symbols.RAD;
Coords.Clear();
Coords.Add(Lat);
Coords.Add(Lon);
}
/// <summary>
/// Converts the location's coordinates to degrees.
/// </summary>
/// <param name="SupressWarning">Suppresses warnings if the coordinates are already in degrees (default: false).</param>
/// <param name="Force">Forces conversion even if the coordinates are already in degrees (default: false).</param>
/// <returns>A list containing the converted latitude and longitude in degrees.</returns>
///
/// TODO: Make C code för this?
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToDegree(bool SupressWarning = false, bool Force = false)
{
if (LUnit == Unit.Degree)
{
if (!SupressWarning && Force)
{
Console.WriteLine("Warning: Already in Degrees, but forced conversion is enabled.");
}
else if (!SupressWarning)
{
Console.WriteLine("Warning: Already in Degrees.");
// return Coords;
}
}
// Perform the actual conversion
Lat = Lat * Math.PI / 180;
Lon = Lon * Math.PI / 180;
LUnit = Unit.Degree;
Symbol = Symbols.DEGREE;
Coords.Clear();
Coords.Add(Lat);
Coords.Add(Lon);
}
/// <summary>
/// Retrieves the current coordinates of the location.
/// </summary>
/// <returns>A list containing the latitude and longitude.</returns>
public List<double> GetCoords()
{
return Coords;
}
public Unit GetUnit()
{
return LUnit;
}
};
/// <summary>
/// Provides methods to calculate the haversine function used in geographical distance calculations.
/// Includes support for both radians and degrees, with optional verbose output for debugging.
/// </summary>
public class Haversine
{
/// <summary>
/// Converts an angle from degrees to radians.
/// </summary>
/// <param name="deg">Angle in degrees.</param>
/// <param name="Printing">Optional. If true, prints intermediate values to console.</param>
/// <returns>The angle converted to radians.</returns>
///
/// TODO: Make C code för this
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double deg2rad(double deg, bool Printing = false)
{
double var = Math.PI / 180 * deg;
if (Printing)
{
Console.WriteLine($"deg = {deg}");
Console.WriteLine($"rad = {var}");
}
return var;
}
/// <summary>
/// Computes the haversine of an angle given in radians.
/// </summary>
/// <param name="x">Angle in radians.</param>
/// <param name="Printing">Optional. If true, logs intermediate steps to console.</param>
/// <returns>Haversine value (double).</returns>
///
/// TODO: Make C code för this
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Hav_rad(double x, bool Printing = false)
{
double Cos = 1 - Math.Cos(x);
double hHav = Cos / 2;
if (Printing)
{
Console.WriteLine($"~! x = {x}{Symbols.RAD}");
Console.WriteLine($"~! cos(x) = {Math.Cos(x)}");
Console.WriteLine($"~! 1-cos(x) = {Cos}");
Console.WriteLine($"~! Hav(x) = {hHav}");
}
return hHav;
}
/// <summary>
/// Computes the haversine of an angle given in degrees.
/// Internally converts degrees to radians before calculation.
/// </summary>
/// <param name="x">Angle in degrees.</param>
/// <param name="Printing">Optional. If true, logs intermediate steps to console.</param>
/// <returns>Haversine value (double).</returns>
///
/// TODO: Make C code för this
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Hav_deg(double x, bool Printing = false)
{
// radian-ize the Angle
// because C# expect angle in radian
x = deg2rad(x);
double Cos = 1 - Math.Cos(x);
double hHav = Cos / 2;
if (Printing)
{
Console.WriteLine($"~! x = {x}{Symbols.RAD}");
Console.WriteLine($"~! cos(x) = {Math.Cos(x)}");
Console.WriteLine($"~! 1-cos(x) = {Cos}");
Console.WriteLine($"~! Hav(x) = {hHav}");
}
return hHav;
}
/// <summary>
/// Generic haversine function that accepts input in either radians or degrees.
/// </summary>
/// <param name="x">Input angle (radians or degrees depending on flag).</param>
/// <param name="isRadian">If true, assumes input is in radians; otherwise, treats it as degrees.</param>
/// <param name="Printing">Optional. If true, logs intermediate steps to console.</param>
/// <returns>Haversine value (double).</returns>
///
/// TODO: Make C code för this
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static double Hav(double x, bool isRadian = false, bool Printing = false)
{
if (isRadian) { }
else
{
x = deg2rad(x);
}
double Cos = 1 - Math.Cos(x);
double hHav = Cos / 2;
if (Printing)
{
Console.WriteLine($"~ x = {x} {Symbols.RAD}");
Console.WriteLine($"~ cos(x) = {Math.Cos(x)}");
Console.WriteLine($"~ 1-cos(x) = {Cos}");
Console.WriteLine($"~ Hav(x) = {hHav}");
}
return hHav;
}
}
/// <summary>
/// Provides Math symbols and unit symbols
/// </summary>
public static class Symbols
{
// Greek Alphabets
public const string DELTA = "\u0394"; // Δ
public const string THETA = "\u03b8"; // θ
public const string PHI = "\u03d5"; // π
public const string LAMBDA = "\u03bb"; // λ
// Greek Alphabets Colored
public const string C_DELTA = "\x1b[38;2;115;192;105m\u0394\x1b[0m"; // Color: 73c069
public const string C_THETA = "\x1b[38;2;255;138;70m\u03b8\x1b[0m"; // Color: ff8a46
public const string C_PHI = "\x1b[38;2;16;150;150m\u03d5\x1b[0m"; // Color: 109696
public const string C_LAMBDA = "\x1b[38;2;223;196;125m\u03bb\x1b[0m"; // Color: dfc47d
// Math symbols
public const string DEGREE = "\u00b0"; // °
public const string RAD = " RAD"; // RAD
public const string SQRT = "\u221a"; // √
public const string APRX = "\u2248"; // ≈
// Subscripts
public const string SB1 = "\u2081"; // ₁
public const string SB2 = "\u2082"; // ₂
};
/// <summary>
/// Provides utility methods for string manipulation and console formatting.
/// Includes functions to repeat strings, center text in the terminal, and more.
/// </summary>
public static class Misc
{
/// <summary>
/// Gets the current width of the console window.
/// Can be used as a reference for aligning or centering output.
/// </summary>
public static int TerminalSize = Console.WindowWidth;
/// <summary>
/// Repeats a given string or character a specified number of times.
/// Or in other words, mimics the string multiplication from Python in C#
/// </summary>
/// <param name="str">The string or character to repeat.</param>
/// <param name="repetitions">Number of times to repeat (default: 1).</param>
/// <returns>A single string with the input repeated.</returns>
/// <example>
/// <code>
/// string result = Misc.Repeater("hello", 3); // Returns "hellohellohello"
/// </code>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Repeater(object str, int repetitions = 1)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < repetitions; i++)
{
sb.Append(str?.ToString());
}
return sb.ToString();
}
/// <summary>
/// Centers a given text within the terminal window, bordered by a repeating character.
/// Useful for creating headers or separators in console applications.
/// </summary>
/// <param name="Text">The text to center (default: "Hello").</param>
/// <param name="Char">Character used to fill borders (default: '=').</param>
/// <param name="offset">Adds extra space to simulate padding or margin (default: 2).</param>
/// <param name="Printing">If true, prints the result to the console (default: false).</param>
/// <returns>The formatted centered string.</returns>
/// <example>
/// <code>
/// Misc.PrintMid("Welcome", '-', offset: 4, Printing: true);
/// // Output:
/// // ------------------[Welcome]-------------------
/// </code>
/// </example>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string PrintMid(string Text = "Hello", char Char = '=', int offset = 2, char LeftBorder = '[', char RightBorder = ']', bool Printing = false)
{
int ConsoleWidth = TerminalSize + offset; // Get width + offset
int Border_sz = (ConsoleWidth - Text.Length - 4) / 2; // Subtract the length of the text and some spacing ([Text] takes up 4 characters).
// Divide the remaining space equally between left and right borders.
// If ConsoleWidth = 80, Text.Length = 5 → Remaining space = 80 - 5 - 4 = 71
// Each border gets 71 / 2 = 35 characters.
// Build Left & Right borders
string Left = Repeater(Char, Border_sz); // Left side uses exactly Border_sz characters.
string Right = Repeater(Char, Border_sz + (Text.Length % 2)); // Right side adjusts if the text has an odd number of characters,
// so total width stays balanced.
// Final assembly
string Content = $"{Left}{LeftBorder}{Text}{RightBorder}{Right}";
if (Printing)
{
Console.WriteLine(Content);
}
return Content;
}
}
/// <summary>
/// Provides string coloring functions
/// </summary>
public class ColorTx
{
public enum Position { Back, Fore }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ColorStr(string text = "Hello, world!", uint hex = 0xFF109696, Position pos = Position.Fore)
{
// Parse RGB from 0xAARRGGBB or 0xRRGGBB
byte a = 255, r, g, b;
if (hex > 0xFFFFFF)
{
a = (byte)((hex >> 24) & 0xFF);
r = (byte)((hex >> 16) & 0xFF);
g = (byte)((hex >> 8) & 0xFF);
b = (byte)(hex & 0xFF);
}
else
{
r = (byte)((hex >> 16) & 0xFF);
g = (byte)((hex >> 8) & 0xFF);
b = (byte)(hex & 0xFF);
}
// Convert RGB to closest ConsoleColor
ConsoleColor color = RgbToConsoleColor(r, g, b);
if (pos == Position.Fore)
Console.ForegroundColor = color;
else
Console.BackgroundColor = color;
return text;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Print(uint hex, Position pos, string text)
{
Console.WriteLine(ColorStr(text, hex, pos));
Console.ResetColor(); // Don't leave your terminal cursed
}
// Kinda not useful
// TODO: Make C code för this
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Debug(string text)
{
Console.Write("DEBUG RAW: ");
foreach (char c in text)
{
if (!char.IsControl(c) && !char.IsWhiteSpace(c))
{
Console.Write(c);
}
else
{
Console.Write($"\\x{((int)c):X2}");
}
}
Console.WriteLine();
}
private static ConsoleColor RgbToConsoleColor(byte r, byte g, byte b)
{
// Naive RGB -> ConsoleColor mapping
int index = (r > 128 ? 4 : 0) + (g > 128 ? 2 : 0) + (b > 128 ? 1 : 0);
return (ConsoleColor)index;
}
}
/// <summary>
/// Provides distance functions for both radians and degrees,
/// with optional verbose output for debugging.
/// </summary>
public class Distance
{
public static int R = 6371;
public class Distance_2D
{
/*
based on Wikipedia article (With Printing)
https://en.wikipedia.org/wiki/Haversine_formula
formula:
hav(θ°) = hav(Δφ) + cos(φ₁) * cos(φ₂) * hav(Δλ)
θ° = 2 * archav(θ)
d = R * θ°
hav(x) = sin²(x/2) = (1 - cos(x))/2
archav(θ) = 2 * arcsin(√(θ)) = 2 * arctan2(√(θ), √(1-θ))
*/
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Distance_Deg(Location A, Location B)
{
// Print Degree coordinates
Console.WriteLine($"Coords in Degrees");
// Latitudes
double lat1 = A.Lat;
double lon1 = A.Lon;
Console.WriteLine($"{Symbols.PHI}{Symbols.SB1} = {lat1}{Symbols.DEGREE}");
Console.WriteLine($"{Symbols.LAMBDA}{Symbols.SB1} = {lon1}{Symbols.DEGREE}\n");
// Longitudes
double lat2 = B.Lat;
double lon2 = B.Lon;
Console.WriteLine($"{Symbols.PHI}{Symbols.SB2} = {lat2}{Symbols.DEGREE}");
Console.WriteLine($"{Symbols.LAMBDA}{Symbols.SB2} = {lon2}{Symbols.DEGREE}\n~~~");
// Calculate Deltas
// Delta phi
double Dlat = lat2 - lat1; /// TODO: Make C code för this
Console.WriteLine($"{Symbols.DELTA}{Symbols.PHI} = {Symbols.PHI}{Symbols.SB2} - {Symbols.PHI}{Symbols.SB1}");
Console.WriteLine($"{Symbols.DELTA}{Symbols.PHI} = {lat2} - {lat1}");
Console.WriteLine($"{Symbols.DELTA}{Symbols.PHI} = {Dlat}\n");
// Delta lambda
double Dlon = lon2 - lon1; /// TODO: Make C code för this
Console.WriteLine($"{Symbols.DELTA}{Symbols.LAMBDA} = {Symbols.LAMBDA}{Symbols.SB2} - {Symbols.LAMBDA}{Symbols.SB1}");
Console.WriteLine($"{Symbols.DELTA}{Symbols.LAMBDA} = {lon2} - {lon1}");
Console.WriteLine($"{Symbols.DELTA}{Symbols.LAMBDA} = {Dlon}\n~~~");
/*
Convert to Radians and Build hav(θ) formula
*/
Console.WriteLine($"~ Hav({Symbols.DELTA}{Symbols.PHI})");
Console.WriteLine($"~ Hav({Dlat}{Symbols.DEGREE})");
double hav1 = Haversine.Hav_deg(Dlat, Printing: true); /// TODO: Make C code för this
Console.WriteLine($"\n~ Hav({Symbols.DELTA}{Symbols.LAMBDA})");
Console.WriteLine($"~ Hav({Dlon}{Symbols.DEGREE})");
double hav2 = Haversine.Hav_deg(Dlon, Printing: true); /// TODO: Make C code för this
double cos1 = Math.Cos(Haversine.deg2rad(lat1));
double cos2 = Math.Cos(Haversine.deg2rad(lat2));
/// TODO: Make C code för this {
// Plug everything
double Hav = hav1 + cos1 * cos2 * hav2;
// Find theta
double T = 2 * Math.Asin(Math.Sqrt(Hav));
// Find d with theta
double d = R * T;
Console.WriteLine($"\nhav({Symbols.THETA}) = hav({Symbols.DELTA}{Symbols.PHI}) + cos({Symbols.PHI}{Symbols.SB1}) * cos({Symbols.PHI}{Symbols.SB2}) * hav({Symbols.DELTA}{Symbols.LAMBDA})");
Console.WriteLine($"hav({Symbols.THETA}) = hav({Dlat}) + cos({lat1}) * cos({lat2}) * hav({Dlon})");
Console.WriteLine($"hav({Symbols.THETA}) = {hav1} + {cos1} * {cos2} * {hav2}");
Console.WriteLine($"hav({Symbols.THETA}) = {Hav}");
Console.WriteLine($"{Symbols.THETA} = 2 * archav({Symbols.SQRT}(1 - hav)))");
Console.WriteLine($"{Symbols.THETA} = 2 * arcsin({Symbols.SQRT}({Hav})");
Console.WriteLine($"{Symbols.THETA} = {T}");
Console.WriteLine($"d = R * θ{Symbols.RAD}");
Console.WriteLine($"d = {R} * {T}");
Console.WriteLine($"d {Symbols.APRX} {d} KM");
Console.WriteLine($"d {Symbols.APRX} {Math.Round(d, 2)} KM");
return d;
}
/*
based on Radian formula (With Printing)
hav(θ) = hav(Δφ) + cos(φ₁) * cos(φ₂) * hav(Δλ)
θ = 2 * arctan2(√(θ), √(1-θ))
d = R * θ
hav(x) = sin²(x/2) = (1 - cos(x))/2
*/
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public static double Distance_Rad(Location A, Location B)
{
// Print Radian coordinates
Console.WriteLine("Coords in Radians");
// Latitudes
double lat1 = A.Lat;
double lon1 = A.Lon;
Console.WriteLine($"{Symbols.PHI}{Symbols.SB1} = {lat1}{Symbols.RAD}");
Console.WriteLine($"{Symbols.LAMBDA}{Symbols.SB1} = {lon1}{Symbols.RAD}\n");
// Longitudes
double lat2 = B.Lat;
double lon2 = B.Lon;
Console.WriteLine($"{Symbols.PHI}{Symbols.SB2} = {lat2}{Symbols.RAD}");
Console.WriteLine($"{Symbols.LAMBDA}{Symbols.SB2} = {lon2}{Symbols.RAD}\n~~~");
// Calculate Deltas
// Delta phi
double Dlat = lat2 - lat1;
Console.WriteLine($"{Symbols.DELTA}{Symbols.PHI} = {Symbols.PHI}{Symbols.SB2} - {Symbols.PHI}{Symbols.SB1}");
Console.WriteLine($"{Symbols.DELTA}{Symbols.PHI} = {lat2} - {lat1}");
Console.WriteLine($"{Symbols.DELTA}{Symbols.PHI} = {Dlat}\n");
// Delta lambda
double Dlon = lon2 - lon1;
Console.WriteLine($"{Symbols.DELTA}{Symbols.LAMBDA} = {Symbols.LAMBDA}{Symbols.SB2} - {Symbols.LAMBDA}{Symbols.SB1}");
Console.WriteLine($"{Symbols.DELTA}{Symbols.LAMBDA} = {lon2} - {lon1}");
Console.WriteLine($"{Symbols.DELTA}{Symbols.LAMBDA} = {Dlon}\n~~~");
// Build hav(θ) formula
Console.WriteLine($"~ Hav({Symbols.DELTA}{Symbols.PHI})");
Console.WriteLine($"~ Hav({Dlat})");
double hav1 = Haversine.Hav_rad(Dlat, Printing: true);
Console.WriteLine($"\n~ Hav({Symbols.DELTA}{Symbols.LAMBDA})");
Console.WriteLine($"~ Hav({Dlon})");
double hav2 = Haversine.Hav_rad(Dlon, Printing: true);
double cos1 = Math.Cos(lat1);
double cos2 = Math.Cos(lat2);
/// TODO: Make C code för this
// Plug everything
double Hav = hav1 + cos1 * cos2 * hav2;
// Find theta
double T = 2 * Math.Atan2(Math.Sqrt(Hav), Math.Sqrt(1 - Hav));
// Find d with theta
double d = R * T;
Console.WriteLine($"\nhav({Symbols.THETA}) = hav({Symbols.DELTA}{Symbols.PHI}) + cos({Symbols.PHI}{Symbols.SB1}) * cos({Symbols.PHI}{Symbols.SB2}) * hav({Symbols.DELTA}{Symbols.LAMBDA})");
Console.WriteLine($"hav({Symbols.THETA}) = hav({Dlat}) + cos({lat1}) * cos({lat2}) * hav({Dlon})");
Console.WriteLine($"hav({Symbols.THETA}) = {hav1} + {cos1} * {cos2} * {hav2}");
Console.WriteLine($"hav({Symbols.THETA}) = {Hav}\n");
Console.WriteLine($"{Symbols.THETA} = 2 * archav({Symbols.THETA})");
Console.WriteLine($"{Symbols.THETA} = 2 * arctan2({Symbols.SQRT}({Symbols.THETA}), {Symbols.SQRT}(1 - {Symbols.THETA}))");
Console.WriteLine($"{Symbols.THETA} = 2 * arctan2({Symbols.SQRT}({Hav}), {Symbols.SQRT}({1 - Hav}))");
Console.WriteLine($"{Symbols.THETA} = {T}\n");
Console.WriteLine($"d = R * θ{Symbols.RAD}");
Console.WriteLine($"d = {R} * {T}");
Console.WriteLine($"d {Symbols.APRX} {d} KM");
Console.WriteLine($"d {Symbols.APRX} {Math.Round(d, 2)} KM");
return d;
}
/// TODO: Make C code för this
[MethodImpl(MethodImplOptions.AggressiveOptimization | MethodImplOptions.AggressiveInlining)]
public static double Distance(Location A, Location B, bool IsRadian = false)
{
double lat1, lon1;
double lat2, lon2;
double Dlat;
double Dlon;
double hav1;
double hav2;
double cos1;
double cos2;
double Hav, T, d;
if (!IsRadian)
{
lat1 = A.Lat;
lon1 = A.Lon;
lat2 = B.Lat;
lon2 = B.Lon;
Dlat = lat2 - lat1;
Dlon = lon2 - lon1;
hav1 = Haversine.Hav_deg(Dlat);
hav2 = Haversine.Hav_deg(Dlon);
cos1 = Math.Cos(Haversine.deg2rad(lat1));
cos2 = Math.Cos(Haversine.deg2rad(lat2));
Hav = hav1 + cos1 * cos2 * hav2;
T = 2 * Math.Asin(Math.Sqrt(Hav));
d = R * T;
return d;
}
else
{
lat1 = A.Lat;
lon1 = A.Lon;
lat2 = B.Lat;
lon2 = B.Lon;
Dlat = lat2 - lat1;
Dlon = lon2 - lon1;
hav1 = Haversine.Hav_rad(Dlat);
hav2 = Haversine.Hav_rad(Dlon);
cos1 = Math.Cos(lat1);
cos2 = Math.Cos(lat2);
Hav = hav1 + cos1 * cos2 * hav2;
T = 2 * Math.Atan2(Math.Sqrt(Hav), Math.Sqrt(1 - Hav));
d = R * T;
return d;
}
}
}
}
/* * * End ProgramOld.cs * * */