fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. class point
  6. {
  7. public:
  8. point (int a, int b): x(a), y(b) {};
  9. ~point () {};
  10.  
  11. int getX() {return x;}
  12. int getY() {return y;}
  13.  
  14. private:
  15. int x;
  16. int y;
  17. };
  18.  
  19.  
  20. class side
  21. {
  22. public:
  23. side(point f, point s): first(f), second(s) {};
  24. ~side() {};
  25. point getFirst() {return first;}
  26. point getSecond() {return second;}
  27.  
  28. private:
  29. point first;
  30. point second;
  31. };
  32.  
  33.  
  34. class boundingBox
  35. {
  36. public:
  37. boundingBox(std::vector <point> p);
  38. ~boundingBox() {};
  39.  
  40. std::vector <side> getSides() {return boundingSides;}
  41.  
  42. private:
  43. std::vector <point> boundingPoints;
  44. std::vector <side> boundingSides;
  45. };
  46.  
  47.  
  48. boundingBox::boundingBox(std::vector <point> p)
  49. {
  50. boundingPoints = p;
  51.  
  52. // in the constructor, create a vector of sides from the points
  53. for (std::vector <point>::iterator i = boundingPoints.begin(); i != boundingPoints.end()-1; i++)
  54. {
  55. boundingSides.push_back( side(*i, *(i+1)) );
  56. }
  57.  
  58. boundingSides.push_back( side (*(boundingPoints.end()-1), *(boundingPoints.begin()) ) );
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. bool collisionCheck(std::vector <side> s, point p)
  67. {
  68. std::vector <side> nodeSides;
  69.  
  70. int toleft = 0;
  71. int toright = 0;
  72.  
  73. for (std::vector <side>::iterator i = s.begin(); i != s.end(); i++)
  74. {
  75. //if the Y value of the point being tested is between the Y values of a side, add a node
  76. if (p.getY() > (*i).getFirst().getY() && p.getY() < (*i).getSecond().getY() ||
  77. p.getY() < (*i).getFirst().getY() && p.getY() > (*i).getSecond().getY() )
  78. {
  79. nodeSides.push_back( side ( (*i).getFirst(), (*i).getSecond() ) );
  80. }
  81.  
  82.  
  83. // if the Y value of the point being tested is also the Y of the second point of the side...
  84. if (p.getY() == (*i).getSecond().getY())
  85. {
  86. //if it isn't the last side, and this side and the next strattle that y value, add a node
  87. if (i != s.end()-1)
  88. {
  89. if ((*i).getFirst().getY() < p.getY() && (*(i+1)).getSecond().getY() > p.getY() ||
  90. (*i).getFirst().getY() > p.getY() && (*(i+1)).getSecond().getY() < p.getY() )
  91. {
  92. nodeSides.push_back( side ( (*i).getFirst(), (*i).getSecond() ) );
  93. }
  94. }
  95.  
  96. //if it is the last side, and this side and the first side strattle that y value, add a node
  97. else if ((*i).getFirst().getY() < p.getY() && s.front().getSecond().getY() > p.getY() ||
  98. (*i).getFirst().getY() > p.getY() && s.front().getSecond().getY() < p.getY() )
  99. {
  100. nodeSides.push_back( side ( (*i).getFirst(), (*i).getSecond() ) );
  101. }
  102. }
  103. }
  104.  
  105. for (std::vector <side>::iterator i = nodeSides.begin(); i != nodeSides.end(); i++)
  106. {
  107. double deltaY = (*i).getSecond().getY() - (*i).getFirst().getY();
  108. double deltaX = (*i).getSecond().getX() - (*i).getFirst().getX();
  109.  
  110. double slope = deltaX - deltaY;
  111.  
  112. double x = ( p.getY() - (*i).getSecond().getY() + (slope * (*i).getSecond().getX()) ) / slope;
  113.  
  114. if (x < p.getX())
  115. {
  116. toleft++;
  117. }
  118.  
  119. else
  120. {
  121. toright++;
  122. }
  123. }
  124.  
  125.  
  126. std::cout << "Analysis: " << toleft << " nodes to the left, " << toright << " nodes to the right." << std::endl;
  127.  
  128. if (toleft % 2 == 0)
  129. {
  130. std::cout << "return false, does not hit" << std::endl;
  131. return false;
  132. }
  133.  
  134. else
  135. {
  136. std::cout << "return true, hits" << std::endl;
  137. return true;
  138. }
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. int main ()
  147. {
  148. std::vector <point> points;
  149.  
  150. points.push_back(point(3, 5));
  151. points.push_back(point(1, 13));
  152. points.push_back(point(7, 16));
  153. points.push_back(point(14, 14));
  154. points.push_back(point(8, 13));
  155. points.push_back(point(9, 11));
  156. points.push_back(point(17, 13));
  157. points.push_back(point(16, 18));
  158. points.push_back(point(21, 15));
  159. points.push_back(point(17, 9));
  160. points.push_back(point(9, 7));
  161. points.push_back(point(12, 5));
  162. points.push_back(point(14, 7));
  163. points.push_back(point(15, 2));
  164. points.push_back(point(6, 3));
  165.  
  166. boundingBox enemy(points);
  167.  
  168. point hitSimp(13, 4);
  169. point hitComp(19, 15);
  170. point missNear(10, 12);
  171. point missFar(100,100);
  172.  
  173. collisionCheck(enemy.getSides(), hitSimp);
  174.  
  175. collisionCheck(enemy.getSides(), hitComp);
  176.  
  177. collisionCheck(enemy.getSides(), missNear);
  178.  
  179. collisionCheck(enemy.getSides(), missFar);
  180.  
  181. return 0;
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
Success #stdin #stdout 0s 2992KB
stdin
Standard input is empty
stdout
Analysis: 1 nodes to the left, 1 nodes to the right.
return true, hits
Analysis: 3 nodes to the left, 1 nodes to the right.
return true, hits
Analysis: 2 nodes to the left, 2 nodes to the right.
return false, does not hit
Analysis: 0 nodes to the left, 0 nodes to the right.
return false, does not hit