Skip to content

Commit 83fdbcc

Browse files
committed
Add Vector2IntRange and Vector3IntRange
1 parent dd49bb4 commit 83fdbcc

File tree

7 files changed

+235
-2
lines changed

7 files changed

+235
-2
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Architecture.Structs
4+
{
5+
/// <summary>
6+
/// A range of Vector2Int values.
7+
/// </summary>
8+
[System.Serializable]
9+
public struct Vector2IntRange : INumberRange<Vector2Int>
10+
{
11+
[SerializeField]
12+
[Tooltip("The lower bound of the range.")]
13+
private Vector2Int m_Min;
14+
15+
[SerializeField]
16+
[Tooltip("The upper bound of the range.")]
17+
private Vector2Int m_Max;
18+
19+
/// <inheritdoc/>
20+
public Vector2Int min
21+
{
22+
get => m_Min;
23+
set => m_Min = value;
24+
}
25+
26+
/// <inheritdoc/>
27+
public Vector2Int max
28+
{
29+
get => m_Max;
30+
set => m_Max = value;
31+
}
32+
33+
/// <inheritdoc/>
34+
public Vector2Int delta => max - min;
35+
36+
/// <inheritdoc/>
37+
public Vector2Int median => (min + max) / 2;
38+
39+
/// <summary>
40+
/// Creates a new range with the specified values.
41+
/// </summary>
42+
/// <param name="min">The lower bound of the range.</param>
43+
/// <param name="max">The upper bound of the range.</param>
44+
public Vector2IntRange(Vector2Int min, Vector2Int max)
45+
{
46+
m_Min = min;
47+
m_Max = max;
48+
}
49+
50+
/// <inheritdoc/>
51+
public Vector2Int Random()
52+
{
53+
return new Vector2Int(
54+
UnityEngine.Random.Range(min.x, max.x + 1),
55+
UnityEngine.Random.Range(min.y, max.y + 1));
56+
}
57+
58+
/// <inheritdoc/>
59+
/// <param name="value">The value to check.</param>
60+
public bool Includes(Vector2Int value)
61+
{
62+
return value.x >= min.x && value.x <= max.x &&
63+
value.y >= min.y && value.y <= max.y;
64+
}
65+
66+
/// <inheritdoc/>
67+
/// <param name="value">The value to check.</param>
68+
public bool Includes(Vector2Int value, bool includeMin, bool includeMax)
69+
{
70+
return value.x.IsBetween(min.x, max.x, includeMin, includeMax) &&
71+
value.y.IsBetween(min.y, max.y, includeMin, includeMax);
72+
}
73+
74+
/// <inheritdoc/>
75+
/// <param name="value">The value to clamp.</param>
76+
public Vector2Int Clamp(Vector2Int value)
77+
{
78+
value.x = Mathf.Clamp(value.x, min.x, max.x);
79+
value.y = Mathf.Clamp(value.y, min.y, max.y);
80+
return value;
81+
}
82+
83+
/// <inheritdoc/>
84+
public Vector2Int Lerp(float t)
85+
{
86+
Vector2 lerp = Vector2.Lerp(min, max, t);
87+
return new Vector2Int((int)lerp.x, (int)lerp.y);
88+
}
89+
90+
/// <inheritdoc/>
91+
/// <param name="value">The value within the range you want to calculate.</param>
92+
public float InverseLerp(Vector2Int value)
93+
{
94+
Vector2 AB = max - min;
95+
Vector2 AV = value - min;
96+
97+
return Mathf.Clamp01(Vector2.Dot(AV, AB) / Vector2.Dot(AB, AB));
98+
}
99+
100+
}
101+
102+
}

Runtime/DataStructures/Ranges/Vector2IntRange.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/DataStructures/Ranges/Vector2Range.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public float InverseLerp(Vector2 value)
9292
{
9393
Vector2 AB = max - min;
9494
Vector2 AV = value - min;
95-
return Vector2.Dot(AV, AB) / Vector2.Dot(AB, AB);
95+
96+
return Mathf.Clamp01(Vector2.Dot(AV, AB) / Vector2.Dot(AB, AB));
9697
}
9798

9899
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using UnityEngine;
2+
3+
namespace Zigurous.Architecture.Structs
4+
{
5+
/// <summary>
6+
/// A range of Vector3Int values.
7+
/// </summary>
8+
[System.Serializable]
9+
public struct Vector3IntRange : INumberRange<Vector3Int>
10+
{
11+
[SerializeField]
12+
[Tooltip("The lower bound of the range.")]
13+
private Vector3Int m_Min;
14+
15+
[SerializeField]
16+
[Tooltip("The upper bound of the range.")]
17+
private Vector3Int m_Max;
18+
19+
/// <inheritdoc/>
20+
public Vector3Int min
21+
{
22+
get => m_Min;
23+
set => m_Min = value;
24+
}
25+
26+
/// <inheritdoc/>
27+
public Vector3Int max
28+
{
29+
get => m_Max;
30+
set => m_Max = value;
31+
}
32+
33+
/// <inheritdoc/>
34+
public Vector3Int delta => max - min;
35+
36+
/// <inheritdoc/>
37+
public Vector3Int median => (min + max) / 2;
38+
39+
/// <summary>
40+
/// Creates a new range with the specified values.
41+
/// </summary>
42+
/// <param name="min">The lower bound of the range.</param>
43+
/// <param name="max">The upper bound of the range.</param>
44+
public Vector3IntRange(Vector3Int min, Vector3Int max)
45+
{
46+
m_Min = min;
47+
m_Max = max;
48+
}
49+
50+
/// <inheritdoc/>
51+
public Vector3Int Random()
52+
{
53+
return new Vector3Int(
54+
UnityEngine.Random.Range(min.x, max.x + 1),
55+
UnityEngine.Random.Range(min.y, max.y + 1),
56+
UnityEngine.Random.Range(min.z, max.z + 1));
57+
}
58+
59+
/// <inheritdoc/>
60+
/// <param name="value">The value to check.</param>
61+
public bool Includes(Vector3Int value)
62+
{
63+
return value.x >= min.x && value.x <= max.x &&
64+
value.y >= min.y && value.y <= max.y &&
65+
value.z >= min.z && value.z <= max.z;
66+
}
67+
68+
/// <inheritdoc/>
69+
/// <param name="value">The value to check.</param>
70+
public bool Includes(Vector3Int value, bool includeMin, bool includeMax)
71+
{
72+
return value.x.IsBetween(min.x, max.x, includeMin, includeMax) &&
73+
value.y.IsBetween(min.y, max.y, includeMin, includeMax) &&
74+
value.z.IsBetween(min.z, max.z, includeMin, includeMax);
75+
}
76+
77+
/// <inheritdoc/>
78+
/// <param name="value">The value to clamp.</param>
79+
public Vector3Int Clamp(Vector3Int value)
80+
{
81+
value.x = Mathf.Clamp(value.x, min.x, max.x);
82+
value.y = Mathf.Clamp(value.y, min.y, max.y);
83+
value.z = Mathf.Clamp(value.z, min.z, max.z);
84+
return value;
85+
}
86+
87+
/// <inheritdoc/>
88+
public Vector3Int Lerp(float t)
89+
{
90+
Vector3 lerp = Vector3.Lerp(min, max, t);
91+
return new Vector3Int((int)lerp.x, (int)lerp.y, (int)lerp.z);
92+
}
93+
94+
/// <inheritdoc/>
95+
/// <param name="value">The value within the range you want to calculate.</param>
96+
public float InverseLerp(Vector3Int value)
97+
{
98+
Vector3 AB = max - min;
99+
Vector3 AV = value - min;
100+
101+
return Mathf.Clamp01(Vector3.Dot(AV, AB) / Vector3.Dot(AB, AB));
102+
}
103+
104+
}
105+
106+
}

Runtime/DataStructures/Ranges/Vector3IntRange.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/DataStructures/Ranges/Vector3Range.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ public float InverseLerp(Vector3 value)
9696
{
9797
Vector3 AB = max - min;
9898
Vector3 AV = value - min;
99-
return Vector3.Dot(AV, AB) / Vector3.Dot(AB, AB);
99+
100+
return Mathf.Clamp01(Vector3.Dot(AV, AB) / Vector3.Dot(AB, AB));
100101
}
101102

102103
}

Runtime/DataStructures/Ranges/Vector4Range.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public float InverseLerp(Vector4 value)
100100
{
101101
Vector4 AB = max - min;
102102
Vector4 AV = value - min;
103+
103104
return Vector4.Dot(AV, AB) / Vector4.Dot(AB, AB);
104105
}
105106

0 commit comments

Comments
 (0)