fork download
  1. public class Main {
  2. public static void main(String[] args) {
  3. VirtualScreen screen = new VirtualScreen(40, 10);
  4.  
  5. screen.box('+', 3, 0, 20, 9);
  6.  
  7. screen.line('#', 5, 2, 3, 9);
  8. screen.line('#', 7, 2, 10, 9);
  9.  
  10. screen.line('#', 9, 1, 9, 3);
  11. screen.line('#', 11, 1, 11, 3);
  12.  
  13. screen.line('#', 14, 2, 19, 2);
  14. screen.line('#', 18, 2, 19, 9);
  15. screen.line('#', 19, 8, 17, 8);
  16.  
  17. screen.line('#', 16, 1, 13, 9);
  18.  
  19. screen.circle('*', 28, 5, 4);
  20. screen.circle('*', 28, 5, 2);
  21.  
  22. screen.fillBox('1', 20, 2, 10, 8);
  23.  
  24. screen.print();
  25. }
  26. }
  27.  
  28. class VirtualScreen {
  29. protected int width;
  30. protected int height;
  31. protected String[] lines;
  32.  
  33. VirtualScreen() {
  34. this(80, 25);
  35. }
  36. VirtualScreen(int width, int height) {
  37. this(' ', width, height);
  38. }
  39. VirtualScreen(char ch, int width, int height) {
  40. reset(ch, width, height);
  41. }
  42.  
  43. public void reset(char ch, int width, int height) {
  44. this.width = width;
  45. this.height = height;
  46. this.lines = new String[height];
  47. for (int i = 0; i < height; ++i) {
  48. lines[i] = "";
  49. for (int j = 0; j < width; ++j) {
  50. lines[i] = lines[i] + ch;
  51. }
  52. }
  53. }
  54.  
  55. public void print() {
  56. for (int y = 0; y < height; ++y) {
  57. System.out.println(lines[y]);
  58. }
  59. }
  60.  
  61. public void setPixel(char ch, int x, int y) {
  62. if (x < 0 || width <= x)
  63. return;
  64. if (y < 0 || height <= y)
  65. return;
  66. String str = lines[y];
  67. lines[y] = str.substring(0, x) + ch + str.substring(x + 1);
  68. }
  69.  
  70. public void fillBox(char ch, int left, int top, int right, int bottom) {
  71. for (int y = top; y <= bottom; ++y) {
  72. for (int x = left; x <= right; ++x) {
  73. setPixel(ch, x, y);
  74. }
  75. }
  76. }
  77.  
  78. public void line(char ch, int x0, int y0, int x1, int y1) {
  79. int dx = x1 - x0, dy = y1 - y0;
  80. if (Math.abs(dx) <= Math.abs(dy)) {
  81. if (dy < 0) {
  82. for (int y = y1; y < y0; ++y) {
  83. int x = x1 + (y - y1) * dx / dy;
  84. setPixel(ch, x, y);
  85. }
  86. } else if (dy > 0) {
  87. for (int y = y0; y < y1; ++y) {
  88. int x = x0 + (y - y0) * dx / dy;
  89. setPixel(ch, x, y);
  90. }
  91. }
  92. } else {
  93. if (dx < 0) {
  94. for (int x = x1; x < x0; ++x) {
  95. int y = y1 + (x - x1) * dy / dx;
  96. setPixel(ch, x, y);
  97. }
  98. } else if (dx > 0) {
  99. for (int x = x0; x < x1; ++x) {
  100. int y = y0 + (x - x0) * dy / dx;
  101. setPixel(ch, x, y);
  102. }
  103. }
  104. }
  105. }
  106.  
  107. public void box(char ch, int x0, int y0, int x1, int y1) {
  108. line(ch, x0, y0, x1, y0);
  109. line(ch, x1, y0, x1, y1);
  110. line(ch, x1, y1, x0, y1);
  111. line(ch, x0, y1, x0, y0);
  112. }
  113.  
  114. public void circle(char ch, int x, int y, double r) {
  115. if (r == 0) {
  116. return;
  117. }
  118. for(double t = 0; t < 2 * Math.PI; t += 1 / (2 * Math.PI * r)) {
  119. double px = x + r * Math.cos(t);
  120. double py = y + r * Math.sin(t);
  121. setPixel(ch, round(px), round(py));
  122. }
  123. }
  124.  
  125. protected int round(double d) {
  126. return (int)Math.round(d);
  127. }
  128. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
   ++++++++++++++++++                   
   +     # #    #   +     *****         
   + # # # #  ##### +    **   **        
   + # #        # # +   ** *** **       
   + # #       #  # +   * ** ** *       
   + #  #      #  # +   * *   * *       
   +#   #      #  # +   * ** ** *       
   +#    #    #   # +   ** *** **       
   +#    #    #  ## +    **   **        
   +++++++++++++++++      *****