Advertisement
NoxScourge

Untitled

Nov 20th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. POLIGON.CPP
  2.  
  3. #include "Poligon.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <math.h>
  7. using namespace std;
  8. int i=0;
  9. Poligon::Poligon(int n)
  10. {
  11. this->BrojTemena = n;
  12. this->x = new double[this->BrojTemena];
  13. this->y = new double[this->BrojTemena];
  14. }
  15.  
  16. Poligon::Poligon()
  17. {
  18. this->BrojTemena = 0;
  19. this->x = new double[this->BrojTemena];
  20. this->y = new double[this->BrojTemena];
  21. }
  22.  
  23. Poligon::~Poligon()
  24. {
  25. if(this->x != NULL)
  26. {
  27. delete [] this->x;
  28. }
  29. if(this->y != NULL)
  30. {
  31. delete [] this->y;
  32. }
  33. }
  34.  
  35. void Poligon::Obim()
  36. {
  37. int i;
  38. double obim=0,k1,k2;
  39. for(i=0; i<this->BrojTemena-1; i++)
  40. {
  41. k1=(this->x[i+1]-this->x[i])*(this->x[i+1]-this->x[i]);
  42. k2=(this->y[i+1]-this->y[i])*(this->y[i+1]-this->y[i]);
  43. obim += sqrt(k1+k2);
  44. }
  45. k1=(this->x[i]-this->x[0])*(this->x[i]-this->x[0]);
  46. k2=(this->y[i]-this->y[0])*(this->y[i]-this->y[0]);
  47. obim += sqrt(k1+k2);
  48. cout<<"Obim:"<<obim;
  49. }
  50.  
  51. void Poligon::PostaviKordinate(int X, int Y)
  52. {
  53.  
  54. this->x[i] = (double)X;
  55. this->y[i] = (double)Y;
  56. i++;
  57. }
  58.  
  59. void Poligon::VratiNajdalje()
  60. {
  61. int i;
  62.  
  63. double r1,r2,r3,max=0,k;
  64. for(i=0; i<this->BrojTemena-1; i++)
  65. {
  66.  
  67. r1 = (this->x[i]-this->x[i+1])*(this->x[0]-this->x[i+1]);
  68. r2 = (this->y[i]-this->y[i+1])*(this->y[i]-this->y[i+1]);
  69. r3=r1+r2;
  70. k = sqrt(r3);
  71. if(k>max)
  72. {
  73. max = k;
  74. }
  75. }
  76.  
  77. r1 = (this->x[0]-this->x[this->BrojTemena-1])*(this->x[0]-this->x[this->BrojTemena-1]);
  78. r2 = (this->y[9]-this->y[this->BrojTemena-1])*(this->y[0]-this->y[this->BrojTemena-1]);
  79. r3=r1+r2;
  80. k = sqrt(r3);
  81. if(k>max)
  82. {
  83. max = k;
  84. }
  85. cout<<"Najduze rastojanje:"<<max;
  86. }
  87.  
  88. void Poligon::PrikaziKordinate()
  89. {
  90. for(int i=0; i<this->BrojTemena; i++)
  91. {
  92. cout<<"X:"<<this->x[i]<<" "<<"Y:"<<this->y[i]<<endl;
  93. }
  94. }
  95.  
  96. void Poligon::IzbaciTeme()
  97. {
  98. double* kopijax;
  99. kopijax = new double[20];
  100. double* kopijay;
  101. kopijay = new double[20];
  102.  
  103. for(int i=0; i<this->BrojTemena; i++)
  104. {
  105. kopijax[i] = this->x[i];
  106. kopijay[i] = this->y[i];
  107. }
  108.  
  109. delete[] this->x;
  110. delete[] this->y;
  111.  
  112. this->BrojTemena--;
  113. this->x = new double[this->BrojTemena];
  114. this->y = new double[this->BrojTemena];
  115.  
  116. for(int i=0; i<this->BrojTemena; i++)
  117. {
  118. this->x[i] = kopijax[i];
  119. this->y[i] = kopijay[i];
  120. }
  121.  
  122. delete[] kopijax;
  123. delete[] kopijay;
  124. }
  125.  
  126. Poligon::Poligon(const Poligon& p)
  127. {
  128. this->BrojTemena = p.BrojTemena;
  129. x = new double[p.BrojTemena];
  130. y = new double[p.BrojTemena];
  131. for(int i =0; i<BrojTemena; i++)
  132. {
  133. x[i] = p.x[i];
  134. y[i] = p.y[i];
  135. }
  136. }
  137.  
  138. void Poligon::PreslikajUOdnosuNaX()
  139. {
  140. for(int i=0; i<this->BrojTemena; i++)
  141. {
  142. this->x[i] = this->x[i]*(-1);
  143. }
  144. }
  145.  
  146. void Poligon::PreslikajUOdnosuNaY()
  147. {
  148. for(int i=0; i<this->BrojTemena; i++)
  149. {
  150. this->y[i] = this->y[i]*(-1);
  151. }
  152. }
  153.  
  154. istream& operator>>(istream& ulaz, Poligon& p)
  155. {
  156. for(int i=0; i<p.BrojTemena; i++)
  157. {
  158. ulaz>>p.x[i]>>p.y[i];
  159. }
  160. return ulaz;
  161. }
  162.  
  163. ostream& operator<<(ostream& izlaz, const Poligon& p)
  164. {
  165. for(int i=0; i<p.BrojTemena; i++)
  166. {
  167. izlaz <<p.x[i]<<' '<<p.y[i]<<endl;
  168. }
  169. return izlaz;
  170. }
  171.  
  172. ------------------------------------------------------------------------------------------------------------------------------------------
  173. MAIN.CPP
  174.  
  175. #include "Slika.h"
  176. #include <iostream>
  177. #include <fstream>
  178. using namespace std;
  179.  
  180. void main()
  181. {
  182. Poligon* p1;
  183. int n;
  184. cout<<"Broj temena p1:"<<endl;
  185. cin>>n;
  186. p1= new Poligon(n);
  187. ifstream f("kordinate.txt");
  188. f>>*p1;
  189. f.close();
  190. cout<<"P1 posle preslikavanjie u odnosu na X osu"<<endl;
  191. p1->PreslikajUOdnosuNaX();
  192. p1->PrikaziKordinate();
  193. cout<<"P1 Posle preslikavanjie u odnosu na Y osu"<<endl;
  194. p1->PreslikajUOdnosuNaY();
  195. p1->PrikaziKordinate();
  196. int m;
  197. cout<<"Broj temena p2:"<<endl;
  198. cin>>m;
  199. Poligon* p2 = new Poligon(m);
  200. cout<<"P2 posle preslikavanjie u odnosu na X osu"<<endl;
  201. ifstream f1("kordinate.txt");
  202. f1>>*p2;
  203. f1.close();
  204. p2->PreslikajUOdnosuNaX();
  205. p2->PrikaziKordinate();
  206. cout<<"P2 posle preslikavanjie u odnosu na Y osu"<<endl;
  207. p2->PreslikajUOdnosuNaY();
  208. p2->PrikaziKordinate();
  209.  
  210. int k;
  211. cout<<"Maksimalan broj elementa:"<<endl;
  212. cin>>k;
  213. Slika s(k);
  214. s.DodajPoligon(*p1);
  215. s.DodajPoligon(*p2);
  216. cout<<s;
  217.  
  218. for(int i=0; i<k-2; i++)
  219. {
  220. int t;
  221. cout<<"Broj tacaka:"<<endl;
  222. cin>>t;
  223. Poligon* p3;
  224. p3 = new Poligon(t);
  225. for(int j=0; j<t; j++)
  226. {
  227. int x,y;
  228. cout<<"X: Y: "<<endl;
  229. cin>>x>>y;
  230. p3->PostaviKordinate(x,y);
  231. }
  232. s.DodajPoligon(*p3);
  233. }
  234. cout<<"posle"<<endl;
  235. cout<<s;
  236. system("pause");
  237.  
  238. }
  239.  
  240. ---------------------------------------------------------------------------------------------------------------------------------
  241. POLIGON.H
  242. #include <iostream>
  243.  
  244. using namespace std;
  245. class Poligon
  246. {
  247. public:
  248. int BrojTemena;
  249. double* x;
  250. double* y;
  251. public:
  252. Poligon(int n);
  253. Poligon();
  254. ~Poligon();
  255. Poligon(const Poligon& p);
  256. inline int Vrati()
  257. {
  258. return this->BrojTemena;
  259. }
  260. void Obim();
  261. void PostaviKordinate(int X, int Y);
  262. void VratiNajdalje();
  263. void PrikaziKordinate();
  264. void IzbaciTeme();
  265. void PreslikajUOdnosuNaX();
  266. void PreslikajUOdnosuNaY();
  267.  
  268. friend istream& operator>>(istream& ulaz, Poligon& p);
  269. friend ostream& operator<<(ostream& izlaz, const Poligon& p);
  270. };
  271.  
  272. -----------------------------------------------------------------------------------------------------------------------------------------
  273.  
  274. SLIKA.CPP
  275.  
  276. #include "Slika.h"
  277. #include <iostream>
  278. #include <fstream>
  279. #include <math.h>
  280. int br=0;
  281. Slika::Slika(int k)
  282. {
  283. this->maxBrojaPoligona=k;
  284. this->niz = new Poligon[this->maxBrojaPoligona];
  285. }
  286.  
  287. void Slika::DodajPoligon(Poligon& p)
  288. {
  289. this->niz[br] = p;
  290. br++;
  291. }
  292.  
  293. ostream& operator<<(ostream& izlaz, const Slika& s)
  294. {
  295. for(int i=0; i<s.maxBrojaPoligona; i++)
  296. {
  297. izlaz << s.niz[i];
  298. }
  299. return izlaz;
  300. }
  301.  
  302. -----------------------------------------------------------------------------------------------------------------------------------------
  303. SLIKA.H
  304. #include <iostream>
  305. #include "Poligon.h"
  306. using namespace std;
  307.  
  308. class Slika
  309. {
  310. public:
  311. int maxBrojaPoligona;
  312. Poligon* niz;
  313. public:
  314. Slika(int k);
  315. void DodajPoligon(Poligon& p);
  316. friend ostream& operator<<(ostream& izlaz,const Slika& p);
  317. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement