Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. The formula I have is
  2. //Pixel(x,y)
  3. for(int xi=0; xi<n;xi++){
  4. double rx = Math.random();
  5. double ry = Math.random();
  6. double xs = x + (xi+rx)/n;
  7. double ys = y + (yi+ry)/n;
  8. ...
  9. }
  10.  
  11. public class Main {
  12.  
  13. static String name = "doc/a01.png";
  14. static int width = 480;
  15. static int height = 270;
  16.  
  17. public static void main(String[] args) {
  18. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  19.  
  20. for (int x = 0; x != width; x++) {
  21. for (int y = 0; y != height; y++) {
  22. double rx = Math.random();
  23. image.setRGB(x, y, StratifiedSampling(x,y));
  24. }
  25. }
  26.  
  27. try {
  28. File outputfile = new File(name);
  29. ImageIO.write(image, "png", outputfile);
  30. System.out.println("Wrote image: " + name);
  31. } catch (IOException e) {
  32. System.out.println("Something went wrong: " + e);
  33. }
  34. }
  35. static int StratifiedSampling(int x,int y){
  36. double sum = 0;
  37. final int samples = 100;
  38. for(int xi=0; xi<samples;xi++){
  39. for(int yi=0; yi<samples;yi++){
  40. double rx = Math.random();
  41. double ry = Math.random();
  42. double xs = x + (xi+rx)/samples;
  43. double ys = y + (yi+ry)/samples;
  44. //System.out.println((int)xs+" "+(int)ys);
  45. sum = sum + colorForCircle((int)xs,(int)ys);
  46. }
  47. }
  48. return (int)sum/samples;
  49. }
  50.  
  51. static int colorForCircle(int x1, int y1){
  52. double x2 = x1;
  53. double y2 = y1;
  54. //System.out.println( 0 + (255 - 0) * y1/height);
  55. int centerX = 240;
  56. int centerY = 135;
  57. double radius = 40;
  58. if( Math.pow( (x2 - centerX),2) + Math.pow((y2 - centerY),2) < Math.pow(radius,2)){
  59. return new Color(255, 0, 0).getRGB();
  60. }
  61. else {
  62. double t = y2/height;
  63. int value = (int) (t* 255);
  64. int value2 = (int) (Math.pow (t,1/2.2)* 255);
  65. return new Color(0, 0, value).getRGB();
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement