How to create a grid of ellipses with different attributes?

90 views
Skip to first unread message

Ricardo Sanchez

unread,
Jan 29, 2014, 3:42:03 PM1/29/14
to clj-pro...@googlegroups.com
I want to create a grid of ellipses with different attributes but I'm struggling to to solve the problem using functional programming clojure-style, I can do it with my eyes close with other (imperative programming) languages, here is the code I sue to draw the ellispes:

(defn draw []
  (background 245)
  (no-stroke)
  (fill 0)
  (doseq [i (range cols)
          j (range rows)]
    (let [center-x (/ (- (screen-size 0) (* (- cols 1) spacing)) 2)
          center-y (/ (- (screen-size 1) (* (- rows 1) spacing)) 2)
          x (+ (* i spacing) center-x)
          y (+ (* j spacing) center-y)]
      (ellipse x y radius radius))))

So I'm thinking I can create a map that stores the ellipses attributes like this:

(defn dot []
  {:position {:x 0 :y 0}
   :radius (random 20)
   :fill-color (rand-nth colors)})

And I can create a container like an atom to store all the ellipses (or dots in this case) in it.

Not sure if my logic is correct at this point, and here is where I'm not sure how to proceed, basically all is left to do for me is to populate the atom container, and update the dots attributes (position, radius, fill color) and draw them, but here is where my functional programming logic fails...

Any help will be much appreciated!

Nikita Beloglazov

unread,
Jan 29, 2014, 5:33:18 PM1/29/14
to clj-pro...@googlegroups.com
Hi Ricardo

I'm not sure I understand what you want to do. Could you provide example in any imperative language?

Nikita


--
You received this message because you are subscribed to the Google Groups "clj-processing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-processin...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ricardo Sanchez

unread,
Jan 30, 2014, 3:00:29 PM1/30/14
to clj-pro...@googlegroups.com
Hi Nikkita,

First thanks for taking the time on helping me!

Here is my code in Processing:
int cols = 18;
int rows = 12;
int spacing = 30;

color[] colors = {color(255, 31, 58), color(255, 104, 254), color(254, 244, 6), color(137, 196, 255)};
ArrayList<Dot> dots;

void setup() {
  size(640, 480);
  smooth();
  
  dots = new ArrayList();
  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      float centerX = (width - ((cols - 1) * spacing)) / 2;
      float centerY = (height - ((rows - 1) * spacing)) / 2;
      float x = centerX + (i * spacing);
      float y = centerY + (j * spacing);
      PVector position = new PVector(x, y);
      
      Dot dot = new Dot(position, 10, colors[int(random(4))]);
      dots.add(dot);
    }
  }
}

void draw() {
  background(240, 255, 248);
  
  for (int i = 0; i < dots.size(); i++) {
    Dot dot = dots.get(i);
    dot.render();
  }
}

class Dot {
  PVector position;
  int radius;
  color col;

  Dot(PVector _position, int _radius, color _col) {
    position = _position;
    radius = _radius;
    col = _col;
  }

  void render() {
    noStroke();
    fill(col);
    ellipse(position.x, position.y, radius, radius);
  }
}

I'm struggling to translate that code to Quil or better say functional programming

Cheers
rS

Nikita Beloglazov

unread,
Jan 31, 2014, 5:21:20 AM1/31/14
to clj-pro...@googlegroups.com
For storing and accessing dots you can use set-state! and state functions: in setup your use (set-state! :dots dots) and in draw you retrieve dots via (state :dots). Here is an example of almost word-to-word translation of you imperative version:

(defn create-dot [col row]
  (let [center-x (/ (- (width) (* (dec cols) spacing)) 2)
        center-y (/ (- (height) (* (dec rows) spacing)) 2)]
    {:position {:x (+ (* col spacing) center-x)
                :y (+ (* row spacing) center-y)}
     :radius (rand-int 20)
     :fill-color (rand-nth colors)}))

(defn setup
  (let [dots (for [i (range cols)
                   j (range rows)]
               (create-dot i j))]
    (set-state! :dots dots)))

(defn draw-dot [dot]
  (no-stroke)
  (fill (:color dot))
  (let [pos (:position dot)]
    (ellipse (:x pos) (:y pos) (:radius dot) (:radius dot))))

(defn draw
  (background 245)
  (doseq [dot (state :dots)]
    (draw-dot dot)))

Nikita



--

Ricardo Sanchez

unread,
Jan 31, 2014, 3:51:11 PM1/31/14
to clj-pro...@googlegroups.com
Hi Nikkita,

Thanks now I'm reading line by line and is making much more sense, the problem I have is that when I try to solve a problem my mind dont think functionally if that makes sense

The only change I had to make to make it work was

(apply fill (:fill-color dot))


Reply all
Reply to author
Forward
0 new messages