forked from ToadsworthLP/desktoptale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathUtilities.cs
67 lines (57 loc) · 2.31 KB
/
MathUtilities.cs
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
using System;
using Microsoft.Xna.Framework;
namespace Desktoptale
{
public class MathUtilities
{
public static float Min(float a, float b)
{
return a < b ? a : b;
}
public static float Clamp(float value, float min, float max)
{
if (value < min)
value = min;
else if (value > max)
value = max;
return value;
}
public static float InterpolateCubicGetSlowerTowardsEnd(float a, float b, float t)
{
float oneMinusT = 1 - t;
float progress = 1 - oneMinusT * oneMinusT * oneMinusT;
return MathHelper.Lerp(a, b, progress);
}
public static float InterpolateQuadraticGetSlowerTowardsEnd(float a, float b, float t)
{
float oneMinusT = 1 - t;
float progress = 1 - oneMinusT * oneMinusT * oneMinusT * oneMinusT;
return MathHelper.Lerp(a, b, progress);
}
// Adapted from https://stackoverflow.com/questions/52004232/how-to-calculate-the-distance-from-a-point-to-the-nearest-point-of-a-rectange
public static double DistanceToNearestPointOnRectangle(Rectangle rectangle, Point point)
{
int dTop = Math.Abs(rectangle.Top - point.Y);
int dBottom = Math.Abs(rectangle.Bottom - point.Y);
int dLeft = Math.Abs(rectangle.Left - point.X);
int dRight = Math.Abs(rectangle.Right - point.X);
if ((rectangle.Left <= point.X && point.X <= rectangle.Right) || (rectangle.Bottom <= point.Y && point.Y <= rectangle.Top))
{
return Math.Min(dTop, Math.Min(dBottom, Math.Min(dLeft, dRight)));
}
else
{
int cornerY = dTop < dBottom ? rectangle.Top : rectangle.Bottom;
int cornerX = dLeft < dRight ? rectangle.Left : rectangle.Right;
int dCx = cornerX - point.X;
int dCy = cornerY - point.Y;
double dCorner = Math.Sqrt(dCx * dCx + dCy * dCy);
return dCorner;
}
}
public static float SignedAngleBetween(Vector2 v, Vector2 w)
{
return (float)Math.Atan2((w.Y * v.X) - (w.X * v.Y), (w.X * v.X) + (w.Y * v.Y));
}
}
}