NoNameCracksInc

Student

Feb 20th, 2018
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. Kdyby si někdo nevěděl rady, tak tohle řešení je na 3 body.
  2.  
  3. /******************************************************************************
  4. * Laborator 02 - Zaklady pocitacove grafiky - IZG
  5. *
  6. * $Id: $
  7. *
  8. * Popis: Hlavicky funkci pro funkce studentu
  9. *
  10. * Opravy a modifikace:
  11. */
  12.  
  13. #include "student.h"
  14. #include "globals.h"
  15.  
  16. #include <time.h>
  17.  
  18. /******************************************************************************
  19. ******************************************************************************
  20. Funkce vraci pixel z pozice x, y. Je nutne hlidat frame_bufferu, pokud
  21. je dana souradnice mimo hranice, funkce vraci barvu (0, 0, 0).*/
  22. S_RGBA getPixel(int x, int y)
  23. {
  24. if (x > 0 && y > 0 && x < width && y < height)
  25. return frame_buffer[y * width + x];
  26. else
  27. return makeBlackColor();
  28. }
  29.  
  30. /******************************************************************************
  31. ******************************************************************************
  32. Funkce vlozi pixel na pozici x, y. Je nutne hlidat frame_bufferu, pokud
  33. je dana souradnice mimo hranice, funkce neprovadi zadnou zmenu.*/
  34. void putPixel(int x, int y, S_RGBA color)
  35. {
  36. if (x > 0 && y > 0 && x < width && y < height)
  37. frame_buffer[y * width + x] = color;
  38. }
  39.  
  40.  
  41. #include <iostream>
  42. using namespace std;
  43.  
  44. ////////////////////////////////////////////////////////////////////////////////
  45. // Ukol za 2b
  46. ////////////////////////////////////////////////////////////////////////////////
  47. #define FRAC_BITS 8
  48. void drawLine (int x1, int y1, int x2, int y2) {
  49.  
  50. int dx = (x2-x1);
  51. int dy = (y2-y1);
  52. // doplnte!
  53. bool j=false;
  54. if(abs(dy)>abs(dx))
  55. {
  56. j=true;
  57. swap(x1,y1);
  58. swap(x2,y2);
  59. }
  60. if(x1>x2)
  61. {
  62. swap(x1,x2);
  63. swap(y1,y2);
  64. }
  65. if(dx==0)
  66. {
  67. putPixel(x1, y1 >> FRAC_BITS, COLOR_GREEN);
  68. return;
  69. }
  70. dx = (x2-x1);
  71. dy = (y2-y1);
  72. int k = (dy << FRAC_BITS) / dx;
  73. int y = y1 << FRAC_BITS;
  74. for (int x = x1; x <= x2; x++)
  75. {
  76. // doplnte a upravte!
  77. if(j==true)
  78. {
  79. putPixel(y >> FRAC_BITS,x,COLOR_GREEN);
  80. }
  81. else
  82. putPixel( x, y >> FRAC_BITS, COLOR_GREEN);
  83. y += k;
  84.  
  85. }
  86.  
  87. }
  88.  
  89. ////////////////////////////////////////////////////////////////////////////////
  90. // Ukol za 1b
  91. ////////////////////////////////////////////////////////////////////////////////
  92. void put8PixelsOfCircle(int x, int y, int s1, int s2, S_RGBA color) {
  93. // doplnte!
  94. putPixel(s1+x,s2+y,color);
  95. putPixel(s1-x,s2-y,color);
  96. putPixel(s1+y,s2+x,color);
  97. putPixel(s1-y,s2-x,color);
  98. putPixel(s1-y,s2+x,color);
  99. putPixel(s1+y,s2-x,color);
  100. putPixel(s1-x,s2+y,color);
  101. putPixel(s1+x,s2-y,color);
  102. }
  103.  
  104. void drawCircle (int s1, int s2, int R)
  105. {
  106. S_RGBA col;
  107. col.blue = 0;
  108. col.green = 255;
  109. col.red = 0;
  110.  
  111. int x = 0, y = R;
  112. int P = 1-R, X2 = 3, Y2 = 2*R-2;
  113. while (x <= y) {
  114. put8PixelsOfCircle(x, y, s1, s2, COLOR_RED);
  115. if (P >= 0) {
  116. P += -Y2;
  117. Y2 -= 2;
  118. y--;
  119. }
  120. P += X2;
  121. X2 += 2;
  122. x++;
  123. }
  124. return;
  125. }
  126.  
  127. /*****************************************************************************/
  128. /*****************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment