Advertisement
Guest User

Untitled

a guest
May 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. package hr.tel.fer.kp.lab3;
  2.  
  3. import java.awt.*;
  4.  
  5. public class GraphicThread implements Runnable {
  6.  
  7. private static int MAX = 64;
  8. private double viewX;
  9. private double viewY;
  10. private double zoom;
  11.  
  12. private Graphics g;
  13. private Dimension size;
  14. private int startY, startX, endY, endX;
  15.  
  16. public GraphicThread(Graphics g, Dimension size, int startY, int startX, int endY, int endX, double zoom, double viewX, double viewY){
  17. this.g = g;
  18. this.startX = startX;
  19. this.startY = startY;
  20. this.endX = endX;
  21. this.endY = endY;
  22. this.zoom = zoom;
  23. this.viewX = viewX;
  24. this.viewY = viewY;
  25. this.size = size;
  26. }
  27.  
  28. @Override
  29. public void run() {
  30.  
  31. for (int y = startY; y < endY; y++) {
  32. for (int x = startX; x < endX; x++) {
  33. double r = zoom / Math.min(size.width, size.height);
  34. double dx = 2.5 * (x * r + viewX) - 2.0;
  35. double dy = 1.25 - 2.5 * (y * r + viewY);
  36. int value = mandel(dx, dy);
  37. value = value * 255 / MAX;
  38. synchronized(g){
  39. g.setColor(new Color(value, value, value));
  40. g.drawLine(x, y, x, y);
  41. }
  42. }
  43. }
  44. }
  45.  
  46. private int mandel(double px, double py) {
  47. int value = 0;
  48. double zx = 0.0;
  49. double zy = 0.0;
  50. double zx2 = 0.0;
  51. double zy2 = 0.0;
  52. while (value < MAX && zx2 + zy2 < 4.0) {
  53. zy = 2.0 * zx * zy + py;
  54. zx = zx2 - zy2 + px;
  55. zx2 = zx * zx;
  56. zy2 = zy * zy;
  57. value++;
  58. }
  59. return value == MAX ? 0 : MAX - value;
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement