Skip to content

Instantly share code, notes, and snippets.

@bcho
Created June 29, 2014 11:09
Show Gist options
  • Select an option

  • Save bcho/b097a84b2c7430d0061f to your computer and use it in GitHub Desktop.

Select an option

Save bcho/b097a84b2c7430d0061f to your computer and use it in GitHub Desktop.
int mouseClickedCount = 0;
float mouseLastClickedTimeMS = 0;
Circle[] circles = new circles[100];
int circleIdx = -1;
boolean dragging = false;
int draggingCircleIdx = -1;
float draggingOffsetX = 0.0;
float draggingOffsetY = 0.0;
void setup() {
frameRate(60);
size(960, 800);
addCircle(1, 10);
addCircle(20, 10);
addCircle(30, 10);
}
void draw() {
background(0);
drawCircles();
}
void mouseClicked() {
float now = millis();
if (now - mouseLastClickedTimeMS > 500) {
mouseClickedCount = 0;
}
mouseClickedCount++;
if (mouseClickedCount == 2) {
mouseClickedCount = 0;
addCircle(mouseX, mouseY);
}
mouseLastClickedTimeMS = now;
}
void mousePressed() {
int i;
float dx, dy;
for (i = 0; i <= circleIdx; i++) {
dx = sq(circles[i].x - mouseX);
dy = sq(circles[i].y - mouseY);
if (dx + dy < sq(circles[i].currentR / 2)) {
draggingCircleIdx = i;
draggingOffsetX = mouseX - circles[i].x;
draggingOffsetY = mouseY - circles[i].y;
dragging = true;
break;
}
}
}
void mouseReleased() {
dragging = false;
}
void addCircle(float x, float y) {
circles[++circleIdx] = new Circle(25, x, y);
}
void drawCircles() {
for (int i = 0; i <= circleIdx; i++) {
if (i == draggingCircleIdx && dragging) {
circles[i].x = mouseX - draggingOffsetX;
circles[i].y = mouseY - draggingOffsetY;
}
circles[i].draw();
}
}
class Circle {
float r, x, y;
float currentR;
int dX, dY;
String name;
Circle(float r_, float x_, float y_, String name_) {
_setup(r_, x_, y_, name_);
}
Circle(float r_, float x_, float y_) {
_setup(r_, x_, y_, "A random name");
}
void _setup(float r_, float x_, float y_, String name_) {
r = r_;
x = x_;
y = y_;
currentR = 0;
dX = random(2) > 1 ? 0.1 : -0.1;
dY = random(2) > 1 ? 0.1 : -0.1;
name = name_;
}
void draw() {
noStroke();
if (sq(x - mouseX) + sq(y - mouseY) < sq(currentR / 2)) {
fill(64, 128, 128, 100);
} else {
fill(255, 255, 255, 100);
}
ellipse(x, y, currentR, currentR);
fill(10, 110, 2, 100);
ellipse(x, y, 2, 2);
if (currentR < r) {
currentR += 0.5;
}
x += dX;
y += dY;
fill(255, 255, 255, 100);
text(name, x - currentR, y - currentR);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment