Guest User

Untitled

a guest
Apr 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include <allegro.h>
  2. #define red makecol(255,0,0)
  3. #define green makecol(0,255,0)
  4.  
  5. double xmax,xmin,ymax,ymin;
  6.  
  7. float max(float value1, float value2)
  8. {
  9. return ( (value1 > value2) ? value1 : value2);
  10. }
  11. float min(float value1, float value2)
  12. {
  13. return ( (value1 < value2) ? value1 : value2);
  14. }
  15.  
  16. int LiangBarsky(double xw0, double yw0, double xw1, double yw1, int colour, bool bscreen, BITMAP * buf, BITMAP * buffer)
  17. {
  18. double x1,y1,x2,y2;
  19. double xwmin,ywmin,xwmax,ywmax;
  20.  
  21. x1 = xw0;
  22. y1 = yw0;
  23. x2 = xw1;
  24. y2 = yw1;
  25. xwmin = xmin;
  26. xwmax = xmax;
  27. ywmin = ymin;
  28. ywmax = ymax;
  29. /*printf("enter the endpoints of the line");
  30. scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
  31. printf("\nEnter the corners of the window");
  32. scanf("\n%d %d %d %d",&xwmin,&ywmin,&xwmax,&ywmax);
  33. */
  34. int p[5],q[5];
  35. p[1]=-(x2-x1);
  36. p[2]=x2-x1;
  37. p[3]=-(y2-y1);
  38. p[4]=y2-y1;
  39. q[1]=x1-xwmin;
  40. q[2]=xwmax-x1;
  41. q[3]=y1-ywmin;
  42. q[4]=ywmax-y1;
  43. int i;
  44. for(i=1;i<5;i++)
  45. {
  46. if(p[i]==0)
  47. {
  48. //linia jest rownolegla
  49. if (q[i]<0)
  50. {
  51. //Linia jest poza obszarem, rysujemy tylko kwadrat
  52. rect(buf,xwmin,ywmin,xwmax,ywmax,red);
  53. return(0);
  54. }
  55. }
  56. }
  57. float u1=0,u2=1;
  58. float r[5];
  59. for(i=1;i<5;i++)
  60. { if(p[i]!=0)
  61. r[i]=q[i]*1.0/p[i];
  62. }
  63.  
  64. for(i=1;i<5;i++)
  65. {
  66. if(p[i]==0)
  67. continue;
  68. if(p[i]<0)
  69. {
  70. u1=max(u1, r[i]);
  71. }
  72. else
  73. { u2=min(u2, r[i]);
  74. }
  75. }
  76. rect(buf,xwmin,ywmin,xwmax,ywmax,red);
  77. //wyliczamy wspolrzedne nowych punktow linii
  78. if(u1<u2)
  79. {
  80. int xx1=x1+u1*(x2-x1);
  81. int yy1=y1+u1*(y2-y1);
  82. int xx2=x1+u2*(x2-x1);
  83. int yy2=y1+u2*(y2-y1);
  84.  
  85. if(bscreen) //jezeli mamy rysowac na ekranie
  86. {
  87. rect(buf,xmin,ymin,xmax,ymax, colour);
  88. line(buf,xx1,yy1,xx2,yy2,red);
  89. }
  90. else //jezeli mamy rysowac na obszarze powiekszenia
  91. {
  92. rect(buffer,0,0,200,200, red);
  93. line(buffer,2*(x1-xmin),2*(y1-ymin),2*(x2-xmin),2*(y2-ymin),red);
  94. blit(buffer,buf,0,0,600,0,200,201);
  95. }
  96. }
  97. }
  98.  
  99. int main(){
  100.  
  101. allegro_init();
  102. install_keyboard();
  103. set_gfx_mode( GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
  104. clear_to_color(screen,makecol(0,0,0));
  105. BITMAP *buf;
  106. buf = create_bitmap(800, 600);
  107. BITMAP *buffer;
  108. buffer = create_bitmap(200, 201);
  109. clear_to_color(buffer, makecol(0,0,0));
  110.  
  111. //ustawienie rozmiarow kwadratu
  112. xmax = 200;
  113. ymax = 200;
  114. xmin = 100;
  115. ymin = 100;
  116. while(!key[KEY_ESC])
  117. {
  118. clear_to_color(buf,makecol(0,0,0));
  119. clear_to_color(buffer,makecol(0,0,0));
  120. //rysuje linie na ekranie(buf) -> rysunek oryginalny
  121. for (int i=200; i<600;i+=100)
  122. {
  123. line(buf,0,0,i,i+100,green);
  124. }
  125.  
  126. if(key[KEY_W])
  127. {
  128. ymax--;
  129. ymin--;
  130. }
  131. if(key[KEY_S])
  132. {
  133. ymax++;
  134. ymin++;
  135. }
  136. if(key[KEY_A])
  137. {
  138. xmax--;
  139. xmin--;
  140. }
  141. if(key[KEY_D])
  142. {
  143. xmax++;
  144. xmin++;
  145. }
  146. //dla wyrysowanych linii wykonuje rysunki obcietego kwadratu na ekranie bscreen=true i buforze bscreen=false, buffer to powiekszony obciety kwadrat
  147. for (int i=200; i<600;i+=100)
  148. {
  149. LiangBarsky(0,0,i,i+100,red,true,buf,buffer);
  150. LiangBarsky(0,0,i,i+100,red,false,buf,buffer);
  151. }
  152.  
  153. blit(buf,screen,0,0,0,0,800,600);
  154. }
  155. return 0;
  156.  
  157. }
  158. END_OF_MAIN();
Add Comment
Please, Sign In to add comment