defmodule Mandelbrot do def setdo xsize = 59 ysize = 21 minIm = -1.0 maxIm = 1.0 minRe = -2.0 maxRe = 1.0 stepX = (maxRe - minRe) / xsize stepY = (maxIm - minIm) / ysize Enum.each(0..ysize, fn y -> im = minIm + stepY * y Enum.map(0..xsize, fn x -> re = minRe + stepX * x 62 - loop(0, re, im, re, im, re*re+im*im) end) |> IO.puts end) end
defp loop(n, _, _, _, _, _) when n>=30, do: n defp loop(n, _, _, _, _, v) when v>4.0, do: n-1 defp loop(n, re, im, zr, zi, _) do a = zr * zr b = zi * zi loop(n+1, re, im, a-b+re, 2*zr*zi+im, a+b) end end
for x in1:w for y in1:h i = maxIter const c = Complex( (2*x - w) / (w * zoom) + moveX, (2*y - h) / (h * zoom) + moveY
) z = c whileabs(z) 2 && (i -= 1) > 0 z = z^2 + c end const r,g,b = hsv2rgb(i / maxIter * 360, 1, i / maxIter) img[y,x] = RGB{Float64}(r, g, b) end end
local maxIterations = 250 local minX, maxX, minY, maxY = -2.5, 2.5, -2.5, 2.5 local miX, mxX, miY, mxY function remap( x, t1, t2, s1, s2 ) local f = ( x - t1 ) / ( t2 - t1 ) local g = f * ( s2 - s1 ) + s1 return g; end function drawMandelbrot() local pts, a, as, za, b, bs, zb, cnt, clr = {} for j = 0, hei - 1do for i = 0, wid - 1do a = remap( i, 0, wid, minX, maxX ) b = remap( j, 0, hei, minY, maxY ) cnt = 0; za = a; zb = b while( cnt do as = a * a - b * b; bs = 2 * a * b a = za + as; b = zb + bs ifmath.abs( a ) + math.abs( b ) > 16thenbreakend cnt = cnt + 1 end if cnt == maxIterations then clr = 0 else clr = remap( cnt, 0, maxIterations, 0, 255 ) end pts[1] = { i, j, clr, clr, 0, 255 } love.graphics.points( pts ) end end end function startFractal() love.graphics.setCanvas( canvas ); love.graphics.clear() love.graphics.setColor( 255, 255, 255 ) drawMandelbrot(); love.graphics.setCanvas() end function love.load() wid, hei = love.graphics.getWidth(), love.graphics.getHeight() canvas = love.graphics.newCanvas( wid, hei ) startFractal() end function love.mousepressed( x, y, button, istouch ) if button == 1then startDrag = true; miX = x; miY = y else minX = -2.5; maxX = 2.5; minY = minX; maxY = maxX startFractal() startDrag = false end end function love.mousereleased( x, y, button, istouch ) if startDrag then local l if x > miX then mxX = x else l = x; mxX = miX; miX = l end if y > miY then mxY = y else l = y; mxY = miY; miY = l end miX = remap( miX, 0, wid, minX, maxX ) mxX = remap( mxX, 0, wid, minX, maxX ) miY = remap( miY, 0, hei, minY, maxY ) mxY = remap( mxY, 0, hei, minY, maxY ) minX = miX; maxX = mxX; minY = miY; maxY = mxY startFractal() end end function love.draw() love.graphics.draw( canvas ) end
tree: aPoint length: aLength angle: anAngle | p a |
(aLength > 10) ifTrue: [ p := Pen new. p up. p goto: aPoint. p turn: anAngle. p down. 5 timesRepeat: [ p go: aLength / 5. p turn: 5. ]. a := anAngle - 30. 3 timesRepeat: [ self tree: p location length: aLength * 0.7 angle: a. a := a + 30. ] ].
fn main() { let max_iterations = 256u16; let img_side = 800u32; let cxmin = -2f32; let cxmax = 1f32; let cymin = -1.5f32; let cymax = 1.5f32; let
scalex = (cxmax - cxmin) / img_side as f32; let scaley = (cymax - cymin) / img_side as f32;
// Create a new ImgBuf let mut imgbuf = image::ImageBuffer::new(img_side, img_side);
// Calculate for each pixel for (x, y, pixel) in imgbuf.enumerate_pixels_mut() { let cx = cxmin + x as f32 * scalex; let cy = cymin + y as f32 * scaley;
let c = Complex::new(cx, cy); let mut z = Complex::new(0f32, 0f32);
let mut i = 0; for t in0..max_iterations { if z.norm() > 2.0 { break; } z = z * z + c; i = t; }
*pixel = image::Luma([i as u8]); }
// Save image let fout = &mut File::create("fractal.png").unwrap(); image::ImageLuma8(imgbuf).save(fout, image::PNG).unwrap(); }
// Set up canvas for drawing var canvas: HTMLCanvasElement = document.createElement('canvas') canvas.width = 600 canvas.height = 500 document.body.appendChild(canvas) var ctx: CanvasRenderingContext2D = canvas.getContext('2d'
) ctx.fillStyle = '#000' ctx.lineWidth = 1
// constants const degToRad: number = Math.PI / 180.0 const totalDepth: number = 9
/** Helper function that draws a line on the canvas */ function drawLine(x1: number, y1: number, x2: number, y2: number): void { ctx.moveTo(x1, y1) ctx.lineTo(x2, y2) }
/** Draws a branch at the given point and angle and then calls itself twice */ function drawTree(x1: number, y1: number, angle: number, depth: number): void { if (depth !== 0) { let x2: number = x1 + (Math.cos(angle * degToRad) * depth * 10.0) let y2: number = y1 + (Math.sin(angle * degToRad) * depth *
10.0) drawLine(x1, y1, x2, y2) drawTree(x2, y2, angle - 20, depth - 1) drawTree(x2, y2, angle + 20, depth - 1) } }
// actual drawing of tree ctx.beginPath() drawTree(300, 500, -90, totalDepth) ctx.closePath() ctx.stroke()
WebAssembly WebAssembly 是一匹黑马。在接下来的十年左右的时间,它可能会衍生出一系列编程语言,这些编程语言有望登顶编程语言排行榜。虽然 WebAssembly 只是一个编译目标,但是它有充足理由被应用到 Web 领域之外。哪种基于 WebAssembly 的编程语言能够荣登榜首?谁也说不准。原文:https://hackernoon.com/programming-languages-of-the-future-b61332kd