Created
October 15, 2020 15:42
-
-
Save gunavaran/46abd59df57eeb443a0cf9dfd6e02e68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//courtesy: MIT Open Courseware (https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-172-performance-engineering-of-software-systems-fall-2018/lecture-slides/MIT6_172F18_lec2.pdf) | |
#include <stdbool.h> | |
#include <math.h> | |
typedef struct{ | |
double x; //x-coordinate | |
double y; //y-coordinate | |
double z; //z-coordinate | |
double r; // radius of the ball | |
} ball_t; | |
double square (double x){ | |
return x * x; | |
} | |
bool collides(ball_t *b1, ball_t *b2){ | |
if((abs(b1->x - b2->x) > abs(b1->r + b2->r)) || ((abs(b1->y - b2->y) > abs(b1->r + b2->r)) || ((abs(b1->z - b2->z) > abs(b1->r + b2->r))){ | |
return false; | |
} | |
double squared_distance = square(b1->x - b2->x) + square(b1->y - b2->y) + square(b1->z - b2->z); | |
return squared_distance <= square(b1->r + b2->r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment