quadrants/source/core/math.cpp

119 lines
3.2 KiB
C++
Raw Normal View History

2023-02-27 14:02:30 +01:00
#include <math.h>
#include <raylib.h>
Vector2 operator +(Vector2 vectorA, Vector2 vectorB) {
return (Vector2){(vectorA.x + vectorB.x), (vectorA.y + vectorB.y)};
}
Vector2 operator +=(Vector2& vectorA, Vector2 vectorB) {
vectorA.x += vectorB.x;
vectorA.y += vectorB.y;
return vectorA;
}
Vector2 operator -(Vector2 vectorA, float const value) {
return (Vector2){(vectorA.x - value), (vectorA.y - value)};
}
Vector2 operator -(Vector2 vectorA, Vector2 vectorB) {
return (Vector2){(vectorA.x - vectorB.x), (vectorA.y - vectorB.y)};
}
Vector2 operator -=(Vector2& vectorA, Vector2 vectorB) {
vectorA.x -= vectorB.x;
vectorA.y -= vectorB.y;
return vectorA;
}
Vector2 operator *(Vector2 vectorA, float const value) {
return (Vector2){(vectorA.x * value), (vectorA.y * value)};
}
Vector2 operator *(Vector2 vectorA, Vector2 vectorB) {
return (Vector2){(vectorA.x * vectorB.x), (vectorA.y * vectorB.y)};
}
Vector2 operator *=(Vector2& vectorA, Vector2 vectorB) {
vectorA.x *= vectorB.x;
vectorA.y *= vectorB.y;
return vectorA;
}
Vector2 operator /(Vector2 vectorA, float const value) {
return (Vector2){(vectorA.x / value), (vectorA.y / value)};
}
Vector2 operator /(Vector2 vectorA, Vector2 vectorB) {
return (Vector2){(vectorA.x / vectorB.x), (vectorA.y / vectorB.y)};
}
Vector2 operator /=(Vector2& vectorA, Vector2 vectorB) {
vectorA.x /= vectorB.x;
vectorA.y /= vectorB.y;
return vectorA;
}
float Vector2Distance(Vector2 vectorA, Vector2 vectorB) {
float const xDifference = (vectorB.x - vectorA.x);
float const yDifference = (vectorB.y - vectorA.y);
return sqrt((xDifference * xDifference) + (yDifference * yDifference));
}
/**
* Computes and returns the X component of a point translation given its `magnitude` and radian
* `angle`.
*/
float const LengthDirectionX(float const magnitude, float const angle) {
return (magnitude * -cos(angle));
}
/**
* Computes and returns the Y component of a point translation given its `magnitude` and radian
* `angle`.
*/
float const LengthDirectionY(float const magnitude, float const angle) {
return (magnitude * -sin(angle));
}
Vector2 const LengthDirection(Vector2 const lengths, float const direction) {
return (Vector2){
LengthDirectionX(lengths.x, direction),
LengthDirectionY(lengths.y, direction)
};
}
float const AngleToPoint(Vector2 const originPoint, Vector2 const targetPoint) {
return atan2((originPoint.y - targetPoint.y), (originPoint.x - targetPoint.x));
}
/**
* Wraps a radian `angle` by its bounds, with any value less than `-PI` returning `PI` and any
* value greater than `PI` returning `-PI`.
*/
float const WrapAngle(float const angle) {
if (angle > PI) {
return -PI;
} else if (angle < -PI) {
return PI;
}
return angle;
}
/**
* Calculates the linear interpolation between `angleStart` to `angleTo` with the given
* `magnitude`, which is expected to be a normalized value representing a percentage of the
* transformation, with `0.0f` being pure `angleStart` and `1.0f` being a very close aproximation
* of `angleTo`.
*/
float const AngleDifference(float const angleStart, float const angleTo) {
float const difference = (angleTo - angleStart);
return atan2(sin(difference), cos(difference));
}