Guest User

Untitled

a guest
Dec 13th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. (defun mandelbrot-value (w h d x y)
  2.   "Find the Mandelbrot value from a scaled set"
  3.   (let* ((cx (* 1.5 (/ (- x (/ w 1.45)) w 0.45)))
  4.          (cy (* 1.5 (/ (- y (/ h 2.0)) h 0.5)))
  5.          (zr 0)
  6.          (zi 0))
  7.     (dotimes (i d d)
  8.       (if (> (+ (* zr zr) (* zi zi)) 4)
  9.           (return i)
  10.         (psetq zr (+ (* zr zr) (- (* zi zi)) cx)
  11.                zi (+ (* zr zi 2) cy))))))
  12.  
  13. (defun mandelbrot-image (w h d)
  14.   "Sometimes I get bored and render the Mandelbrot set in Emacs Lisp"
  15.   (interactive "nWidth: \nnHeight: \nnDepth: ")
  16.   (pop-to-buffer (get-buffer-create "*mandelbrot*"))
  17.   (fundamental-mode)
  18.   (erase-buffer)
  19.   (set-buffer-multibyte nil)
  20.   (insert (format "P6\n%d %d\n255\n" w h))
  21.   (dotimes (y h)
  22.     (dotimes (x w)
  23.       (insert-char (floor (* 256 (/ (mandelbrot-value w h d x y) 1.0 d))) 3)))
  24.   (image-mode))
  25.  
  26. (defun mandelbrot-text (w h d)
  27.   "Sometimes it needs to be textual"
  28.   (interactive "nWidth: \nnHeight: \nnDepth: ")
  29.   (pop-to-buffer (get-buffer-create "*mandelbrot*"))
  30.   (fundamental-mode)
  31.   (erase-buffer)
  32.   (set-buffer-multibyte nil)
  33.   (dotimes (y h)
  34.     (dotimes (x w)
  35.       (let* ((v (mandelbrot-value w h d x y))
  36.              (c (cond ((= v d) ? )
  37.                       ((evenp v) ?*)
  38.                       (t ?#))))
  39.         (insert-char c 1)))
  40.     (insert-char ?\n 1)))
  41.  
  42. (provide 'mandelbrot-image)
  43. (provide 'mandelbrot-text)
Add Comment
Please, Sign In to add comment