create.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. #include "bmp.h"
  5. using namespace std;
  6. static const int WIDTH = 4;
  7. static const int HEIGHT = 4;
  8. static void showCheckerBoard(Bmp &bmp)
  9. {
  10. // Color c1 = Color(235, 235, 235);
  11. Color c2 = Color::white();
  12. Color c1 = Color(0, 0, 0);
  13. // Color c2 = Color(255, 255, 255);
  14. Color c3 = Color(-1,-1,-1);
  15. Color c4 = Color(1, 1, 1);
  16. //big checkerBoard
  17. for(int i = 0; i < HEIGHT; i ++)
  18. {
  19. for(int j = 0; j < WIDTH; j ++)
  20. {
  21. // if((i / 30) % 2 == (j / 30) % 2)
  22. if((i/2)%2 == (j/2)%2)
  23. bmp.setPixel(j, i, c1);
  24. //bmp.setPixel(j, i, c3);
  25. else
  26. bmp.setPixel(j, i, c2);
  27. //bmp.setPixel(j, i, c4);
  28. }
  29. }
  30. /* for(int i = 0; i < HEIGHT; i ++)
  31. {
  32. for(int j = 0; j < WIDTH; j ++)
  33. {
  34. if(j % 2 == 0)
  35. bmp.setPixel(j, i, c1);
  36. else
  37. bmp.setPixel(j, i, c2);
  38. }
  39. }
  40. */
  41. }
  42. static void showRectangles(Bmp &bmp)
  43. {
  44. bmp.drawRectangle(0, 0, WIDTH - 1, HEIGHT - 1, Color::cyan());
  45. bmp.fillRectangle(20, 20, 40, 60, Color::yellow());
  46. bmp.drawRectangle(20, 20, 40, 60, Color::black());
  47. int xStep = 4;
  48. int yStep = 4;
  49. int nRectangles = 10;
  50. double dRed = 0.0;
  51. double dGreen = 255.0;
  52. double dBlue = 0.0;
  53. double stepValue = (255.0 / (nRectangles - 1));
  54. double dRedStep = 0.0;
  55. double dGreenStep = -stepValue;
  56. double dBlueStep = stepValue;
  57. for(int i = 0; i < nRectangles; i ++)
  58. {
  59. Color theColor(
  60. (unsigned char)(dRed + 0.5),
  61. (unsigned char)(dGreen + 0.5),
  62. (unsigned char)(dBlue + 0.5)
  63. );
  64. bmp.drawRectangle(
  65. 70 + xStep * i, 50 - yStep * i, 80, 60, theColor
  66. );
  67. dRed += dRedStep;
  68. dGreen += dGreenStep;
  69. dBlue += dBlueStep;
  70. }
  71. }
  72. static double degreesToRadians(double degrees)
  73. {
  74. double radians = (3.14159265 / 180.0) * degrees;
  75. return(radians);
  76. }
  77. static void showLines(Bmp &bmp)
  78. {
  79. int xCenter = WIDTH / 2;
  80. int yCenter = HEIGHT / 2;
  81. double degrees = 0.0;
  82. double radius = 75.0;
  83. while(degrees < 360.0)
  84. {
  85. double radians = degreesToRadians(degrees);
  86. double x = radius * cos(radians);
  87. double y = radius * sin(radians);
  88. bmp.drawLine(
  89. xCenter, yCenter,
  90. xCenter + (int)x, yCenter + (int)y,
  91. Color::red()
  92. );
  93. degrees += 10.0;
  94. }
  95. }
  96. static void showPolygons(Bmp &bmp)
  97. {
  98. vector<int> points;
  99. double step = 2 * (360.0 / 5.0);
  100. int xOffset = bmp.getWidth() / 2 - 50;
  101. int yOffset = 125 + bmp.getHeight() / 2;
  102. double angle = 0.0;
  103. double radius = 50.0;
  104. for(int i = 0; i < 6; i ++)
  105. {
  106. double radians = degreesToRadians(angle);
  107. double x = xOffset + radius * cos(radians);
  108. double y = yOffset + radius * sin(radians);
  109. points.push_back((int)(x + 0.5));
  110. points.push_back((int)(y + 0.5));
  111. angle += step;
  112. if(angle >= 360.0)
  113. angle -= 360.0;
  114. }
  115. bmp.fillPolygon(points, Color::black());
  116. }
  117. int main()
  118. {
  119. Bmp image(WIDTH, HEIGHT);
  120. bool doCheckerBoard = true;
  121. /* bool doRectangles = true;
  122. bool doLines = true;
  123. bool doPolygons = true;
  124. */
  125. if(doCheckerBoard)
  126. {
  127. showCheckerBoard(image);
  128. }
  129. /*
  130. if(doRectangles)
  131. {
  132. showRectangles(image);
  133. }
  134. if(doLines)
  135. {
  136. showLines(image);
  137. }
  138. if(doPolygons)
  139. {
  140. showPolygons(image);
  141. }
  142. */
  143. string errMsg;
  144. string fileName = "4.bmp";
  145. if(!image.write(fileName, errMsg))
  146. {
  147. cout << errMsg << endl;
  148. }
  149. else
  150. {
  151. cout << "Successfully wrote file: [" << fileName << "]" << endl;
  152. }
  153. return(0);
  154. }