Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.90 KB | None | 0 0
  1. #ifdef __cplusplus
  2. #include <cstdlib>
  3. #else
  4. #include <stdlib.h>
  5.  
  6. #endif
  7. #include <SDL/SDL.h>
  8. #include <math.h>
  9. #define pi 3.1415926536
  10. #include <time.h>
  11. #include <iostream>
  12. using namespace std;
  13.  
  14. SDL_Surface *screen;
  15. int width = 900;
  16. int height = 600;
  17. char const* tytul = "GKiM2019 - Lab 6 - Liszka Bartosz";
  18.  
  19. struct linia
  20. {
  21. float xp;
  22. float yp;
  23. float xk;
  24. float yk;
  25. };
  26.  
  27. linia wzorzec[] = { {100, 100, 100, 120},
  28. {100, 120, 110, 120},
  29. {110, 120, 110, 160},
  30. {110, 160, 130, 160},
  31. {130, 160, 130, 120},
  32. {130, 120, 140, 120},
  33. {140, 120, 120, 100},
  34. {120, 100, 100, 100} };
  35.  
  36. linia mlotek[8];
  37.  
  38.  
  39.  
  40.  
  41. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B);
  42. SDL_Color getPixel (int x, int y);
  43.  
  44. void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
  45.  
  46. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
  47. void Okrag(int x, int y, int r, Uint8 R, Uint8 G, Uint8 B);
  48. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B);
  49.  
  50. void inicjalizacja()
  51. {
  52. for(int i=0; i<8; i++)
  53. {
  54. mlotek[i] = wzorzec[i];
  55. }
  56.  
  57. }
  58.  
  59. void rysowanie(Uint8 R, Uint8 G, Uint8 B)
  60. {
  61. for(int i=0; i<8; i++)
  62. {
  63. Linia(mlotek[i].xp, mlotek[i].yp,
  64. mlotek[i].xk, mlotek[i].yk, R, G, B);
  65. }
  66.  
  67. }
  68.  
  69.  
  70. void obrotL() {
  71. float x1[8], x2[8], y1[8], y2[8], srX = 0, srY = 0;
  72.  
  73. // Srodek ciezkosci
  74. for(int i = 0; i < 8; i++)
  75. {
  76. srX += mlotek[i].xp;
  77. srY += mlotek[i].yp;
  78. }
  79. srX /= 8;
  80. srY /= 8;
  81.  
  82. for(int i = 0; i < 8; i++)
  83. {
  84. mlotek[i].xp -= srX;
  85. mlotek[i].yp -= srY;
  86. mlotek[i].xk -= srX;
  87. mlotek[i].yk -= srY;
  88. }
  89.  
  90. for(int i = 0; i < 8; i++)
  91. {
  92.  
  93. x1[i] = round(mlotek[i].xp * sqrt(3) * 0.5 + mlotek[i].yp * 0.5);
  94. y1[i] = round(mlotek[i].yp * sqrt(3) * 0.5 - mlotek[i].xp * 0.5);
  95. x2[i] = round(mlotek[i].xk * sqrt(3) * 0.5 + mlotek[i].yk * 0.5);
  96. y2[i] = round(mlotek[i].yk * sqrt(3) * 0.5 - mlotek[i].xk * 0.5);
  97. }
  98.  
  99. for(int i = 0; i < 8; i++)
  100. {
  101. mlotek[i].xp = x1[i]+srX;
  102. mlotek[i].yp = y1[i]+srY;
  103. mlotek[i].xk = x2[i]+srX;
  104. mlotek[i].yk = y2[i]+srY;
  105. }
  106. }
  107.  
  108. void obrotP() {
  109. float x1[8], x2[8], y1[8], y2[8], srX = 0, srY = 0;
  110.  
  111. // Srodek ciezkosci
  112. for(int i = 0; i < 8; i++)
  113. {
  114. srX += mlotek[i].xp;
  115. srY += mlotek[i].yp;
  116. }
  117. srX /= 8;
  118. srY /= 8;
  119.  
  120. for(int i = 0; i < 8; i++)
  121. {
  122. mlotek[i].xp -= srX;
  123. mlotek[i].yp -= srY;
  124. mlotek[i].xk -= srX;
  125. mlotek[i].yk -= srY;
  126. }
  127.  
  128. for(int i = 0; i < 8; i++)
  129. {
  130.  
  131. x1[i] = round(mlotek[i].xp * sqrt(3) * 0.5 - mlotek[i].yp * 0.5);
  132. y1[i] = round(mlotek[i].yp * sqrt(3) * 0.5 + mlotek[i].xp * 0.5);
  133. x2[i] = round(mlotek[i].xk * sqrt(3) * 0.5 - mlotek[i].yk * 0.5);
  134. y2[i] = round(mlotek[i].yk * sqrt(3) * 0.5 + mlotek[i].xk * 0.5);
  135. }
  136.  
  137. for(int i = 0; i < 8; i++)
  138. {
  139. mlotek[i].xp = x1[i]+srX;
  140. mlotek[i].yp = y1[i]+srY;
  141. mlotek[i].xk = x2[i]+srX;
  142. mlotek[i].yk = y2[i]+srY;
  143. }
  144. }
  145.  
  146. void Funkcja1();
  147. void Funkcja2();
  148. void Funkcja3();
  149. void Funkcja4();
  150. void Funkcja5();
  151. void Funkcja6();
  152. void Funkcja7();
  153. void Funkcja8();
  154. void Funkcja9();
  155.  
  156. void algorytm1(float x, float y){
  157. float a=x/y;
  158. int xx, yy;
  159. if(a>=1){
  160. for(xx=0; xx<=x; xx++){
  161. yy=round(xx/a);
  162. setPixel(xx,yy,255,255,255);
  163. }
  164. }
  165. else{
  166. for(yy=0; yy<=y; yy++){
  167. xx=round(yy*a);
  168. setPixel(xx,yy,255,0,0);
  169. }
  170. }
  171. cout<<a;
  172. }
  173.  
  174. void algorytm3(float x1, float y1,float x2, float y2){
  175. if(x1<=x2 && y1<=y2 ){
  176. float a=(x2-x1)/(y2-y1);
  177. int xx, yy;
  178. if(a>=1){
  179. for(xx=x1; xx<=x2; xx++){
  180. yy=round(xx/a);
  181. setPixel(xx,yy,255,255,255);
  182. }
  183. }
  184. else{
  185. for(yy=y1; yy<=y2; yy++){
  186. xx=round(yy*a);
  187. setPixel(xx,yy,255,0,0);
  188. }
  189. }
  190. cout<<a;
  191. }
  192. else if(x1>x2 && y1<y2){
  193. float a=(x1-x2)/(y2-y1);
  194. int xx, yy;
  195. if(a>=1){
  196. int xxx=x1;
  197. for(xx=x2; xx<=x1; xx++){
  198. yy=round(xx/a);
  199. setPixel(xxx,yy,0,255,0);
  200. xxx--;
  201.  
  202. SDL_Flip(screen);
  203. }
  204. }
  205. else{
  206. int yyy=y1;
  207. for(yy=y2; yy>=y1; yy--){
  208. xx=round(yy*a);
  209. setPixel(xx,yyy,0,0,255);
  210. yyy++;
  211. SDL_Flip(screen);
  212. }
  213. }
  214. }
  215. else if(x1<x2 && y1>y2){
  216. float a=(x2-x1)/(y1-y2);
  217. int xx, yy;
  218. if(a>=1){
  219. int xxx= x1;
  220. for(xx=x2; xx>=x1; xx--){
  221. yy=round(xx/a);
  222. setPixel(xxx,yy,0,255,0);
  223. xxx++;
  224. SDL_Flip(screen);
  225. }
  226. }
  227. else{
  228. int yyy=y1;
  229. for(yy=y2; yy<=y1; yy++){
  230. xx=round(yy*a);
  231. setPixel(xx,yyy,0,0,255);
  232. yyy--;
  233. SDL_Flip(screen);
  234. }
  235. }
  236. }
  237.  
  238. else{
  239. float a=(x1-x2)/(y1-y2);
  240. int xx, yy;
  241. if(a>=1){
  242. for(xx=x1; xx>=x2; xx--){
  243. yy=round(xx/a);
  244. setPixel(xx,yy,0,255,255);
  245. SDL_Flip(screen);
  246. }
  247. }
  248. else{
  249. for(yy=y1; yy>=y2; yy--){
  250. xx=round(yy*a);
  251. setPixel(xx,yy,200,0,0);
  252. SDL_Flip(screen);
  253. }
  254. }
  255. }
  256.  
  257. SDL_Flip(screen);
  258. }
  259.  
  260. void algorytm2(float x, float y){
  261. float a=x/y;
  262. int xx,yy;
  263. if(a>=1){
  264. for(xx=0;xx<=x;xx++){
  265. yy=round(xx/a + 1/a);
  266. setPixel(xx,yy,255,255,255);
  267.  
  268. }
  269. }
  270. else{
  271. for(yy=0;yy<=y;yy++){
  272. xx=round(yy*a + 1);
  273. setPixel(xx,yy,255,0,0);
  274.  
  275. }
  276.  
  277. }
  278.  
  279. }
  280.  
  281. void algorytm4(float x1, float y1,float x2, float y2){
  282. if(x1<x2 && y1<y2){
  283. float a=(x2-x1)/(y2-y1);
  284. int xx,yy;
  285. if(a>=1){
  286. for(xx=x1;xx<=x2;xx++){
  287. yy=round(xx/a + 1/a);
  288. setPixel(xx,yy,255,255,255);
  289.  
  290. }
  291. }
  292. else{
  293. for(yy=y1;yy<=y2;yy++){
  294. xx=round(yy*a + 1);
  295. setPixel(xx,yy,255,0,0);
  296.  
  297. }
  298.  
  299. }
  300. }
  301. else if(x1>x2 && y1<y2){
  302. float a=(x1-x2)/(y2-y1);
  303. int xx,yy;
  304. if(a>=1){
  305. int xxx=x1;
  306. for(xx=x2;xx<=x1;xx++){
  307. yy=round(xx/a + 1/a);
  308. setPixel(xxx,yy,255,255,255);
  309. xxx--;
  310. }
  311. }
  312. else{
  313. int yyy=y1;
  314. for(yy=y2;yy>=y1;yy--){
  315. xx=round(yy*a + 1);
  316. setPixel(xx,yyy,255,0,0);
  317. yyy++;
  318. }
  319.  
  320. }
  321. }
  322. else if(x1<x2 && y1>y2){
  323. float a=(x2-x1)/(y1-y2);
  324. int xx,yy;
  325. if(a>=1){
  326. int xxx=x1;
  327. for(xx=x2;xx>=x1;xx--){
  328. yy=round(xx/a + 1/a);
  329. setPixel(xxx,yy,255,255,255);
  330. xxx++;
  331. }
  332. }
  333. else{
  334. int yyy=y1;
  335. for(yy=y2;yy<=y1;yy++){
  336. xx=round(yy*a + 1);
  337. setPixel(xx,yyy,255,0,0);
  338. yyy--;
  339. }
  340.  
  341. }
  342. }
  343. else{
  344. float a=(x1-x2)/(y1-y2);
  345. int xx,yy;
  346. if(a>=1){
  347.  
  348. for(xx=x1;xx>=x2;xx--){
  349. yy=round(xx/a + 1/a);
  350. setPixel(xx,yy,255,255,255);
  351.  
  352. }
  353. }
  354. else{
  355.  
  356. for(yy=y1;yy<=y2;yy--){
  357. xx=round(yy*a + 1);
  358. setPixel(xx,yy,255,0,0);
  359.  
  360. }
  361.  
  362. }
  363. }
  364. }
  365.  
  366.  
  367. void Funkcja1() {
  368.  
  369. algorytm1(rand()%900, rand()%600);
  370.  
  371. SDL_Flip(screen);
  372. }
  373.  
  374.  
  375. void Funkcja2() {
  376.  
  377. algorytm4(100,300,200,100);
  378.  
  379.  
  380. SDL_Flip(screen);
  381. }
  382.  
  383. void Funkcja3() {
  384.  
  385. algorytm3(rand()%900, rand()%600,rand()%900, rand()%600);
  386.  
  387.  
  388. SDL_Flip(screen);
  389. }
  390.  
  391. void Funkcja4() {
  392. int yy=height/20;
  393. int xx;
  394. for(int xx=0;xx<=width;xx+=width/20){
  395. Linia(xx,0,width,yy,255,255,255);
  396. yy+=height/20;
  397. }
  398.  
  399. xx=width-width/20;
  400. for(yy=0;yy<=height;yy+=height/20){
  401.  
  402. Linia(width,yy,xx,height,255,0,0);
  403. xx-=width/20;
  404. }
  405.  
  406. yy=height-height/20;
  407. for(xx=width;xx>=0;xx-=width/20){
  408. Linia(xx,height,0,yy,255,255,0);
  409. yy-=height/20;
  410. }
  411.  
  412. xx=width/20;
  413. for(yy=height;yy>=0;yy-=height/20){
  414. Linia(0,yy,xx,0,0,0,255);
  415. xx+=width/20;
  416.  
  417.  
  418. }
  419.  
  420. SDL_Flip(screen);
  421. }
  422.  
  423.  
  424. void Funkcja5() {
  425.  
  426. inicjalizacja();
  427. rysowanie(255,255,255);
  428.  
  429.  
  430.  
  431. SDL_Flip(screen);
  432. }
  433.  
  434. void Funkcja6() {
  435. rysowanie(0,0,0);
  436. obrotL();
  437. rysowanie(255,255,255);
  438.  
  439. SDL_Flip(screen);
  440. }
  441.  
  442.  
  443. void Funkcja7() {
  444.  
  445. rysowanie(0,0,0);
  446. obrotP();
  447. rysowanie(255,255,255);
  448.  
  449.  
  450. SDL_Flip(screen);
  451. }
  452.  
  453.  
  454. void Funkcja8() {
  455.  
  456. algorytm4(rand()%900, rand()%600,rand()%900, rand()%600);
  457.  
  458. SDL_Flip(screen);
  459. }
  460.  
  461.  
  462. void Funkcja9() {
  463.  
  464. algorytm4(rand()%900, rand()%600,rand()%900, rand()%600);
  465.  
  466. SDL_Flip(screen);
  467. }
  468.  
  469.  
  470. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
  471. setPixel(x1,y1,R,G,B);
  472. setPixel(x2,y2,R,G,B);
  473. // zmienne pomocnicze
  474. int d, dx, dy, ai, bi, xi, yi;
  475. int x = x1, y = y1;
  476. // ustalenie kierunku rysowania
  477. if (x1 < x2)
  478. {
  479. xi = 1;
  480. dx = x2 - x1;
  481. }
  482. else
  483. {
  484. xi = -1;
  485. dx = x1 - x2;
  486. }
  487. // ustalenie kierunku rysowania
  488. if (y1 < y2)
  489. {
  490. yi = 1;
  491. dy = y2 - y1;
  492. }
  493. else
  494. {
  495. yi = -1;
  496. dy = y1 - y2;
  497. }
  498. // pierwszy piksel
  499. setPixel(x, y, R, G, B);
  500. // oś wiodąca OX
  501. if (dx > dy)
  502. {
  503. ai = (dy - dx) * 2;
  504. bi = dy * 2;
  505. d = bi - dx;
  506. // pętla po kolejnych x
  507. while (x != x2)
  508. {
  509. // test współczynnika
  510. if (d >= 0)
  511. {
  512. x += xi;
  513. y += yi;
  514. d += ai;
  515. }
  516. else
  517. {
  518. d += bi;
  519. x += xi;
  520. }
  521. setPixel(x, y, R, G, B);
  522. }
  523. }
  524. // oś wiodąca OY
  525. else
  526. {
  527. ai = ( dx - dy ) * 2;
  528. bi = dx * 2;
  529. d = bi - dy;
  530. // pętla po kolejnych y
  531. while (y != y2)
  532. {
  533. // test współczynnika
  534. if (d >= 0)
  535. {
  536. x += xi;
  537. y += yi;
  538. d += ai;
  539. }
  540. else
  541. {
  542. d += bi;
  543. y += yi;
  544. }
  545. setPixel(x, y, R, G, B);
  546. }
  547. }
  548. SDL_Flip(screen);
  549. }
  550.  
  551. void RysujOkrag(int x0, int y0, int x, int y, Uint8 R, Uint8 G, Uint8 B){
  552. setPixel(x+x0, y+y0, R, G, B);
  553. setPixel(y+x0, x+y0, R, G, B);
  554. setPixel(y+x0, -x+y0, R, G, B);
  555. setPixel(x+x0, -y+y0, R, G, B);
  556. setPixel(-x+x0, -y+y0, R, G, B);
  557. setPixel(-y+x0, -x+y0, R, G, B);
  558. setPixel(-y+x0, x+y0, R, G, B);
  559. setPixel(-x+x0, y+y0, R, G, B);;
  560. }
  561.  
  562.  
  563. void Okrag(int x0, int y0, int r, Uint8 R, Uint8 G, Uint8 B) {
  564. int x =0;
  565. int y=r;
  566. int d = 3-2*r;
  567.  
  568. while (x <= y){
  569. if (d<0){
  570. d=d+4*x+6;
  571. RysujOkrag(x0, y0, x, y, R, G, B);
  572. } else{
  573. d=d+4*(x-y)+10;
  574. y--;
  575. RysujOkrag(x0, y0, x, y, R, G, B);
  576. }
  577. x++;
  578. }
  579. SDL_Flip(screen);
  580. }
  581.  
  582. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
  583. int xc = 0,
  584. yc = b;
  585. int aa = a * a,
  586. aa2 = aa + aa,
  587. bb = b * b,
  588. bb2 = bb + bb;
  589. int d = bb - aa * b + (aa / 4),
  590. dx = 0,
  591. dy = aa2 * b;
  592.  
  593. while (dx < dy) {
  594. setPixel (x - xc,y - yc, R, G, B);
  595. setPixel (x - xc,y + yc, R, G, B);
  596. setPixel (x + xc,y - yc, R, G, B);
  597. setPixel (x + xc,y + yc, R, G, B);
  598. if (d > 0){
  599. yc = yc-1;
  600. dy -= aa2;
  601. d -= dy;
  602. }
  603. xc =xc+1;
  604. dx += bb2;
  605. d += bb + dx;
  606. }
  607. d += (3 * ((aa - bb) / 2) - (dx + dy) / 2);
  608.  
  609. while (yc >= 0) {
  610. setPixel (x - xc,y - yc, R, G, B);
  611. setPixel (x - xc,y + yc, R, G, B);
  612. setPixel (x + xc,y - yc, R, G, B);
  613. setPixel (x + xc,y + yc, R, G, B);
  614. if (d < 0) {
  615. xc =xc+1;
  616. dx += bb2;
  617. d += (bb + dx);
  618. }
  619. yc = yc-1;
  620. dy -= aa2;
  621. d += aa - dy;
  622. }
  623. SDL_Flip(screen);
  624. }
  625.  
  626.  
  627.  
  628. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  629. {
  630. if ((x>=0) && (x<width) && (y>=0) && (y<height))
  631. {
  632. /* Zamieniamy poszczególne składowe koloru na format koloru piksela */
  633. Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  634.  
  635. /* Pobieramy informację ile bajtów zajmuje jeden piksel */
  636. int bpp = screen->format->BytesPerPixel;
  637.  
  638. /* Obliczamy adres piksela */
  639. Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
  640.  
  641. /* Ustawiamy wartość piksela, w zależności od formatu powierzchni*/
  642. switch(bpp)
  643. {
  644. case 1: //8-bit
  645. *p = pixel;
  646. break;
  647.  
  648. case 2: //16-bit
  649. *(Uint16 *)p = pixel;
  650. break;
  651.  
  652. case 3: //24-bit
  653. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  654. p[0] = (pixel >> 16) & 0xff;
  655. p[1] = (pixel >> 8) & 0xff;
  656. p[2] = pixel & 0xff;
  657. } else {
  658. p[0] = pixel & 0xff;
  659. p[1] = (pixel >> 8) & 0xff;
  660. p[2] = (pixel >> 16) & 0xff;
  661. }
  662. break;
  663.  
  664. case 4: //32-bit
  665. *(Uint32 *)p = pixel;
  666. break;
  667.  
  668. }
  669. /* ewentualna aktualizacja obrazu (aka double buffering) */
  670. }
  671. }
  672.  
  673.  
  674. SDL_Color getPixel (int x, int y) {
  675. SDL_Color color ;
  676. Uint32 col = 0 ;
  677. if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
  678. //określamy pozycję
  679. char* pPosition=(char*)screen->pixels ;
  680. //przesunięcie względem y
  681. pPosition+=(screen->pitch*y) ;
  682. //przesunięcie względem x
  683. pPosition+=(screen->format->BytesPerPixel*x);
  684. //kopiujemy dane piksela
  685. memcpy(&col, pPosition, screen->format->BytesPerPixel);
  686. //konwertujemy kolor
  687. SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  688. }
  689. return ( color ) ;
  690. }
  691.  
  692. void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
  693. {
  694. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  695. SDL_Flip(screen);
  696.  
  697. }
  698.  
  699.  
  700.  
  701. int main ( int argc, char** argv )
  702. {
  703. // console output
  704. freopen( "CON", "wt", stdout );
  705. freopen( "CON", "wt", stderr );
  706.  
  707. // initialize SDL video
  708. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  709. {
  710. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  711. return 1;
  712. }
  713.  
  714. // make sure SDL cleans up before exit
  715. atexit(SDL_Quit);
  716.  
  717. // create a new window
  718. screen = SDL_SetVideoMode(width, height, 32,
  719. SDL_HWSURFACE|SDL_DOUBLEBUF);
  720. if ( !screen )
  721. {
  722. printf("Unable to set video: %s\n", SDL_GetError());
  723. return 1;
  724. }
  725.  
  726. SDL_WM_SetCaption( tytul , NULL );
  727. // program main loop
  728. bool done = false;
  729. while (!done)
  730. {
  731. // message processing loop
  732. SDL_Event event;
  733. while (SDL_PollEvent(&event))
  734. {
  735. // check for messages
  736. switch (event.type)
  737. {
  738. // exit if the window is closed
  739. case SDL_QUIT:
  740. done = true;
  741. break;
  742.  
  743. // check for keypresses
  744. case SDL_KEYDOWN:
  745. {
  746. // exit if ESCAPE is pressed
  747. if (event.key.keysym.sym == SDLK_ESCAPE)
  748. done = true;
  749. if (event.key.keysym.sym == SDLK_1)
  750. Funkcja1();
  751. if (event.key.keysym.sym == SDLK_2)
  752. Funkcja2();
  753. if (event.key.keysym.sym == SDLK_3)
  754. Funkcja3();
  755. if (event.key.keysym.sym == SDLK_4)
  756. Funkcja4();
  757. if (event.key.keysym.sym == SDLK_5)
  758. Funkcja5();
  759. if (event.key.keysym.sym == SDLK_6)
  760. Funkcja6();
  761. if (event.key.keysym.sym == SDLK_7)
  762. Funkcja7();
  763. if (event.key.keysym.sym == SDLK_b)
  764. czyscEkran(0, 0, 10); break;
  765. }
  766. } // end switch
  767. } // end of message processing
  768.  
  769. } // end main loop
  770.  
  771.  
  772. // all is well ;)
  773. printf("Exited cleanly\n");
  774. return 0;
  775. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement