Guest User

Untitled

a guest
Apr 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. #include <allegro.h>
  2. #include <cmath>
  3.  
  4. class punkt{
  5. public:
  6. int x, y;
  7. double a;
  8.  
  9. punkt *next;
  10. punkt *prev;
  11. };
  12.  
  13. punkt *head = NULL;
  14. punkt *tail = NULL;
  15.  
  16. //NARYSUJ PUNKTY
  17. int rys_pkt()
  18. {
  19. clear_to_color( screen, makecol( 255, 255, 255 ) );
  20.  
  21. punkt *nowy;
  22.  
  23. if(head != NULL)
  24. {
  25. nowy = head;
  26. while(nowy != NULL)
  27. {
  28. circlefill( screen, nowy->x, nowy->y, 2, makecol( 0, 0, 255 ) );
  29. nowy = nowy->next;
  30. }
  31. }
  32. else
  33. {
  34. textprintf_ex( screen, font, 20, 20, makecol( 255, 0, 0 ), - 1, "Nie dodales jeszcze zadnego punktu!!!" );
  35. }
  36.  
  37. return D_O_K;
  38. }
  39.  
  40. //DODAWANIE PUNKTU
  41. int add()
  42. {
  43. punkt *nowy = NULL;
  44.  
  45. while(true)
  46. {
  47. if(mouse_b == 1)
  48. {
  49. nowy = new punkt;
  50. nowy->x = mouse_x;
  51. nowy->y = mouse_y;
  52. if(head == NULL)
  53. {
  54. nowy->next = NULL;
  55. nowy->prev = NULL;
  56. head = nowy;
  57. tail = nowy;
  58. }
  59. else
  60. {
  61. nowy->next = NULL;
  62. nowy->prev = tail;
  63. tail->next = nowy;
  64. tail = nowy;
  65. }
  66.  
  67. circlefill( screen, nowy->x, nowy->y, 2, makecol( 0, 0, 255 ) );
  68.  
  69. while(mouse_b == 1){}
  70. break;
  71. }
  72. else if(mouse_b == 2) return 0;
  73. }
  74.  
  75. return 1;
  76. }
  77.  
  78. //DODAWANIE PUNKTÓW
  79. int dodawanie()
  80. {
  81. clear_to_color( screen, makecol( 255, 255, 255 ) );
  82. head = NULL;
  83. tail = NULL;
  84.  
  85. textprintf_ex( screen, font, 20, 20, makecol( 0, 0, 0 ), - 1, "ABY ZAKONCZYC DODAWANIE PUNKTOW WCISNIJ PPM" );
  86.  
  87. while(add())
  88. {
  89. add();
  90. }
  91.  
  92. rys_pkt();
  93.  
  94. return D_O_K;
  95. }
  96.  
  97. //OBLICZANIE ALFA
  98. double alfa(punkt *k, int sx, int sy)
  99. {
  100. double a, d, x, y, pomx, pomy;
  101.  
  102. x = k->x - sx;
  103. y = k->y - sy;
  104.  
  105. pomx = x;
  106. pomy = y;
  107.  
  108. if(pomx < 0) pomx = -pomx;
  109. if(pomy < 0) pomy = -pomy;
  110.  
  111. d = pomx + pomy;
  112.  
  113. //obliczanie alfa
  114. if(x > 0 && y >= 0) a = pomy/d;
  115. else if(x <= 0 && y > 0) a = 2 - (pomy/d);
  116. else if(x < 0 && y <= 0) a = 2 + (pomy/d);
  117. else if(x >= 0 && y < 0) a = 4 - (pomy/d);
  118.  
  119. return a;
  120. }
  121.  
  122. //ZLICZACZ ILOSCI PUNKTOW
  123. int licz()
  124. {
  125. punkt *nowy;
  126. int licznik = 0;
  127.  
  128. if(head != NULL)
  129. {
  130. nowy = head;
  131. while(nowy != NULL)
  132. {
  133. nowy = nowy->next;
  134. licznik++;
  135. }
  136. }
  137.  
  138. return licznik;
  139. }
  140.  
  141. //SPRAWDZANIE CZY PUNKT LEZY WEWNATRZ TRUJKATA
  142. int spr(punkt *q1, punkt *q2, punkt *q3, int sx, int sy)
  143. {
  144. int pom_x = 800, pom_y = 300, licznik = 0;
  145. float det1, det2, det3, det4;
  146.  
  147. //sprawdzenie czy prosta q2 pom przecina się z prosta O q1
  148. det1 = sx*q1->y + q1->x*q2->y + q2->x*sy - q2->x*q1->y - sx*q2->y - q1->x*sy;
  149. det2 = sx*q1->y + q1->x*pom_y + pom_x*sy - pom_x*q1->y - sx*pom_y - q1->x*sy;
  150. det3 = pom_x*q2->y + q2->x*sy + sx*pom_y - sx*q2->y - pom_x*sy - q2->x*pom_y;
  151. det4 = pom_x*q2->y + q2->x*q1->y + q1->x*pom_y - q1->x*q2->y - pom_x*q1->y - q2->x*pom_y;
  152.  
  153. if(det1 > 0) det1 = 1;
  154. if(det1 == 0) det1 = 0;
  155. if(det1 < 0) det1 = -1;
  156.  
  157. if(det2 > 0) det2 = 1;
  158. if(det2 == 0) det2 = 0;
  159. if(det2 < 0) det2 = -1;
  160.  
  161. if(det3 > 0) det3 = 1;
  162. if(det3 == 0) det3 = 0;
  163. if(det3 < 0) det3 = -1;
  164.  
  165. if(det4 > 0) det4 = 1;
  166. if(det4 == 0) det4 = 0;
  167. if(det4 < 0) det4 = -1;
  168.  
  169. if(det1 != det2 && det3 != det4) licznik++;
  170.  
  171. //sprawdzenie czy prosta q2 pom przecina się z prosta O q3
  172. det1 = sx*q3->y + q3->x*q2->y + q2->x*sy - q2->x*q3->y - sx*q2->y - q3->x*sy;
  173. det2 = sx*q3->y + q3->x*pom_y + pom_x*sy - pom_x*q3->y - sx*pom_y - q3->x*sy;
  174. det3 = pom_x*q2->y + q2->x*sy + sx*pom_y - sx*q2->y - pom_x*sy - q2->x*pom_y;
  175. det4 = pom_x*q2->y + q2->x*q3->y + q3->x*pom_y - q3->x*q2->y - pom_x*q3->y - q2->x*pom_y;
  176.  
  177. if(det1 > 0) det1 = 1;
  178. if(det1 == 0) det1 = 0;
  179. if(det1 < 0) det1 = -1;
  180.  
  181. if(det2 > 0) det2 = 1;
  182. if(det2 == 0) det2 = 0;
  183. if(det2 < 0) det2 = -1;
  184.  
  185. if(det3 > 0) det3 = 1;
  186. if(det3 == 0) det3 = 0;
  187. if(det3 < 0) det3 = -1;
  188.  
  189. if(det4 > 0) det4 = 1;
  190. if(det4 == 0) det4 = 0;
  191. if(det4 < 0) det4 = -1;
  192.  
  193. if(det1 != det2 && det3 != det4) licznik++;
  194.  
  195. //sprawdzenie czy prosta q2 pom przecina się z prosta q1 q3
  196. det1 = q1->x*q3->y + q3->x*q2->y + q2->x*q1->y - q2->x*q3->y - q1->x*q2->y - q3->x*q1->y;
  197. det2 = q1->x*q3->y + q3->x*pom_y + pom_x*q1->y - pom_x*q3->y - q1->x*pom_y - q3->x*q1->y;
  198. det3 = pom_x*q2->y + q2->x*q1->y + q1->x*pom_y - q1->x*q2->y - pom_x*q1->y - q2->x*pom_y;
  199. det4 = pom_x*q2->y + q2->x*q3->y + q3->x*pom_y - q3->x*q2->y - pom_x*q3->y - q2->x*pom_y;
  200.  
  201. if(det1 > 0) det1 = 1;
  202. if(det1 == 0) det1 = 0;
  203. if(det1 < 0) det1 = -1;
  204.  
  205. if(det2 > 0) det2 = 1;
  206. if(det2 == 0) det2 = 0;
  207. if(det2 < 0) det2 = -1;
  208.  
  209. if(det3 > 0) det3 = 1;
  210. if(det3 == 0) det3 = 0;
  211. if(det3 < 0) det3 = -1;
  212.  
  213. if(det4 > 0) det4 = 1;
  214. if(det4 == 0) det4 = 0;
  215. if(det4 < 0) det4 = -1;
  216.  
  217. if(det1 != det2 && det3 != det4) licznik++;
  218.  
  219. if(licznik % 2 == 0) return 0;
  220. else return 1;
  221. }
  222.  
  223. //GRAHAM
  224. int graham()
  225. {
  226. clear_to_color( screen, makecol( 255, 255, 255 ) );
  227. int sx = 0, sy = 0, max_y = 600;
  228. punkt *nowy;
  229. punkt *nowy2;
  230. punkt *pom;
  231. punkt *pom2;
  232. punkt *s;
  233. punkt *q;
  234. bool zamiana;
  235. int n;
  236.  
  237. if(licz() < 3)
  238. {
  239. textprintf_ex( screen, font, 20, 20, makecol( 255, 0, 0 ), - 1, "ZA MALO PUNKTOW, DODAJ MINIMUM 3!!!" );
  240. }
  241. else
  242. {
  243. //rys_pkt();
  244. n = licz();
  245. //obliczanie centroidu
  246. nowy = head;
  247. while(nowy != NULL)
  248. {
  249. sx += nowy->x;
  250. sy += nowy->y;
  251. nowy = nowy->next;
  252. }
  253. sx = sx/n;
  254. sy = sy/n;
  255. //obliczanie wspolczynnikow alfa
  256. nowy = head;
  257. while(nowy != NULL)
  258. {
  259. nowy->a = alfa(nowy, sx, sy);
  260. nowy = nowy->next;
  261. }
  262. //sortowanie punktow
  263. while(true)
  264. {
  265. nowy = head;
  266. nowy2 = nowy->next;
  267. zamiana = false;
  268. while(nowy->next != NULL)
  269. {
  270.  
  271. if(nowy->a > nowy2->a)
  272. {
  273. zamiana = true;
  274.  
  275. int pomx = nowy->x;
  276. int pomy = nowy->y;
  277. double poma = nowy->a;
  278. nowy->x = nowy2->x;
  279. nowy->y = nowy2->y;
  280. nowy->a = nowy2->a;
  281. nowy2->x = pomx;
  282. nowy2->y = pomy;
  283. nowy2->a = poma;
  284. }
  285. nowy = nowy->next;
  286. nowy2 = nowy2->next;
  287. /*
  288. if(nowy == head)
  289. {
  290. if(nowy->a > nowy->next->a)
  291. {
  292. zamiana = true;
  293.  
  294. pom = nowy->next;
  295. nowy->next = nowy;
  296. nowy->next->next = pom->next;
  297. pom->next = nowy->next;
  298. nowy = pom;
  299. head = nowy;
  300. }
  301. nowy = nowy->next;
  302. }
  303. else if(nowy->next == tail)
  304. {
  305. if(nowy->a > nowy->next->a)
  306. {
  307. zamiana = true;
  308.  
  309. pom = nowy->next;
  310. nowy->next = nowy;
  311. nowy->next->next = pom->next;
  312. pom->next = nowy->next;
  313. nowy = pom;
  314. tail = nowy->next;
  315. tail->next = NULL;
  316. }
  317. nowy = nowy->next;
  318. }
  319. else
  320. {
  321. if(nowy->a > nowy->next->a)
  322. {
  323. zamiana = true;
  324.  
  325. pom = nowy->next;
  326. nowy->next = nowy;
  327. nowy->next->next = pom->next;
  328. pom->next = nowy->next;
  329. nowy = pom;
  330. }
  331. nowy = nowy->next;
  332. }
  333. */
  334. }
  335. if(!zamiana) break;
  336. }
  337.  
  338. rys_pkt();
  339. //circlefill( screen, sx, sy, 2, makecol( 255, 0, 0 ) );
  340.  
  341. //szukanie punktu o najmniejszym y
  342. nowy = head;
  343. while(nowy->next != NULL)
  344. {
  345. if(nowy->y < max_y)
  346. {
  347. max_y = nowy->y;
  348. s = nowy;
  349. }
  350. nowy = nowy->next;
  351. }
  352.  
  353. //ustawienie head i tail tak zeby zapetlic liste
  354. tail->next = head;
  355. head->prev = tail;
  356. //usuwanie punktow nie nalezacych do otoczki
  357. q = s;
  358. while(q->next != s)
  359. {
  360. if(spr(q, q->next, q->next->next, sx, sy) == 1)
  361. {
  362. q->next = q->next->next;
  363. q->next->prev = q;
  364. if(q != s) q = q->prev;
  365. }
  366. else q = q->next;
  367. }
  368. //rysowanie otoczki
  369. q = s;
  370. while(q->next != s)
  371. {
  372. line( screen, q->x, q->y, q->next->x, q->next->y, makecol( 255, 0, 0 ) );
  373. q = q->next;
  374. }
  375. line( screen, q->x, q->y, q->next->x, q->next->y, makecol( 255, 0, 0 ) );
  376. //ustanienie head na punkt s oraz tail na punkt s->prev
  377. pom = s;
  378. pom2 = s->prev;
  379.  
  380. pom->prev = NULL;
  381. pom2->next = NULL;
  382.  
  383. head = pom;
  384. tail = pom2;
  385. }
  386.  
  387. return D_O_K;
  388. }
  389. //JARVIS
  390. int jarvis()
  391. {
  392. clear_to_color( screen, makecol( 255, 255, 255 ) );
  393.  
  394. if(licz() < 3)
  395. {
  396. textprintf_ex( screen, font, 20, 20, makecol( 255, 0, 0 ), - 1, "ZA MALO PUNKTOW, DODAJ MINIMUM 3!!!" );
  397. }
  398. else
  399. {
  400. //algorytm
  401. }
  402.  
  403. return D_O_K;
  404. }
  405. //CZYSZCZENIE EKRANU
  406. int czysc()
  407. {
  408. clear_to_color( screen, makecol( 255, 255, 255 ) );
  409.  
  410. return D_O_K;
  411. }
  412. //KONIEC
  413. int quitter()
  414. {
  415. return D_CLOSE;
  416. }
  417.  
  418. //MENU
  419.  
  420. MENU file_menu[] =
  421. {
  422. { "&Wyczysc ekran", czysc, NULL, 0, NULL },
  423. { "", NULL, NULL, 0, NULL },
  424. { "&Wyjscie", quitter, NULL, 0, NULL },
  425. { NULL, NULL, NULL, 0, NULL }
  426. };
  427.  
  428. MENU zadania_menu[] =
  429. {
  430. { "&Graham", graham, NULL, 0, NULL },
  431. { "&Jarvis", jarvis, NULL, 0, NULL },
  432. { "&Dodaj punkty", dodawanie, NULL, 0, NULL },
  433. { "&Narysuj punkty", rys_pkt, NULL, 0, NULL },
  434. { NULL, NULL, NULL, 0, NULL }
  435. };
  436.  
  437. MENU main_menu[] =
  438. {
  439. { "&Plik", NULL, file_menu, 0, NULL },
  440. { "&Zadania", NULL, zadania_menu, 0, NULL },
  441. { NULL, NULL, NULL, 0, NULL }
  442. };
  443.  
  444. /* glowny dialog */
  445.  
  446. DIALOG main_dialog[] =
  447. {
  448. /* (dialog proc) (x) (y) (w) (h) (fg) (bg) (key) (flags) (d1) (d2) (dp) */
  449. { d_clear_proc, 0, 0, 800, 600, 0, 0, 0, 0, 0, 0, NULL },
  450. { d_menu_proc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, main_menu },
  451. { NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL }
  452. };
  453.  
  454. int main()
  455. {
  456. allegro_init();
  457. install_keyboard();
  458. install_mouse();
  459. install_timer();
  460. set_color_depth( 8 );
  461. set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
  462. set_palette(desktop_palette);
  463.  
  464. do_dialog(main_dialog, -1);
  465.  
  466. return 0;
  467. }
  468. END_OF_MAIN()
Add Comment
Please, Sign In to add comment