1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-25 01:39:23 +01:00

added some more utility functions for directions and vectors

This commit is contained in:
Ellpeck 2020-08-10 02:16:35 +02:00
parent 3a1b73934b
commit 52443bfa68
3 changed files with 38 additions and 0 deletions

View file

@ -59,6 +59,21 @@ namespace MLEM.Extensions {
return new Vector4(vec.X.Floor(), vec.Y.Floor(), vec.Z.Floor(), vec.W.Floor());
}
/// <inheritdoc cref="Math.Ceiling(decimal)"/>
public static Vector2 CeilCopy(this Vector2 vec) {
return new Vector2(vec.X.Ceil(), vec.Y.Ceil());
}
/// <inheritdoc cref="Math.Ceiling(decimal)"/>
public static Vector3 CeilCopy(this Vector3 vec) {
return new Vector3(vec.X.Ceil(), vec.Y.Ceil(), vec.Z.Ceil());
}
/// <inheritdoc cref="Math.Ceiling(decimal)"/>
public static Vector4 CeilCopy(this Vector4 vec) {
return new Vector4(vec.X.Ceil(), vec.Y.Ceil(), vec.Z.Ceil(), vec.W.Ceil());
}
/// <summary>
/// Multiplies a point by a given scalar.
/// </summary>
@ -184,5 +199,6 @@ namespace MLEM.Extensions {
matrix.M31 / scZ, matrix.M32 / scZ, matrix.M33 / scZ, 0,
0, 0, 0, 1));
}
}
}

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using MLEM.Extensions;
namespace MLEM.Misc {
/// <summary>
@ -229,5 +230,19 @@ namespace MLEM.Misc {
}
}
/// <summary>
/// Returns the <see cref="Direction2"/> that is closest to the given position's facing direction.
/// </summary>
/// <param name="offset">The vector whose corresponding direction to get</param>
/// <returns>The vector's direction</returns>
public static Direction2 ToDirection(this Vector2 offset) {
var offsetAngle = (float) Math.Atan2(offset.Y, offset.X);
foreach (var dir in AllExceptNone) {
if (Math.Abs(dir.Angle() - offsetAngle) <= MathHelper.PiOver4 / 2)
return dir;
}
return Direction2.None;
}
}
}

View file

@ -100,6 +100,13 @@ namespace Sandbox {
};
Console.WriteLine(obj);
for (var i = 0; i < 360; i += 45) {
var rad = MathHelper.ToRadians(i);
var vec = new Vector2((float) Math.Sin(rad), (float) Math.Cos(rad));
var dir = vec.ToDirection();
Console.WriteLine(vec + " -> " + dir);
}
var copy = obj.DeepCopy();
Console.WriteLine(copy);