Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. uses java.math.BigDecimal
  2. uses java.lang.System
  3.  
  4. static function sqrt(x : BigDecimal) : BigDecimal {
  5. var guess : BigDecimal = x / 2
  6. var step : BigDecimal = guess / 2
  7. var minstep : BigDecimal = (1 as BigDecimal).divide((10 as BigDecimal).pow(x.scale()+1))
  8. while(step > minstep) {
  9. if(guess*guess < x) {
  10. guess += step
  11. } else {
  12. guess -= step
  13. }
  14. step = step.divide(2)
  15. }
  16. return guess
  17. }
  18.  
  19. static class Complex {
  20. var a : BigDecimal
  21. var b : BigDecimal
  22.  
  23. construct(that : Complex) {
  24. a = that.a
  25. b = that.b
  26. }
  27.  
  28. construct(x : BigDecimal, y : BigDecimal) {
  29. a = x
  30. b = y
  31. }
  32.  
  33. function add(that : Complex) {
  34. this.a += that.a
  35. this.b += that.b
  36. }
  37.  
  38. function mul(that : Complex) {
  39. this.a = this.a * that.a - this.b * that.b
  40. this.b = this.a * that.b + this.b * that.a
  41. }
  42.  
  43. function r() : BigDecimal {
  44. return sqrt(this.a.pow(2) + this.b.pow(2))
  45. }
  46.  
  47. function rsquared() : BigDecimal { // like Euclidean distance but with out the square root because BigDecimal doesn't have a sqrt
  48. return this.a.pow(2) + this.b.pow(2)
  49. }
  50.  
  51. override function toString() : String {
  52. return "(${this.a}, ${this.b})"
  53. }
  54. }
  55.  
  56. function mandelbrot(c : Complex, max : int) : int {
  57. var n = 0
  58. var z = new Complex(0,0)
  59. //while(n < max and z.r() < 2) { // slooooow
  60. while(n < max and z.rsquared() < 4) {
  61. z.mul(z)
  62. z.add(c)
  63. n += 1
  64.  
  65. //print(z.a.scale())
  66. }
  67. return n
  68. }
  69.  
  70. var center = new Complex(0,0)
  71. var scale : BigDecimal = 10
  72. var grid : int = 35
  73. var escape : int = 9
  74.  
  75. function mapGridPoint(x : int, y : int) : Complex {
  76. return new Complex(
  77. BigDecimal.ONE / scale * (x - grid / 2) + center.a,
  78. BigDecimal.ONE / scale * (y - grid / 2) + center.b
  79. )
  80. }
  81.  
  82. function write(s : String) {
  83. System.out.print(s)
  84. }
  85.  
  86. for(i in 0..grid) {
  87. //write("| ")
  88. for(j in 0..grid) {
  89. var c = mapGridPoint(j, i)
  90. var iterations = mandelbrot(c, escape)
  91. write(" ")
  92. if(iterations == escape) write("*")
  93. else write(".")
  94. System.out.flush()
  95. }
  96. write("\n")
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement