Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef COLLISION_H_
- #define COLLISION_H_
- #include <stdarg.h>
- #include <stdlib.h>
- #include <stdio.h>
- // Implementation details only, disregard as this is irrelevant to the implementation of the actual algorithm.
- typedef struct {float x, y;} vec;
- typedef struct {vec p0, dir;} ray;
- typedef struct {vec p0, p1, dir;} seg;
- typedef struct {int n; vec *vertices; seg *edges;} polygon; // Assumption: Simply connected => chain vertices together
- polygon new_polygon(int nvertices, vec *vertices){
- seg *edges = (seg*)malloc(sizeof(seg)*(nvertices));
- for (int i = 0; i < nvertices-1; i++){
- vec dir = {vertices[i+1].x-vertices[i].x, vertices[i+1].y-vertices[i].y};seg cur = {vertices[i], vertices[i+1], dir};
- edges[i] = cur;
- }
- vec dir = {vertices[0].x-vertices[nvertices-1].x, vertices[0].y-vertices[nvertices-1].y};seg cur = {vertices[nvertices-1], vertices[0], dir};
- edges[nvertices-1] = cur;
- polygon shape = {nvertices, vertices, edges};
- return shape;
- }
- vec v(float x, float y){
- vec a = {x, y};
- return a;
- }
- polygon Polygon(int nvertices, ...){
- va_list args;
- va_start(args, nvertices);
- vec *vertices = (vec*)malloc(sizeof(vec)*nvertices);
- for (int i = 0; i < nvertices; i++){
- vertices[i] = va_arg(args, vec);
- }
- va_end(args);
- return new_polygon(nvertices, vertices);
- }
- #endif /* COLLISION_H_ */
Add Comment
Please, Sign In to add comment