#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#include <time.h>
static const long double PI = 3.141592653589793238462643383279502884197169399375105;
static const double toRADIANS = 0.017453292519943295769236907684886127134428718885417;
static const double toDEGREES = 57.29577951308232087679815481410517033240547246656432;
bool CollisionBetweenCircles(double x1, double y1, double r1, double x2, double y2, double r2);
bool CollisionBetweenSpheres(double x1, double y1, double z1, double r1, double x2, double y2, double z2, double r2);
bool CollisionPointInRectLT(double pX, double pY, double rectLeft, double rectTop, double rectWidth, double rectHeight);
bool CollisionPointInRect(double pX, double pY, double rectCenterX, double rectCenterY, double rectWidth, double rectHeight);
bool CollisionRectInRect(double rect1Left, double rect1Top, double rect1Right, double rect1Bottom, double rect2Left, double rect2Top, double rect2Right, double rect2Bottom);
double cCos(double x);
double cCosD(double x);
double cSin(double x);
double cSinD(double x);
double cACos(double x);
double cACosD(double x);
double cASin(double x);
double cASinD(double x);
double cTan(double x);
double cTanD(double x);
double cATan(double x);
double cATanD(double x);
double cATan2(double y, double x);
double cATan2D(double y, double x);
double cMax(double x, double y);
double cMin(double x, double y);
double cDim(double x, double y);
double cAbs(double x);
double cRemainder(double x, double y);
double cRound(double x);
double cFloor(double x);
double cCeil(double x);
double cTrunc(double x);
double cSqrt(double x);
double cPow(double x, double y);
double cCbrt(double x);
double cHypot(double x, double y);
double DistanceBetween2D(double x1, double y1, double x2, double y2);
double DistanceBetween3D(double x1, double y1, double z1, double x2, double y2, double z2);
double PointOfCollisionBetweenCircles_X(double x1, double r1, double x2, double r2);
double PointOfCollisionBetweenSpheres_X(double x1, double r1, double x2, double r2);
double PointOfCollisionBetweenCircles_Y(double y1, double r1, double y2, double r2);
double PointOfCollisionBetweenSpheres_Y(double y1, double r1, double y2, double r2);
double PointOfCollisionBetweenSpheres_Z(double z1, double r1, double z2, double r2);
double PointOfIntersectionBetweenLines_X(double Ax, double Ay, double Bx, double By, double Cx, double Cy, double Dx, double Dy);
double PointOfIntersectionBetweenLines_Y(double Ax, double Ay, double Bx, double By, double Cx, double Cy, double Dx, double Dy);
double PostCollisionVelocity(double u1, double u2, double m1, double m2, double CoR);
double RandomNumber(int lowest, int highest, int decimalPlaces, bool preventZero);
double cCos(double x)
{
return cos(x);
}
double cCosD(double x)
{
double n = truncl(x / 360);
if (x > 360) {
x -= (360 * n);
}
else if (x < 360) {
x += (360 * n);
}
if (x == 90 || x == 270 || x == -90 || x == -270) {
return 0;
}
return cos(x * toRADIANS);
}
double cSin(double x)
{
return sin(x);
}
double cSinD(double x)
{
double n = truncl(x / 360);
if (x > 360) {
x -= (360 * n);
}
else if (x < 360) {
x += (360 * n);
}
if (x == 0 || x == 180 || x == -180) {
return 0;
}
return sin(x * toRADIANS);
}
double cACos(double x)
{
return acos(x);
}
double cACosD(double x)
{
return acos(x * toRADIANS) * toDEGREES;
}
double cASin(double x)
{
return asin(x);
}
double cASinD(double x)
{
return asin(x * toRADIANS) * toDEGREES;
}
double cTan(double x)
{
return tan(x);
}
double cTanD(double x)
{
return tan(x * toRADIANS);
}
double cATan(double x)
{
return atan(x);
}
double cATanD(double x)
{
return atan(x) * toDEGREES;
}
double cATan2(double y, double x)
{
return atan2(y, x);
}
double cATan2D(double y, double x)
{
return atan2(y, x) * toDEGREES;
}
double cMax(double x, double y)
{
return fmax(x, y);
}
double cMin(double x, double y)
{
return fmin(x, y);
}
double cDim(double x, double y)
{
return fdim(x, y);
}
double cAbs(double x)
{
return fabs(x);
}
double cRemainder(double x, double y)
{
return remainder(x, y);
}
double cRound(double x)
{
return round(x);
}
double cFloor(double x)
{
return floor(x);
}
double cCeil(double x)
{
return ceil(x);
}
double cTrunc(double x)
{
return trunc(x);
}
double cSqrt(double x)
{
return sqrt(x);
}
double cPow(double x, double y)
{
return pow(x, y);
}
double cCbrt(double x)
{
return cbrt(x);
}
double cHypot(double x, double y)
{
return hypot(x, y);
}
double DistanceBetween2D(double x1, double y1, double x2, double y2)
{
return sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
}
double DistanceBetween3D(double x1, double y1, double z1, double x2, double y2, double z2)
{
return sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1)));
}
bool CollisionBetweenCircles(double x1, double y1, double r1, double x2, double y2, double r2)
{
if ((((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))) <= (r1 + r2) * (r1 + r2)) {
return true;
}
else {
return false;
}
}
bool CollisionBetweenSpheres(double x1, double y1, double z1, double r1, double x2, double y2, double z2, double r2)
{
if ((((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1))) <= ((r1 + r2) * (r1 + r2))) {
return true;
}
else {
return false;
}
}
double PointOfCollisionBetweenCircles_X(double x1, double r1, double x2, double r2)
{
return ((x1 * r2) + (x2 * r1)) / (r1 + r2);
}
double PointOfCollisionBetweenSpheres_X(double x1, double r1, double x2, double r2)
{
return ((x1 * r2) + (x2 * r1)) / (r1 + r2);
}
double PointOfCollisionBetweenCircles_Y(double y1, double r1, double y2, double r2)
{
return ((y1 * r2) + (y2 * r1)) / (r1 + r2);
}
double PointOfCollisionBetweenSpheres_Y(double y1, double r1, double y2, double r2)
{
return ((y1 * r2) + (y2 * r1)) / (r1 + r2);
}
double PointOfCollisionBetweenSpheres_Z(double z1, double r1, double z2, double r2)
{
return ((z1 * r2) + (z2 * r1)) / (r1 + r2);
}
double PointOfIntersectionBetweenLines_X(double Ax, double Ay, double Bx, double By, double Cx, double Cy, double Dx, double Dy)
{
double divisor = ((Ax - Bx)*(Cy - Dy) - (Ay - By)*(Cx - Dx));
if (divisor == 0) {
return INFINITY;
}
double dividendX = ((Ax*By - Ay*Bx)*(Cx - Dx) - (Ax - Bx)*(Cx*Dy - Cy*Dx));
if (dividendX == 0) {
return NAN;
}
return dividendX / divisor;
}
double PointOfIntersectionBetweenLines_Y(double Ax, double Ay, double Bx, double By, double Cx, double Cy, double Dx, double Dy)
{
double divisor = ((Ax - Bx)*(Cy - Dy) - (Ay - By)*(Cx - Dx));
if (divisor == 0) {
return INFINITY;
}
double dividendY = ((Ax*By - Ay*Bx)*(Cy - Dy) - (Ay - By)*(Cx*Dy - Cy*Dx));
if (dividendY == 0) {
return NAN;
}
return dividendY / divisor;
}
bool CollisionPointInRectLT(double pX, double pY, double rectLeft, double rectTop, double rectWidth, double rectHeight)
{
if (pX >= rectLeft && pX <= (rectLeft + rectWidth) && pY >= rectTop && pY <= (rectTop + rectHeight)) {
return true;
}
else {
return false;
}
}
bool CollisionPointInRect(double pX, double pY, double rectCenterX, double rectCenterY, double rectWidth, double rectHeight)
{
if (pX >= rectCenterX - (rectWidth * 0.5) && pX <= rectCenterX + (rectWidth * 0.5) && pY >= rectCenterY - (rectHeight * 0.5) && pY <= rectCenterY + (rectHeight * 0.5)) {
return true;
}
else {
return false;
}
}
bool CollisionRectInRect(double rect1Left, double rect1Top, double rect1Right, double rect1Bottom, double rect2Left, double rect2Top, double rect2Right, double rect2Bottom)
{
if (rect1Left <= rect2Right && rect2Left <= rect1Right && rect1Top <= rect2Bottom && rect2Top <= rect1Bottom) {
return true;
}
else {
return false;
}
}
double PostCollisionVelocity(double u1, double u2, double m1, double m2, double CoR)
{
double v1 = ((u1 * (m1 - m2)) + (2 * m2 * u2)) / (m1 + m2);
return v1 * CoR;
}
double RandomNumber(int lowest, int highest, int decimalPlaces, bool preventZero)
{
srand((unsigned int)time(NULL));
double decimal = 0;
int decimalRange = (int)(pow(10, decimalPlaces));
if (decimalPlaces > 0) {
decimal = (rand() % decimalRange);
decimal = decimal / decimalRange;
}
if (lowest == highest) {
return lowest;
}
else {
if (lowest > highest) {
int tempValue = lowest;
lowest = highest;
highest = tempValue;
}
double returnValue = lowest + rand() % (highest - lowest) + decimal;
if (preventZero == true && returnValue == 0) {
return RandomNumber(lowest, highest, decimalPlaces, preventZero);
}
else {
return returnValue;
}
}
}