Courses/CS 461/Winter 2006/Rick Strom/Week One/HW1 - Plasma Fractal

From CSWiki

Jump to: navigation, search
;; stromplasma.nlogo
;; by Rick Strom
;;
;; a 2D plasma fractal (cloud) generator
;; see Information tab for info on this

;patches-own [  ]

to setup
  ca
  set pcolor-of patch-at (0 - screen-edge-x)      screen-edge-y  random(140)
  set pcolor-of patch-at      screen-edge-x       screen-edge-y  random(140)
  set pcolor-of patch-at      screen-edge-x  (0 - screen-edge-y) random(140)
  set pcolor-of patch-at (0 - screen-edge-x) (0 - screen-edge-y) random(140)
 
  build-and-displace-midpoint (0 - screen-edge-x)       screen-edge-y
                          screen-edge-x  (0 - screen-edge-y)     
                          
  repeat smoothing [diffuse pcolor 1]
                     
end

to go
  displace-midpoint (0 - screen-edge-x)  screen-edge-y  
                          screen-edge-x  (0 - screen-edge-y)     
                      
  repeat smoothing [diffuse pcolor 1]
end

to build-and-displace-midpoint[ x0 y0 x1 y1]

  ;; we receive the top left coordinate, and the other three moving clockwise around the rectangle

  ;; stop if difference between two coordinates is 1 or less
  ;; else, calculate 5 new points
  
  if ((x1 - x0 > 1 or x1 - x0 < -1) and (y1 - y0 > 1 or y1 - y0 < -1)) [
  
    set pcolor-of patch-at ((x1 + x0) / 2) (y0)            ((pcolor-of patch-at (x0) (y0) + pcolor-of patch-at (x1) (y0)) / 2)
    set pcolor-of patch-at (x0)            ((y1 + y0) / 2) ((pcolor-of patch-at (x1) (y0) + pcolor-of patch-at (x1) (y1)) / 2)
    set pcolor-of patch-at ((x1 + x0) / 2) (y1)            ((pcolor-of patch-at (x0) (y1) + pcolor-of patch-at (x1) (y1)) / 2)
    set pcolor-of patch-at (x1)            ((y1 + y0) / 2) ((pcolor-of patch-at (x1) (y1) + pcolor-of patch-at (x1) (y0)) / 2)

    set pcolor-of patch-at ((x1 + x0) / 2) ((y1 + y0) / 2) ( ((pcolor-of patch-at (x1) (y1) + pcolor-of patch-at (x1) (y0)) / 2) + random(displace-value) )
  
    build-and-displace-midpoint x0               y0             ((x1 + x0) / 2) ((y1 + y0) / 2)
    build-and-displace-midpoint ((x1 + x0) / 2)  y0             x1              ((y1 + y0) / 2)
    build-and-displace-midpoint ((x1 + x0) / 2) ((y1 + y0) / 2) x1              y1       
    build-and-displace-midpoint x0              ((y1 + y0) / 2) ((x1 + x0) / 2) y1
    
  ]

end

to displace-midpoint[ x0 y0 x1 y1]

  ;; we receive the top left coordinate, and the other three moving clockwise around the rectangle

  ;; stop if difference between two coordinates is 1 or less
  ;; else, calculate 5 new points
  
  if ((x1 - x0 > 1 or x1 - x0 < -1) and (y1 - y0 > 1 or y1 - y0 < -1)) [
  
    ;set pcolor-of patch-at ((x1 + x0) / 2) (y0)            ((pcolor-of patch-at (x0) (y0) + pcolor-of patch-at (x1) (y0)) / 2)
    ;set pcolor-of patch-at (x0)            ((y1 + y0) / 2) ((pcolor-of patch-at (x1) (y0) + pcolor-of patch-at (x1) (y1)) / 2)
    ;set pcolor-of patch-at ((x1 + x0) / 2) (y1)            ((pcolor-of patch-at (x0) (y1) + pcolor-of patch-at (x1) (y1)) / 2)
    ;set pcolor-of patch-at (x1)            ((y1 + y0) / 2) ((pcolor-of patch-at (x1) (y1) + pcolor-of patch-at (x1) (y0)) / 2)  

    set pcolor-of patch-at ((x1 + x0) / 2) ((y1 + y0) / 2) (pcolor-of patch-at ((x1 + x0) / 2) ((y1 + y0) / 2) + random(displace-value) )
    
    if (pcolor-of patch-at ((x1 + x0) / 2) ((y1 + y0) / 2) > 140) [ set pcolor-of patch-at ((x1 + x0) / 2) ((y1 + y0) / 2) 0 ]
  
    displace-midpoint x0               y0             ((x1 + x0) / 2) ((y1 + y0) / 2)
    displace-midpoint ((x1 + x0) / 2)  y0             x1              ((y1 + y0) / 2)
    displace-midpoint ((x1 + x0) / 2) ((y1 + y0) / 2) x1              y1       
    displace-midpoint x0              ((y1 + y0) / 2) ((x1 + x0) / 2) y1
    
  ]     

end
Personal tools