Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.46 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. SDL_Flip(screen);
  194. }
  195. }
  196. }
  197. else if(x1<x2 && y1>y2){
  198. float a=(x2-x1)/(y1-y2);
  199. int xx, yy;
  200. if(a>=1){
  201. int yyy=y1;
  202. for(xx=x1; xx<=x2; xx++){
  203. yy=round(xx/a);
  204. setPixel(xx,yy,0,255,0);
  205. yyy--;
  206. SDL_Flip(screen);
  207. }
  208. }
  209. else{
  210. int yyy=y1;
  211. for(yy=y2; yy<=y1; yy++){
  212. xx=round(yy*a);
  213. setPixel(xx,yyy,0,0,255);
  214. yyy--;
  215. SDL_Flip(screen);
  216. }
  217. }
  218. }
  219.  
  220. else{
  221. float a=(x1-x2)/(y1-y2);
  222. int xx, yy;
  223. if(a>=1){
  224. for(xx=x1; xx>=x2; xx--){
  225. yy=round(xx/a);
  226. setPixel(xx,yy,0,255,255);
  227. SDL_Flip(screen);
  228. }
  229. }
  230. else{
  231. for(yy=y1; yy>=y2; yy--){
  232. xx=round(yy*a);
  233. setPixel(xx,yy,200,0,0);
  234. SDL_Flip(screen);
  235. }
  236. }
  237. }
  238.  
  239. SDL_Flip(screen);
  240. }
  241.  
  242. void algorytm2(float x, float y){
  243. float a=x/y;
  244. int xx,yy;
  245. if(a>=1){
  246. for(xx=0;xx<=x;xx++){
  247. yy=round(xx/a + 1/a);
  248. setPixel(xx,yy,255,255,255);
  249.  
  250. }
  251. }
  252. else{
  253. for(yy=0;yy<=y;yy++){
  254. xx=round(yy*a + 1);
  255. setPixel(xx,yy,255,0,0);
  256.  
  257. }
  258.  
  259. }
  260.  
  261. }
  262.  
  263. void algorytm2(float x1, float y1,float x2, float y2){
  264. if(x1<x2 && y1<y2){
  265. float a=(x2-x1)/(y2-y1);
  266. int xx,yy;
  267. if(a>=1){
  268. for(xx=0;xx<=x;xx++){
  269. yy=round(xx/a + 1/a);
  270. setPixel(xx,yy,255,255,255);
  271.  
  272. }
  273. }
  274. else{
  275. for(yy=0;yy<=y;yy++){
  276. xx=round(yy*a + 1);
  277. setPixel(xx,yy,255,0,0);
  278.  
  279. }
  280.  
  281. }
  282. }
  283. else if(x1>x2 && y1<y2)
  284. float a=(x1-x2)/(y2-y1);
  285. int xx,yy;
  286. if(a>=1){
  287. for(xx=0;xx<=x;xx++){
  288. yy=round(xx/a + 1/a);
  289. setPixel(xx,yy,255,255,255);
  290.  
  291. }
  292. }
  293. else{
  294. for(yy=0;yy<=y;yy++){
  295. xx=round(yy*a + 1);
  296. setPixel(xx,yy,255,0,0);
  297.  
  298. }
  299.  
  300. }
  301. }
  302. }
  303.  
  304.  
  305. void Funkcja1() {
  306.  
  307. algorytm1(rand()%900, rand()%600);
  308.  
  309. SDL_Flip(screen);
  310. }
  311.  
  312.  
  313. void Funkcja2() {
  314.  
  315. algorytm2(rand()%900, rand()%600);
  316.  
  317. SDL_Flip(screen);
  318. }
  319.  
  320. void Funkcja3() {
  321.  
  322. algorytm3(rand()%900, rand()%600,rand()%900, rand()%600);
  323.  
  324. SDL_Flip(screen);
  325. }
  326.  
  327. void Funkcja4() {
  328. int yy=height/20;
  329. int xx;
  330. for(int xx=0;xx<=width;xx+=width/20){
  331. Linia(xx,0,width,yy,255,255,255);
  332. yy+=height/20;
  333. }
  334.  
  335. xx=width-width/20;
  336. for(yy=0;yy<=height;yy+=height/20){
  337.  
  338. Linia(width,yy,xx,height,255,0,0);
  339. xx-=width/20;
  340. }
  341.  
  342. yy=height-height/20;
  343. for(xx=width;xx>=0;xx-=width/20){
  344. Linia(xx,height,0,yy,255,255,0);
  345. yy-=height/20;
  346. }
  347.  
  348. xx=width/20;
  349. for(yy=height;yy>=0;yy-=height/20){
  350. Linia(0,yy,xx,0,0,0,255);
  351. xx+=width/20;
  352.  
  353.  
  354. }
  355.  
  356. SDL_Flip(screen);
  357. }
  358.  
  359.  
  360. void Funkcja5() {
  361.  
  362. inicjalizacja();
  363. rysowanie(255,255,255);
  364.  
  365.  
  366.  
  367. SDL_Flip(screen);
  368. }
  369.  
  370. void Funkcja6() {
  371. rysowanie(0,0,0);
  372. obrotL();
  373. rysowanie(255,255,255);
  374.  
  375. SDL_Flip(screen);
  376. }
  377.  
  378.  
  379. void Funkcja7() {
  380.  
  381. rysowanie(0,0,0);
  382. obrotP();
  383. rysowanie(255,255,255);
  384.  
  385.  
  386. SDL_Flip(screen);
  387. }
  388.  
  389.  
  390. void Funkcja8() {
  391.  
  392. //...
  393.  
  394. SDL_Flip(screen);
  395. }
  396.  
  397.  
  398. void Funkcja9() {
  399.  
  400. //...
  401.  
  402. SDL_Flip(screen);
  403. }
  404.  
  405.  
  406. void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
  407. setPixel(x1,y1,R,G,B);
  408. setPixel(x2,y2,R,G,B);
  409. // zmienne pomocnicze
  410. int d, dx, dy, ai, bi, xi, yi;
  411. int x = x1, y = y1;
  412. // ustalenie kierunku rysowania
  413. if (x1 < x2)
  414. {
  415. xi = 1;
  416. dx = x2 - x1;
  417. }
  418. else
  419. {
  420. xi = -1;
  421. dx = x1 - x2;
  422. }
  423. // ustalenie kierunku rysowania
  424. if (y1 < y2)
  425. {
  426. yi = 1;
  427. dy = y2 - y1;
  428. }
  429. else
  430. {
  431. yi = -1;
  432. dy = y1 - y2;
  433. }
  434. // pierwszy piksel
  435. setPixel(x, y, R, G, B);
  436. // oś wiodąca OX
  437. if (dx > dy)
  438. {
  439. ai = (dy - dx) * 2;
  440. bi = dy * 2;
  441. d = bi - dx;
  442. // pętla po kolejnych x
  443. while (x != x2)
  444. {
  445. // test współczynnika
  446. if (d >= 0)
  447. {
  448. x += xi;
  449. y += yi;
  450. d += ai;
  451. }
  452. else
  453. {
  454. d += bi;
  455. x += xi;
  456. }
  457. setPixel(x, y, R, G, B);
  458. }
  459. }
  460. // oś wiodąca OY
  461. else
  462. {
  463. ai = ( dx - dy ) * 2;
  464. bi = dx * 2;
  465. d = bi - dy;
  466. // pętla po kolejnych y
  467. while (y != y2)
  468. {
  469. // test współczynnika
  470. if (d >= 0)
  471. {
  472. x += xi;
  473. y += yi;
  474. d += ai;
  475. }
  476. else
  477. {
  478. d += bi;
  479. y += yi;
  480. }
  481. setPixel(x, y, R, G, B);
  482. }
  483. }
  484. SDL_Flip(screen);
  485. }
  486.  
  487. void RysujOkrag(int x0, int y0, int x, int y, Uint8 R, Uint8 G, Uint8 B){
  488. setPixel(x+x0, y+y0, R, G, B);
  489. setPixel(y+x0, x+y0, R, G, B);
  490. setPixel(y+x0, -x+y0, R, G, B);
  491. setPixel(x+x0, -y+y0, R, G, B);
  492. setPixel(-x+x0, -y+y0, R, G, B);
  493. setPixel(-y+x0, -x+y0, R, G, B);
  494. setPixel(-y+x0, x+y0, R, G, B);
  495. setPixel(-x+x0, y+y0, R, G, B);;
  496. }
  497.  
  498.  
  499. void Okrag(int x0, int y0, int r, Uint8 R, Uint8 G, Uint8 B) {
  500. int x =0;
  501. int y=r;
  502. int d = 3-2*r;
  503.  
  504. while (x <= y){
  505. if (d<0){
  506. d=d+4*x+6;
  507. RysujOkrag(x0, y0, x, y, R, G, B);
  508. } else{
  509. d=d+4*(x-y)+10;
  510. y--;
  511. RysujOkrag(x0, y0, x, y, R, G, B);
  512. }
  513. x++;
  514. }
  515. SDL_Flip(screen);
  516. }
  517.  
  518. void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
  519. int xc = 0,
  520. yc = b;
  521. int aa = a * a,
  522. aa2 = aa + aa,
  523. bb = b * b,
  524. bb2 = bb + bb;
  525. int d = bb - aa * b + (aa / 4),
  526. dx = 0,
  527. dy = aa2 * b;
  528.  
  529. while (dx < dy) {
  530. setPixel (x - xc,y - yc, R, G, B);
  531. setPixel (x - xc,y + yc, R, G, B);
  532. setPixel (x + xc,y - yc, R, G, B);
  533. setPixel (x + xc,y + yc, R, G, B);
  534. if (d > 0){
  535. yc = yc-1;
  536. dy -= aa2;
  537. d -= dy;
  538. }
  539. xc =xc+1;
  540. dx += bb2;
  541. d += bb + dx;
  542. }
  543. d += (3 * ((aa - bb) / 2) - (dx + dy) / 2);
  544.  
  545. while (yc >= 0) {
  546. setPixel (x - xc,y - yc, R, G, B);
  547. setPixel (x - xc,y + yc, R, G, B);
  548. setPixel (x + xc,y - yc, R, G, B);
  549. setPixel (x + xc,y + yc, R, G, B);
  550. if (d < 0) {
  551. xc =xc+1;
  552. dx += bb2;
  553. d += (bb + dx);
  554. }
  555. yc = yc-1;
  556. dy -= aa2;
  557. d += aa - dy;
  558. }
  559. SDL_Flip(screen);
  560. }
  561.  
  562.  
  563.  
  564. void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
  565. {
  566. if ((x>=0) && (x<width) && (y>=0) && (y<height))
  567. {
  568. /* Zamieniamy poszczególne składowe koloru na format koloru piksela */
  569. Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
  570.  
  571. /* Pobieramy informację ile bajtów zajmuje jeden piksel */
  572. int bpp = screen->format->BytesPerPixel;
  573.  
  574. /* Obliczamy adres piksela */
  575. Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
  576.  
  577. /* Ustawiamy wartość piksela, w zależności od formatu powierzchni*/
  578. switch(bpp)
  579. {
  580. case 1: //8-bit
  581. *p = pixel;
  582. break;
  583.  
  584. case 2: //16-bit
  585. *(Uint16 *)p = pixel;
  586. break;
  587.  
  588. case 3: //24-bit
  589. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  590. p[0] = (pixel >> 16) & 0xff;
  591. p[1] = (pixel >> 8) & 0xff;
  592. p[2] = pixel & 0xff;
  593. } else {
  594. p[0] = pixel & 0xff;
  595. p[1] = (pixel >> 8) & 0xff;
  596. p[2] = (pixel >> 16) & 0xff;
  597. }
  598. break;
  599.  
  600. case 4: //32-bit
  601. *(Uint32 *)p = pixel;
  602. break;
  603.  
  604. }
  605. /* ewentualna aktualizacja obrazu (aka double buffering) */
  606. }
  607. }
  608.  
  609.  
  610. SDL_Color getPixel (int x, int y) {
  611. SDL_Color color ;
  612. Uint32 col = 0 ;
  613. if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
  614. //określamy pozycję
  615. char* pPosition=(char*)screen->pixels ;
  616. //przesunięcie względem y
  617. pPosition+=(screen->pitch*y) ;
  618. //przesunięcie względem x
  619. pPosition+=(screen->format->BytesPerPixel*x);
  620. //kopiujemy dane piksela
  621. memcpy(&col, pPosition, screen->format->BytesPerPixel);
  622. //konwertujemy kolor
  623. SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
  624. }
  625. return ( color ) ;
  626. }
  627.  
  628. void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
  629. {
  630. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
  631. SDL_Flip(screen);
  632.  
  633. }
  634.  
  635.  
  636.  
  637. int main ( int argc, char** argv )
  638. {
  639. // console output
  640. freopen( "CON", "wt", stdout );
  641. freopen( "CON", "wt", stderr );
  642.  
  643. // initialize SDL video
  644. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
  645. {
  646. printf( "Unable to init SDL: %s\n", SDL_GetError() );
  647. return 1;
  648. }
  649.  
  650. // make sure SDL cleans up before exit
  651. atexit(SDL_Quit);
  652.  
  653. // create a new window
  654. screen = SDL_SetVideoMode(width, height, 32,
  655. SDL_HWSURFACE|SDL_DOUBLEBUF);
  656. if ( !screen )
  657. {
  658. printf("Unable to set video: %s\n", SDL_GetError());
  659. return 1;
  660. }
  661.  
  662. SDL_WM_SetCaption( tytul , NULL );
  663. // program main loop
  664. bool done = false;
  665. while (!done)
  666. {
  667. // message processing loop
  668. SDL_Event event;
  669. while (SDL_PollEvent(&event))
  670. {
  671. // check for messages
  672. switch (event.type)
  673. {
  674. // exit if the window is closed
  675. case SDL_QUIT:
  676. done = true;
  677. break;
  678.  
  679. // check for keypresses
  680. case SDL_KEYDOWN:
  681. {
  682. // exit if ESCAPE is pressed
  683. if (event.key.keysym.sym == SDLK_ESCAPE)
  684. done = true;
  685. if (event.key.keysym.sym == SDLK_1)
  686. Funkcja1();
  687. if (event.key.keysym.sym == SDLK_2)
  688. Funkcja2();
  689. if (event.key.keysym.sym == SDLK_3)
  690. Funkcja3();
  691. if (event.key.keysym.sym == SDLK_4)
  692. Funkcja4();
  693. if (event.key.keysym.sym == SDLK_5)
  694. Funkcja5();
  695. if (event.key.keysym.sym == SDLK_6)
  696. Funkcja6();
  697. if (event.key.keysym.sym == SDLK_7)
  698. Funkcja7();
  699. if (event.key.keysym.sym == SDLK_b)
  700. czyscEkran(0, 0, 10); break;
  701. }
  702. } // end switch
  703. } // end of message processing
  704.  
  705. } // end main loop
  706.  
  707.  
  708. // all is well ;)
  709. printf("Exited cleanly\n");
  710. return 0;
  711. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement