Question about sorting object arrays
in
Programming Questions
•
1 year ago
Hey guys,
I have been trying to figure out a way to sort object arrays based on one of the objects properties.
At line 120, I was trying to add a sort algorithm for _budArr based on the property fill1. I want to order it from dark (first in array) to lightest so that when I call the draw function the darker objects are on the bottom and lighter ones on the top. Can anyone help me.
Thanks very much in advance,
Francis
- class Bud {
- float xCoord, yCoord, xWidth, yHeight;
- float twist;
- float x, y;
- float rad, twi;
- float alph;
- float xmove, ymove;
- float fill1, fill2, fillincrease;
- Bud() {
- xCoord = int(random(1280));
- yCoord = int(random(720));
- twist = (PI * random(360))/180;
- xWidth = int(random(20) + 20);
- yHeight = int(random(30) + 10);
- xmove = random(1) ;
- ymove = random(1) ;
- rad = (yHeight + 10)*2;
- twi = random(0.01);
- fill1 = random(255) - 50;
- fill2 = fill1 - (random(50) + 20);
- fillincrease = random(0.1);
- //fillincrease = random(0.1) - 0.2;
- }
- void construct() {
- noStroke();
- fill(fill1);
- beginShape();
- vertex(0, 0);
- bezierVertex(xWidth/5, -yHeight, (xWidth*4)/5, -yHeight, xWidth, 0);
- bezierVertex((xWidth*4)/5, yHeight/2, xWidth/5, yHeight/2, 0, 0);
- endShape();
- noStroke();
- fill(fill2);
- beginShape();
- vertex(0, 0);
- bezierVertex(xWidth/10, yHeight/4, (xWidth*9)/10, yHeight/4, xWidth, 0);
- bezierVertex((xWidth*4)/5, yHeight/2, xWidth/5, yHeight/2, 0, 0);
- endShape();
- }
- void drawMe () {
- translate (xCoord, yCoord);
- scale(2);
- pushMatrix();
- rotate(twist);
- for (int i = 0; i <= 3; i++) {
- float xTrans = -xWidth/2, yTrans = yHeight + 5;
- pushMatrix();
- rotate (i * PI/2);
- translate (xTrans, yTrans);
- construct();
- popMatrix();
- }
- popMatrix();
- resetMatrix();
- }
- void updateMe() {
- twist += twi;
- xCoord += xmove;
- yCoord += ymove;
- if (xCoord > (width+rad)) { xCoord = 0 - rad; }
- if (xCoord < (0-rad)) { xCoord = width+rad; }
- if (yCoord > (height+rad)) { yCoord = 0 - rad; }
- if (yCoord < (0-rad)) { yCoord = height+rad; }
- if (fill1 < 15) {
- fill1 += 1;
- }
- if (fill1 > 230) {
- fill1 -= 1;
- }
- if (fill2 > 200) {
- fill2 -= 1;
- }
- for (int i=0; i<_budArr.length; i++) {
- x = xCoord;
- y = yCoord;
- Bud otherBud = _budArr[i];
- if (otherBud != this) {
- float dis = dist(x, y, otherBud.x, otherBud.y);
- float overlap = dis - rad - otherBud.rad;
- if (overlap < 0) {
- float midx, midy;
- if (x < otherBud.x) {
- midx = x + (otherBud.x - x)/2;
- } else {
- midx = otherBud.x + (x - otherBud.x)/2;
- }
- if (y < otherBud.y) {
- midy = y + (otherBud.y - y)/2;
- } else {
- midy = otherBud.y + (y - otherBud.y)/2;
- }
- otherBud.fill1 += otherBud.fillincrease;
- otherBud.fill2 += otherBud.fillincrease;
- }
- } else {
- otherBud.fill1 -= otherBud.fillincrease;
- otherBud.fill2 -= otherBud.fillincrease;
- }
- }
- drawMe();
- }
- }
- int _num = 10;
- Bud[] _budArr = {};
- void setup () {
- size(1280, 720);
- noStroke();
- smooth();
- strokeWeight(1);
- background(0);
- drawBud();
- }
- void draw() {
- background(0);
- for (int i=0; i<_budArr.length; i++) {
- Bud thisBud = _budArr[i];
- thisBud.updateMe();
- }
- }
- void drawBud() {
- for (int i=0; i<_num; i++) {
- Bud thisBud = new Bud();
- thisBud.drawMe();
- _budArr = (Bud[])append(_budArr, thisBud);
- }
- }
- void mouseReleased() {
- drawBud();
- }
1