Random Circles
in
Contributed Library Questions
•
11 months ago
Basically the idea here is to draw some random circles on the canvas with no two circles overlap each other. I know the code is incorrect but is there a better way to do so.
TweenShape.pde
- import ijeoma.motion.*;
- import ijeoma.motion.tween.*;
- Tween t;
- int number = 10;
- //Circle[] circles;
- ArrayList<Circle> circles;
- void setup() {
- size(400, 400);
- smooth();
- Motion.setup(this);
- circles = new ArrayList<Circle>();
- // end value must be a float
- for(int i = 0; i < number; i++)
- {
- // add the location of the circle
- PVector loc = getLoc();
- Circle tempCircle = new Circle(loc, random(20, 40));
- circles.add(tempCircle);
- // tween the circle
- t = new Tween(random(1000)).add(tempCircle, "dia", tempCircle.finalDia).play();
- }
- }
- void draw() {
- background(255);
- for(int i = 0; i < number; i++)
- {
- circles.get(i).draw();
- }
- }
- PVector getLoc()
- {
- PVector loc = new PVector(random(width), random(height));
- if(circles.size() == 0)
- return loc;
- else
- {
- boolean out = false;
- do
- {
- out = checkLoc(loc);
- println("check: " + out);
- loc = new PVector(random(width), random(height));
- }while(!out);
- }
- return loc;
- }
- boolean checkLoc(PVector loc)
- {
- boolean retVal = false;
- for(Circle pcircle : circles)
- {
- float gap = pcircle.finalDia / 2;
- if((loc.x > (pcircle.x + gap) || loc.x < (pcircle.x - gap)) && (loc.y > (pcircle.y + gap) || loc.y < (pcircle.y - gap)))
- retVal = true;
- else
- retVal = false;
- }
- return retVal;
- }
Circle.pde
- class Circle
- {
- float x, y, dia, out, finalDia;
- Circle()
- {
- x = random(0, width);
- y = random(0, height);
- out = random(2, 5);
- }
- Circle(PVector pos)
- {
- x = pos.x;
- y = pos.y;
- out = random(2, 5);
- }
- Circle(PVector pos, float val)
- {
- x = pos.x;
- y = pos.y;
- finalDia = val;
- out = random(2, 5);
- }
- void draw() {
- stroke(150);
- strokeWeight(out);
- fill(0);
- ellipse(x, y, dia, dia);
- }
- }
Hope you could understand the code. Thanks in advance!
1