Guest User

Untitled

a guest
Jul 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #ifndef COLLISION_H_
  2. #define COLLISION_H_
  3.  
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. // Implementation details only, disregard as this is irrelevant to the implementation of the actual algorithm.
  9.  
  10. typedef struct {float x, y;} vec;
  11. typedef struct {vec p0, dir;} ray;
  12. typedef struct {vec p0, p1, dir;} seg;
  13. typedef struct {int n; vec *vertices; seg *edges;} polygon; // Assumption: Simply connected => chain vertices together
  14.  
  15. polygon new_polygon(int nvertices, vec *vertices){
  16. seg *edges = (seg*)malloc(sizeof(seg)*(nvertices));
  17. for (int i = 0; i < nvertices-1; i++){
  18. vec dir = {vertices[i+1].x-vertices[i].x, vertices[i+1].y-vertices[i].y};seg cur = {vertices[i], vertices[i+1], dir};
  19. edges[i] = cur;
  20. }
  21. vec dir = {vertices[0].x-vertices[nvertices-1].x, vertices[0].y-vertices[nvertices-1].y};seg cur = {vertices[nvertices-1], vertices[0], dir};
  22. edges[nvertices-1] = cur;
  23. polygon shape = {nvertices, vertices, edges};
  24. return shape;
  25. }
  26.  
  27. vec v(float x, float y){
  28. vec a = {x, y};
  29. return a;
  30. }
  31.  
  32. polygon Polygon(int nvertices, ...){
  33. va_list args;
  34. va_start(args, nvertices);
  35. vec *vertices = (vec*)malloc(sizeof(vec)*nvertices);
  36. for (int i = 0; i < nvertices; i++){
  37. vertices[i] = va_arg(args, vec);
  38. }
  39. va_end(args);
  40. return new_polygon(nvertices, vertices);
  41. }
  42.  
  43. #endif /* COLLISION_H_ */
Add Comment
Please, Sign In to add comment