Advertisement
Guest User

Untitled

a guest
Aug 19th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. package line;
  2.  
  3. import java.awt.Point;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Random;
  10.  
  11. import javax.imageio.ImageIO;
  12.  
  13. import snake.Image;
  14.  
  15. public class Lines {
  16.  
  17. private final static int NB_LINES = 5000;
  18. private final static int MIN_LENGTH = 10;
  19. private final static int MAX_LENGTH = 50;
  20.  
  21. public static void main(String[] args) throws IOException {
  22. BufferedImage src = ImageIO.read(Image.class.getClassLoader().getResourceAsStream("joconde.png"));
  23. BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_RGB);
  24.  
  25.  
  26. int [] bgColor = {0, 0, 0};
  27. int avgRed = 0, avgGreen = 0, avgBlue = 0, count = 0;
  28. for (int y = 0; y < src.getHeight(); y++) {
  29. for (int x = 0; x < src.getWidth(); x++) {
  30. int colsrc = src.getRGB(x, y);
  31. avgRed += colsrc & 255;
  32. avgGreen += (colsrc >> 8) & 255;
  33. avgBlue += (colsrc >> 16) & 255;
  34. count++;
  35. }
  36. }
  37.  
  38. bgColor[0] = avgBlue/count; bgColor[1] = avgGreen/count; bgColor[2] = avgRed/count;
  39. for (int y = 0; y < src.getHeight(); y++) {
  40. for (int x = 0; x < src.getWidth(); x++) {
  41. dest.getRaster().setPixel(x, y, bgColor);
  42. }
  43. }
  44. List<List<Point>> lines = new ArrayList<List<Point>>();
  45. Random rand = new Random();
  46. for (int i = 0; i < NB_LINES; i++) {
  47. int length = rand.nextInt(MAX_LENGTH - MIN_LENGTH) + MIN_LENGTH;
  48. double ang = rand.nextDouble() * Math.PI;
  49. int lx = (int)(Math.cos(ang) * length); // can be negative or positive
  50. int ly = (int)(Math.sin(ang) * length); // positive only
  51. int sx = rand.nextInt(dest.getWidth() -1 - Math.abs(lx));
  52. int sy = rand.nextInt(dest.getHeight() - 1- Math.abs(ly));
  53. List<Point> line;
  54. if (lx > 0) {
  55. line = line(sx, sy, sx+lx, sy+ly);
  56. } else {
  57. line = line(sx+Math.abs(lx), sy, sx, sy+ly);
  58. }
  59. lines.add(line);
  60. }
  61.  
  62. // render the picture
  63. int [] color = {0, 0, 0};
  64. for (List<Point> line : lines) {
  65.  
  66. avgRed = 0; avgGreen = 0; avgBlue = 0;
  67. count = 0;
  68. for (Point p : line) {
  69. int colsrc = src.getRGB(p.x, p.y);
  70. avgRed += colsrc & 255;
  71. avgGreen += (colsrc >> 8) & 255;
  72. avgBlue += (colsrc >> 16) & 255;
  73. count++;
  74. }
  75. avgRed /= count; avgGreen /= count; avgBlue /= count;
  76. color[0] = avgBlue; color[1] = avgGreen; color[2] = avgRed;
  77. for (Point p : line) {
  78. dest.getRaster().setPixel(p.x, p.y, color);
  79. }
  80.  
  81. }
  82. ImageIO.write(dest, "png", new File("a0.png"));
  83.  
  84. }
  85.  
  86. private static List<Point> line(int x0, int y0, int x1, int y1) {
  87. List<Point> points = new ArrayList<Point>();
  88. int deltax = x1 - x0;
  89. int deltay = y1 - y0;
  90. int tmp;
  91. double error = 0;
  92. double deltaerr = 0;
  93. if (Math.abs(deltax) >= Math.abs(deltay)) {
  94. if (x0 > x1) { // swap the 2 points
  95. tmp = x0; x0 = x1; x1 = tmp;
  96. tmp = y0; y0 = y1; y1 = tmp;
  97. deltax = - deltax; deltay = -deltay;
  98. }
  99. deltaerr = Math.abs (((double)deltay) / deltax);
  100. int y = y0;
  101. for (int x = x0; x <= x1; x++) {
  102. points.add(new Point(x, y));
  103. error += deltaerr;
  104. if (error >= 0.5) {
  105. if (y0 < y1) y++; else y--;
  106. error -= 1.0;
  107. }
  108. }
  109. } else {
  110. if (y0 > y1) { // swap the 2 points
  111. tmp = x0; x0 = x1; x1 = tmp;
  112. tmp = y0; y0 = y1; y1 = tmp;
  113. deltax = - deltax; deltay = -deltay;
  114. }
  115. deltaerr = Math.abs (((double)deltax) / deltay); // Assume deltay != 0 (line is not horizontal),
  116. int x = x0;
  117. for (int y = y0; y <= y1; y++) {
  118. points.add(new Point(x, y));
  119. error += deltaerr;
  120. if (error >= 0.5) {
  121. if (x0 < x1) x++; else x--;
  122. error -= 1.0;
  123. }
  124. }
  125. }
  126. return points;
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement