breakout/vector.c

89 lines
1.9 KiB
C
Raw Permalink Normal View History

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