-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralMath.cs
More file actions
220 lines (197 loc) · 8.36 KB
/
GeneralMath.cs
File metadata and controls
220 lines (197 loc) · 8.36 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
using System;
using System.Collections.Generic;
using System.Text;
namespace Pulsar4X.Orbital
{
/// <summary>
/// Just a container for some general math functions.
/// </summary>
public class GeneralMath
{
/// <summary>
/// Clamps a value between the provided man and max.
/// </summary>
public static double Clamp(double value, double min, double max)
{
if (value > max)
return max;
if (value < min)
return min;
return value;
}
public static double Clamp(double value, MinMaxStruct minMax)
{
return Clamp(value, minMax.Min, minMax.Max);
}
/// <summary>
/// Linearly interpolates between two values.
/// </summary>
public static double Lerp(MinMaxStruct minMax, double selection)
{
return minMax.Min + selection * (minMax.Max - minMax.Min);
}
/// <summary>
/// Linearly interpolates between two values.
/// </summary>
public static double Lerp(double min, double max, double selection)
{
return min + selection * (max - min);
}
/// <summary>
/// Calculates where the value falls inside the MinMaxStruct.
/// </summary>
/// <returns>Value's percent in the MinMaxStruct (Ranged from 0.0 to 1.0)</returns>
public static double GetPercentage(double value, MinMaxStruct minMax)
{
return GetPercentage(value, minMax.Min, minMax.Max);
}
/// <summary>
/// Calculates where the value falls between the min and max.
/// </summary>
/// <returns>Value's percent in the MinMaxStruct (Ranged from 0.0 to 1.0)</returns>
public static double GetPercentage(double value, double min, double max)
{
if (min >= max)
{
throw new ArgumentOutOfRangeException("min", "Min value must be less than Max value.");
}
double adjustedMax = max - min;
double adjustedValue = value - min;
return adjustedValue / adjustedMax;
}
/// <summary>
/// Returns the gravitational attraction between two masses.
/// </summary>
/// <param name="mass1">Mass of first body. (KG)</param>
/// <param name="mass2">Mass of second body. (KG)</param>
/// <param name="distance">Distance between bodies. (M)</param>
/// <returns>Force (Newtons)</returns>
public static double GetGravitationalAttraction(double mass1, double mass2, double distance)
{
// http://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation
return UniversalConstants.Science.GravitationalConstant * mass1 * mass2 / (distance * distance);
}
/// <summary>
/// Returns the gravitational attraction of a body at a specified distance.
/// </summary>
/// <param name="mass">Mass of the body. (KG)</param>
/// <param name="distance">Distance to the body. (M)</param>
/// <returns>Force (Newtons)</returns>
public static double GetStandardGravitationAttraction(double mass, double distance)
{
return GetGravitationalAttraction(mass, 1, distance);
}
/// <summary>
/// Standard Gravitational parameter. in m^3 s^-2
/// </summary>
/// <returns>The gravitational parameter.</returns>
/// <param name="mass">Mass.</param>
public static double StandardGravitationalParameter(double mass) =>
mass * UniversalConstants.Science.GravitationalConstant;
/// <summary>
/// Standard Gravitational parameter. in m^3 s^-2
/// </summary>
/// <returns>The gravitational parameter.</returns>
/// <param name="mass1">Mass 1.</param>
/// <param name="mass2">Mass 2.</param>
public static double StandardGravitationalParameter(double mass1, double mass2) =>
StandardGravitationalParameter(mass1 + mass2);
/// <summary>
/// calculates a vector from two positions and a magnatude
/// </summary>
/// <returns>The vector.</returns>
/// <param name="currentPosition">Current position.</param>
/// <param name="targetPosition">Target position.</param>
/// <param name="speedMagnitude_AU">Speed magnitude.</param>
public static Vector3 GetVector(Vector3 currentPosition, Vector3 targetPosition, double speedMagnitude_AU)
{
Vector3 direction = targetPosition - currentPosition;
double length = direction.Length(); // Distance between targets in AU
return (length != 0) ? direction *speedMagnitude_AU / length : Vector3.Zero;
}
public static bool LineIntersectsLine(Vector2 l1start, Vector2 l1End, Vector2 l2Start, Vector2 l2End, out Vector2 intersectsAt)
{
// calculate the direction of the lines
var uA =
((l2End.X-l2Start.X)*(l1start.Y-l2Start.Y) - (l2End.Y-l2Start.Y)*(l1start.X-l2Start.X)) /
((l2End.Y-l2Start.Y)*(l1End.X-l1start.X) - (l2End.X-l2Start.X)*(l1End.Y-l1start.Y));
var uB =
((l1End.X-l1start.X)*(l1start.Y-l2Start.Y) - (l1End.Y-l1start.Y)*(l1start.X-l2Start.X)) /
((l2End.Y-l2Start.Y)*(l1End.X-l1start.X) - (l2End.X-l2Start.X)*(l1End.Y-l1start.Y));
// if uA and uB are between 0-1, lines are colliding
if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
double intersectionX = l1start.X + (uA * (l1End.X-l1start.X));
double intersectionY = l1start.Y + (uA * (l1End.Y-l1start.Y));
intersectsAt = new Vector2(intersectionX, intersectionY);
return true;
}
intersectsAt = new Vector2();
return false;
}
/// <summary>
/// this is incomplete, needs to out two vectors since a rectangle and line will have two points of intercetption
/// assuming the start and end are outside the rectangle.
/// </summary>
/// <param name="l1start"></param>
/// <param name="l1End"></param>
/// <param name="topLeft"></param>
/// <param name="bottomRight"></param>
/// <param name="intersectsAt"></param>
/// <returns></returns>
public static bool LineIntersectsRectangle(Vector2 l1start, Vector2 l1End, Vector2 topLeft, Vector2 bottomRight, out Vector2 intersectsAt)
{
var tr = new Vector2(bottomRight.X, topLeft.Y);
var bl = new Vector2(topLeft.X, bottomRight.Y);
bool intersects = false;
//left
if (GeneralMath.LineIntersectsLine(l1start, l1End, topLeft, bl, out intersectsAt))
{
intersects = true;
}
//right
if (GeneralMath.LineIntersectsLine(l1start, l1End, tr, bottomRight, out intersectsAt))
{
intersects = true;
}
//top
if (GeneralMath.LineIntersectsLine(l1start,l1End,topLeft, tr, out intersectsAt))
{
intersects = true;
}
//bottom
if (GeneralMath.LineIntersectsLine(l1start,l1End,bl, bottomRight, out intersectsAt))
{
intersects = true;
}
return intersects;
}
/// <summary>
/// A decimal Sqrt. not as fast as normal Math.Sqrt, but better precision.
/// </summary>
/// <returns>The sqrt. of x</returns>
/// <param name="x">x</param>
/// <param name="guess">normaly ignored, this is for the recursion</param>
public static decimal Sqrt(decimal x, decimal? guess = null)
{
var ourGuess = guess.GetValueOrDefault(x / 2m);
var result = x / ourGuess;
var average = (ourGuess + result) / 2m;
if (average == ourGuess) // This checks for the maximum precision possible with a decimal.
return average;
else
return Sqrt(x, average);
}
}
/// <summary>
/// Small helper struct to make all these min/max dicts. nicer.
/// </summary>
public struct MinMaxStruct
{
public double Min, Max;
public MinMaxStruct(double min, double max)
{
Min = min;
Max = max;
}
}
}