Guest User

Untitled

a guest
Nov 17th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. QuadTree : Capacity = 1, Divided (0:False, 1:True) = 0
  2. Rectangle : Center Position = (0, 0), Width = 10, Height = 10
  3. -------------------
  4. -------------------
  5. QuadTree : Capacity = 1, Divided (0:False, 1:True) = 1
  6. Rectangle : Center Position = (0, 0), Width = 10, Height = 10
  7. Northwest :
  8. terminate called after throwing an instance of 'std::bad_alloc'
  9. what(): std::bad_alloc
  10. Aborted (core dumped)
  11.  
  12. #include <iostream> //For console output/input
  13. #include <fstream> //Allows to read/write files
  14. #include <math.h> //Basic mathematic functions
  15. #include <vector> //For dynamic arrays
  16. #include <string> //Operations on strings
  17. #include <tuple>
  18. #include <cmath>
  19. #include <sstream>
  20. using namespace std;
  21.  
  22. class Point
  23. {
  24. public :
  25.  
  26. double get_x();
  27. double get_y();
  28. void set_x(double xp);
  29. void set_y(double yp);
  30.  
  31. void get_information();
  32.  
  33. private :
  34.  
  35. double x;
  36. double y;
  37. };
  38.  
  39. double Point::get_x(){return x;}
  40. double Point::get_y(){return y;}
  41. void Point::set_x(double xp){x = xp;}
  42. void Point::set_y(double yp){y = yp;}
  43.  
  44.  
  45. class Rectangle
  46. {
  47. public :
  48.  
  49. double get_x();
  50. double get_y();
  51. double get_w();
  52. double get_h();
  53.  
  54. void set_x(double xc);
  55. void set_y(double yc);
  56. void set_w(double wc);
  57. void set_h(double hc);
  58.  
  59. bool contain(Point pt);
  60.  
  61. void get_information();
  62.  
  63. private :
  64.  
  65. double x;
  66. double y;
  67. double w;
  68. double h;
  69. };
  70.  
  71. double Rectangle::get_x() {return x;}
  72. double Rectangle::get_y() {return y;}
  73. double Rectangle::get_w() {return w;}
  74. double Rectangle::get_h() {return h;}
  75.  
  76. void Rectangle::set_x(double xc) {x = xc;}
  77. void Rectangle::set_y(double yc) {y = yc;}
  78. void Rectangle::set_w(double wc) {w = wc;}
  79. void Rectangle::set_h(double hc) {h = hc;}
  80.  
  81.  
  82. class QuadTree
  83. {
  84. public :
  85.  
  86. Rectangle get_boundary();
  87. int get_capacity();
  88. void set_boundary(double xc, double yc, double wc, double hc);
  89. void set_rectangle(Rectangle rect);
  90. void set_capacity(int capacity);
  91.  
  92. void insert(Point pt);
  93. void subdivide();
  94. void set_divided();
  95. bool is_divided();
  96.  
  97. void get_information();
  98.  
  99. QuadTree getNW();
  100. QuadTree getNE();
  101. QuadTree getSW();
  102. QuadTree getSE();
  103. void setNW(QuadTree nw);
  104. void setNE(QuadTree ne);
  105. void setSW(QuadTree sw);
  106. void setSE(QuadTree se);
  107.  
  108.  
  109. private :
  110.  
  111. QuadTree* NW=NULL;
  112. QuadTree* NE=NULL;
  113. QuadTree* SW=NULL;
  114. QuadTree* SE=NULL;
  115. Rectangle boundary;
  116. vector<Point> p;
  117. bool divided = false;
  118. };
  119.  
  120. QuadTree QuadTree::getNW(){return *NW;}
  121. QuadTree QuadTree::getNE(){return *NE;}
  122. QuadTree QuadTree::getSW(){return *SW;}
  123. QuadTree QuadTree::getSE(){return *SE;}
  124.  
  125. void QuadTree::setNW(QuadTree nw){NW=&nw;}
  126. void QuadTree::setNE(QuadTree ne){NE=&ne;}
  127. void QuadTree::setSW(QuadTree sw){SW=&sw;}
  128. void QuadTree::setSE(QuadTree se){SE=&se;}
  129.  
  130. bool QuadTree::is_divided(){return divided;}
  131. bool Rectangle::contain(Point pt)
  132. {
  133. return (pt.get_x() > get_x() - get_w()
  134. and pt.get_x() < get_x() + get_w()
  135. and pt.get_y() > get_y() - get_h()
  136. and pt.get_y() < get_y() + get_h());
  137. }
  138. Rectangle QuadTree::get_boundary() {return boundary;}
  139. void QuadTree::set_boundary(double xc, double yc, double wc, double hc)
  140. {
  141. boundary.set_x(xc);
  142. boundary.set_y(yc);
  143. boundary.set_w(wc);
  144. boundary.set_h(hc);
  145. }
  146.  
  147. //int QuadTree::get_capacity() {return n;}
  148. //void QuadTree::set_capacity(int capacity) {n = capacity;}
  149. void QuadTree::set_divided() {divided = true;}
  150. void QuadTree::set_rectangle(Rectangle rect) {boundary = rect;}
  151. void QuadTree::subdivide()
  152. {
  153. double xc = boundary.get_x();
  154. double yc = boundary.get_y();
  155. double wc = boundary.get_w();
  156. double hc = boundary.get_h();
  157. Rectangle nw;
  158. nw.set_x(xc-wc/2.);
  159. nw.set_y(yc+hc/2.);
  160. nw.set_w(wc/2.);
  161. nw.set_h(hc/2.);
  162. Rectangle ne;
  163. ne.set_x(xc+wc/2.);
  164. ne.set_y(yc+hc/2.);
  165. ne.set_w(wc/2.);
  166. ne.set_h(hc/2.);
  167. Rectangle sw;
  168. sw.set_x(xc-wc/2.);
  169. sw.set_y(yc-hc/2.);
  170. sw.set_w(wc/2.);
  171. sw.set_h(hc/2.);
  172. Rectangle se;
  173. se.set_x(xc-wc/2.);
  174. se.set_y(yc+hc/2.);
  175. se.set_w(wc/2.);
  176. se.set_h(hc/2.);
  177.  
  178. QuadTree oNW, oNE, oSW, oSE;
  179. oNW.set_rectangle(nw);
  180. oNE.set_rectangle(ne);
  181. oSW.set_rectangle(sw);
  182. oSE.set_rectangle(se);
  183.  
  184. setNW(oNW);
  185. setNE(oNE);
  186. setSW(oSW);
  187. setSE(oSE);
  188. //NW = &oNW;
  189. //NE = &oNE;
  190. //SW = &oSW;
  191. //SE = &oSE;
  192. }
  193.  
  194. void QuadTree::insert(Point pt)
  195. {
  196. if (! get_boundary().contain(pt) ) {cout<<"Hello 1"<<endl; return; }
  197. if (p.size() < 1)
  198. {
  199. cout<<"Hello 2"<<endl;
  200. p.push_back(pt); // Insert element at the end
  201. }
  202. else
  203. {
  204. if (!divided)
  205. {
  206. QuadTree::subdivide();
  207. QuadTree::set_divided();
  208. }
  209. }
  210. NW->insert(pt);
  211. NE->insert(pt);
  212. SW->insert(pt);
  213. SE->insert(pt);
  214. }
  215.  
  216. void Point::get_information(){cout<<"Point : x = "<<get_x()<<"; y = "<<get_y()<<endl;}
  217. void Rectangle::get_information(){cout<<"Rectangle : Center Position = ("<<get_x()<<", "<<get_y()<<"), Width = "<<get_w()<<", Height = "<<get_h()<<endl;}
  218. void QuadTree::get_information()
  219. {
  220. cout<<"QuadTree : Capacity = "<<" 1"<<", Divided (0:False, 1:True) = "<<divided<<endl;
  221. boundary.get_information();
  222. /*cout<<"Points_in : "<<endl;
  223. int siz = p.size();
  224. for (int ii=0; ii<siz; ii++)
  225. {
  226. p[ii].get_information();
  227. }*/
  228. if (divided) {
  229. cout<<" Northwest : "<<endl;
  230. getNW().get_information();
  231. cout<<" Northeast : "<<endl;
  232. getNE().get_information();
  233. cout<<" Southwest : "<<endl;
  234. getSW().get_information();
  235. cout<<" Southeast : "<<endl;
  236. getSE().get_information();
  237. }
  238. }
  239.  
  240.  
  241. int main()
  242. {
  243. QuadTree tree;
  244. tree.set_boundary(0., 0., 10., 10.);
  245. tree.get_information();
  246. cout<<"-------------------"<<endl;
  247. tree.subdivide();
  248. tree.set_divided();
  249. cout<<"-------------------"<<endl;
  250. tree.get_information();
  251. }
Add Comment
Please, Sign In to add comment