Loop through values of an array on mouse drag
in
Programming Questions
•
11 months ago
Hey guys,
I'm trying to loop through the colors of an array so that as you hold the mouse and drag the sketch will generate a new circle using the next set of values of the array. I am using three arrays (R,G,B). Here's my code!
- float posX, posY;
- float ellipseDiameter;
- //String timestamp;
- //DECLARE COLOR ARRAY
- int[] r = {69, 231, 248, 137};
- int[] g = {39, 99, 233, 224};
- int[] b = {67, 94, 168, 173};
- //DEFINE SHAPE ARRAY LIST
- ArrayList shapeCollection;
- void setup() {
- size(400, 400);
- frameRate(30);
- background(69, 39, 67);
- smooth();
- //timestamp = year() + nf(month(), 2) + nf(day(), 2) + nf(hour(), 2) + nf(minute(), 2)+".png";
- //DEFINE NEW ARRAY LIST
- shapeCollection = new ArrayList();
- }
- void draw() {
- background (r[0], g[0], b[0]);
- //CALL SHAPE ARRAY LIST
- for(int i = 0; i < shapeCollection.size(); i++){
- Shape myShape = (Shape) shapeCollection.get(i);
- myShape.display();
- }
- }
- void mouseDragged() {
- //ADD NEW SHAPE TO ARRAY LIST
- Shape myShape = new Shape(mouseX, mouseY, pmouseX, pmouseY);
- shapeCollection.add(myShape);
- }
- class Shape{
- //GLOBAL VARIABLES
- float x = 0;
- float y = 0;
- float pX = 0;
- float pY = 0;
- //CONSTRUCTOR
- Shape(float _x, float _y, float _pX, float _pY){
- x = _x;
- y = _y;
- pX = _pX;
- pY = _pY;
- }
- //FUNCTIONS
- void display(){
- stroke(255, 255, 255, 130);
- ellipseDiameter = 100/abs(sqrt(sq(x-pX)+sq(y-pY)));
- for (int i = 0; i < r.length; i++) {
- fill(r[i], g[i], b[i]);
- }
- ellipse(x,y,ellipseDiameter,ellipseDiameter);
- }
- }
Thanks in advance!
I
I
1