Last active
November 14, 2018 21:39
-
-
Save 8q/b1b50ed476a7d7a72c3b4d392c8c79ff to your computer and use it in GitHub Desktop.
scalaでマンデルブロ
This file contains hidden or 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
| object M{val W=200;val H=80;def m(v:Float,a:Float,b:Float,c:Float,d:Float)=c+(d-c)*((v-a)/(b-a));def r(a:Float,b:Float,x:Float,y:Float,c:Int):Int=if(Math.abs(x*x+y*y)>2) c else if(c>999) 0 else r(a,b,x*x-y*y+a,2*x*y+b,c+1);def c(a:Float,b:Float)=r(a,b,0,0,0);def main(a:Array[String])=for(j<-(0 to H).map(m(_,0,H,-2,2))){for(i<-(0 to W).map(m(_,0,W,-2,2))){print(if(c(i,j)==0) 2 else 9)};println()}} |
This file contains hidden or 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
| object Main { | |
| val N = 100 | |
| val W_SIZE = 200 | |
| val H_SIZE = 80 | |
| val X_MIN = -1.75f | |
| val X_MAX = 1.25f | |
| val Y_MIN = -1.5f | |
| val Y_MAX = 1.5f | |
| def m(v: Float, s1: Float, e1: Float, s2: Float, e2: Float) = s2 + (e2 - s2) * ((v - s1) / (e1 - s1)) | |
| def r(a: Float, b: Float, x: Float, y: Float, c: Int): Int = if (scala.math.abs(x * x + y * y) > 2) c else if (c >= N) 0 else r(a, b, x * x - y * y + a, 2 * x * y + b, c + 1) | |
| def calc(a: Float, b: Float) = r(a, b, 0, 0, 0) | |
| def main(args: Array[String]) = { | |
| (0 until H_SIZE).map(m(_, 0, H_SIZE, Y_MIN, Y_MAX)).foreach(j => { | |
| (0 until W_SIZE).map(m(_, 0, W_SIZE, X_MIN, X_MAX)).foreach(i => { | |
| print(if (calc(i, j) == 0) 2 else 9) | |
| }) | |
| println(); | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment