Pixel editing

Started by cormullion, May 05, 2008, 10:29:27 AM

Previous topic - Next topic

cormullion

Is there a way you can create bit-mapped pictures using Guiserver?



Eg say I wanted to draw images by setting pixels to different colours, could I do that?

cormullion

#1
I think there's over 50 000 filled rectangles in here!



http://unbalanced-parentheses.nfshost.com/images/mandel1.png">



Code is pretty basic - apart from michael's elegant complexity.



#!/usr/bin/env newlisp

(load (append (env "NEWLISPDIR") "/guiserver.lsp"))

(define (Class:Class) (cons (context) (args)))
(new Class 'Complex)
(define (Complex:rad c)
    (sqrt (add (pow (c 1) ) (pow (c 2)))))

(define (Complex:add a b)
    (Complex (add (a 1) (b 1)) (add (a 2) (b 2))))

(define (Complex:mul a b)
    (let (a.re (a 1) a.im (a 2) b.re (b 1) b.im (b 2))
        (Complex
            (sub (mul a.re b.re) (mul a.im b.im))
            (add (mul a.re b.im) (mul a.im b.re)))))

(gs:init)
(set 'canvas-width 600 'canvas-height 400)
(gs:frame 'Mandelbrot 60 60 canvas-width canvas-height "Mandelbrot")
(gs:canvas 'MyCanvas 'Mandelbrot)
(gs:add-to 'Mandelbrot 'MyCanvas)
(gs:set-visible 'Mandelbrot true)

(seed (date-value))

(set 'colour-map  (sort (transpose (list (random 0 1 255) (random 0 1 255) (random 0 1 255)))))

(define (do-cell x y c)
    (set 'colour (if (>= c limit) '(0.0 0.0 0.0) (colour-map c)))
    (set 'x (int (mul x scale)))
    (set 'y (int (mul y -1 scale))) ; y goes upwards
    (gs:fill-rect (string c) x y width width colour))

(define (escape x y)
    (set 'z (Complex x y) 'c 0 'a z)
    (while (and
              (< (abs (:rad (set 'z (:add (:mul z z) a)))) 2)
              (<inc> 80000 cells
    (set 'scale (div canvas-width (sub max-x min-x)))
    (set 'width (ceil (mul step scale)))
    (for (y min-y max-y step)
        (for (x min-x max-x step)            
            (do-cell x y (escape x y c)))
        ; end of row
        (gs:update)))

(set 'start (time-of-day))
(set 'limit 100)
(plot)
(gs:set-text 'Mandelbrot (format "from %d/%d to %d/%d step: %0.4f scale: %0.4f cell-width: %d time: %0.4f " min-x min-y  max-x max-x step scale width (div (- (time-of-day) start) 1000)))
(gs:listen)



PS: The forum doesn't escape angle brackets in the CODE sections but when I replace them in with ampersand-codes they don't convert to angle brackets when displayed... ? Confused...

hsmyers

#2
The work around for ampersand escapes that I've found useful is to use the alternate &#; form.<like this>

--hsm
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

m i c h a e l

#3
Quote from: "cormullion"Code is pretty basic - apart from michael's elegant complexity.


Ideally, the code for Complex would be in an existing and fully tested file named complex.lsp, which would be loaded with (load "complex.lsp") and used the same way we use 5 or a string (just as you did). That would reduce it down to two lines ;-)



m i c h a e l

hsmyers

#4
Ah but to do it well, the thing to do would be to add libmpfr and libmpc to the gmp module. This would give us fast unlimited(memory limit) complex math.



--hsm
\"Censeo Toto nos in Kansa esse decisse.\"—D. Gale \"[size=117]ℑ♥λ[/size]\"—Toto

cormullion

#5
Quote from: "hsmyers"The work around for ampersand escapes that I've found useful is to use the alternate &#; form.<like>


It works, thanks!



I'm not too worried about the speed of the complex numbers (or the size of the code :-). I think that all those rectangles are hard work...and I can't get better resolution because I run out of Java heap space...