-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMathHelpers.cs
More file actions
44 lines (39 loc) · 1.44 KB
/
MathHelpers.cs
File metadata and controls
44 lines (39 loc) · 1.44 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
// MIT License - Copyright (c) 2024 wallstop
// Full license text: https://github.com/wallstop/unity-helpers/blob/main/LICENSE
namespace WallstopStudios.UnityHelpers.Core.Helper
{
using UnityEngine;
/// <summary>
/// Math helpers for common geometric conversions and tests.
/// </summary>
public static partial class Helpers
{
/// <summary>
/// Determines whether a point lies to the left of the ray from <paramref name="a"/> to <paramref name="b"/>.
/// </summary>
/// <remarks>
/// Returns false when on or to the right of the ray.
/// </remarks>
public static bool IsLeft(Vector2 a, Vector2 b, Vector2 point)
{
// https://alienryderflex.com/point_left_of_ray/
//check which side of line AB the point P is on
float cross = (b.x - a.x) * (point.y - a.y) - (point.x - a.x) * (b.y - a.y);
return cross > 0f;
}
/// <summary>
/// Converts radians to a unit <see cref="Vector2"/>.
/// </summary>
public static Vector2 RadianToVector2(float radian)
{
return new Vector2(Mathf.Cos(radian), Mathf.Sin(radian));
}
/// <summary>
/// Converts degrees to a unit <see cref="Vector2"/>.
/// </summary>
public static Vector2 DegreeToVector2(float degree)
{
return RadianToVector2(degree * Mathf.Deg2Rad);
}
}
}