2018-01-09 13:22:10 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "vector.h"
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
const double degreeToRadians = M_PI / 180.0f;
|
|
|
|
// End Properties
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
Vector VECTOR_ScaleBy(Vector v, double factor){
|
2018-01-09 13:22:10 +01:00
|
|
|
return (Vector)
|
|
|
|
{
|
|
|
|
.x = v.x * factor,
|
|
|
|
.y = v.y * factor
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
double VECTOR_GetMagnitude(Vector v){
|
2018-01-09 13:22:10 +01:00
|
|
|
return sqrt((v.x * v.x) + (v.y * v.y));
|
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
double VECTOR_GetRotation(Vector v){
|
2018-01-09 13:22:10 +01:00
|
|
|
double da = atan2(v.x, -v.y) / degreeToRadians;
|
|
|
|
|
|
|
|
if (da < 0.0f) return (da + 360.0f);
|
|
|
|
else return da;
|
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
Vector VECTOR_ChangeScaleTo(Vector v, double magnitude){
|
|
|
|
return VECTOR_ScaleBy(v, magnitude / VECTOR_GetMagnitude(v));
|
2018-01-09 13:22:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
double VECTOR_DotProduct(Vector v1, Vector v2){
|
2018-01-09 13:22:10 +01:00
|
|
|
return (v1.x * v2.x ) + (v1.y * v2.y);
|
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
double VECTOR_Distance(Vector v1, Vector v2){
|
|
|
|
return sqrt(VECTOR_DotProduct(v1, v2));
|
2018-01-09 13:22:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
Vector VECTOR_Add(Vector v1, Vector v2){
|
2018-01-09 13:22:10 +01:00
|
|
|
return (Vector)
|
|
|
|
{
|
|
|
|
.x = v1.x + v2.x,
|
|
|
|
.y = v1.y + v2.y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
Vector VECTOR_Subtract(Vector v1, Vector v2){
|
2018-01-09 13:22:10 +01:00
|
|
|
return (Vector)
|
|
|
|
{
|
|
|
|
.x = v1.x - v2.x,
|
|
|
|
.y = v1.y - v2.y
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
double degreeSin(double x){
|
|
|
|
return sin(x * degreeToRadians);
|
|
|
|
}
|
|
|
|
|
|
|
|
double degreeCos(double x){
|
|
|
|
return cos(x * degreeToRadians);
|
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
Vector VECTOR_GetDirectionalUnitVector(double rotation){
|
2018-01-09 13:22:10 +01:00
|
|
|
return (Vector)
|
|
|
|
{
|
|
|
|
.x = degreeSin(rotation),
|
|
|
|
.y = -degreeCos(rotation)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
Vector VECTOR_GetScaledDirectionalUnitVector(double rotation, double Magnitude){
|
2018-01-09 13:22:10 +01:00
|
|
|
Vector v = (Vector)
|
|
|
|
{
|
|
|
|
.x = degreeSin(rotation),
|
|
|
|
.y = -degreeCos(rotation)
|
|
|
|
};
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
return VECTOR_ScaleBy(v, Magnitude);
|
2018-01-09 13:22:10 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 10:56:52 +01:00
|
|
|
Vector VECTOR_GetScaledVectorFromTo(Vector from, Vector to, double Magnitude){
|
|
|
|
return VECTOR_ChangeScaleTo(VECTOR_Subtract(to, from), Magnitude);
|
2018-01-09 13:22:10 +01:00
|
|
|
}
|
2018-01-21 15:33:09 +01:00
|
|
|
|
|
|
|
Vector VECTOR_GetVectorFromTo(Vector from, Vector to){
|
|
|
|
return VECTOR_Subtract(to, from);
|
|
|
|
}
|