Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uses java.math.BigDecimal
- uses java.lang.System
- static function sqrt(x : BigDecimal) : BigDecimal {
- var guess : BigDecimal = x / 2
- var step : BigDecimal = guess / 2
- var minstep : BigDecimal = (1 as BigDecimal).divide((10 as BigDecimal).pow(x.scale()+1))
- while(step > minstep) {
- if(guess*guess < x) {
- guess += step
- } else {
- guess -= step
- }
- step = step.divide(2)
- }
- return guess
- }
- static class Complex {
- var a : BigDecimal
- var b : BigDecimal
- construct(that : Complex) {
- a = that.a
- b = that.b
- }
- construct(x : BigDecimal, y : BigDecimal) {
- a = x
- b = y
- }
- function add(that : Complex) {
- this.a += that.a
- this.b += that.b
- }
- function mul(that : Complex) {
- this.a = this.a * that.a - this.b * that.b
- this.b = this.a * that.b + this.b * that.a
- }
- function r() : BigDecimal {
- return sqrt(this.a.pow(2) + this.b.pow(2))
- }
- function rsquared() : BigDecimal { // like Euclidean distance but with out the square root because BigDecimal doesn't have a sqrt
- return this.a.pow(2) + this.b.pow(2)
- }
- override function toString() : String {
- return "(${this.a}, ${this.b})"
- }
- }
- function mandelbrot(c : Complex, max : int) : int {
- var n = 0
- var z = new Complex(0,0)
- //while(n < max and z.r() < 2) { // slooooow
- while(n < max and z.rsquared() < 4) {
- z.mul(z)
- z.add(c)
- n += 1
- //print(z.a.scale())
- }
- return n
- }
- var center = new Complex(0,0)
- var scale : BigDecimal = 10
- var grid : int = 35
- var escape : int = 9
- function mapGridPoint(x : int, y : int) : Complex {
- return new Complex(
- BigDecimal.ONE / scale * (x - grid / 2) + center.a,
- BigDecimal.ONE / scale * (y - grid / 2) + center.b
- )
- }
- function write(s : String) {
- System.out.print(s)
- }
- for(i in 0..grid) {
- //write("| ")
- for(j in 0..grid) {
- var c = mapGridPoint(j, i)
- var iterations = mandelbrot(c, escape)
- write(" ")
- if(iterations == escape) write("*")
- else write(".")
- System.out.flush()
- }
- write("\n")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement