Last active
September 26, 2017 19:29
-
-
Save geofrey/2e8a96628e808dde683948d01d124069 to your computer and use it in GitHub Desktop.
Crappy Mandelbrot Renderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uses java.math.BigDecimal | |
uses java.lang.System | |
uses java.math.MathContext | |
uses java.lang.Math | |
var center = new Complex(0,0) | |
var zoom : BigDecimal = 10 | |
var mathContext = new MathContext(java.lang.Math.log10(zoom.doubleValue()) as int + 2, java.math.RoundingMode.HALF_DOWN) | |
var grid : int = 34 | |
var escape : int = 38 | |
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, mc : MathContext) { | |
this.a = this.a.add(that.a, mc) | |
this.b = this.b.add(that.b, mc) | |
} | |
function mul(that : Complex, mc : MathContext) { | |
//print("${this.a.scale()}/${this.b.scale()}") | |
this.a = this.a.multiply(that.a, mc).subtract(this.b.multiply(that.b, mc), mc) | |
this.b = this.a.multiply(that.b, mc).add(this.b.multiply(that.a, mc), mc) | |
} | |
override function toString() : String { | |
return "(${this.a}, ${this.b})" | |
} | |
} | |
function write(s : String) { | |
System.out.print(s) | |
} | |
function mandelbrot(c : Complex, max : int) : int { | |
var n = 0 | |
var z = new Complex(c) | |
//while(n < max and z.rsquared(mathContext) < 4) { | |
while(n < max and (z.a < 4 and z.b < 4)) { | |
// BigDecimal scale is getting out of control here; isn't the MathContext setting supposed to prevent this? | |
z.mul(z, mathContext) | |
z.add(c, mathContext) | |
n += 1 | |
} | |
return n | |
} | |
function mapGridPoint(x : int, y : int) : Complex { | |
return new Complex( | |
BigDecimal.ONE.divide(zoom, mathContext).multiply(x - grid / 2, mathContext).add(center.a, mathContext), | |
BigDecimal.ONE.divide(zoom, mathContext).multiply(y - grid / 2, mathContext).add(center.b, mathContext) | |
) | |
} | |
for(i in 0..grid) { | |
for(j in 0..grid) { | |
var c = mapGridPoint(j, i) | |
var iterations = mandelbrot(c, escape) | |
write(" ") | |
if(iterations == escape) write("*") | |
else write(".") | |
} | |
write("\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Derpy AF