Hm… I would make several shapes with common vertex (1 or more) to simulate that they are connected, that way in the code they would still be able to be colored individually.
In a quick way I was able to put this together, and you can mix it with the more lines stuff I did before to make more complex shapes, and I suggest that more than one common vertex is used so it appears more like one single shape.
int moreShapes, jointX, jointY;
void setup() {
size(400, 400);
frameRate(5);
}
void draw() {
background(255);
fill(255, 0, 0);
stroke(0);
moreShapes = int(random(0, 3));
jointX = int(random(5, 395));
jointY = int(random(5, 395));
beginShape();
vertex(random(5, 395), random(5, 395));
vertex(random(5, 395), random(5, 395));
vertex(jointX, jointY);
endShape(CLOSE);
if (moreShapes == 1) {
shapeA();
}
if (moreShapes == 2) {
shapeB();
}
}
void shapeA() {
fill(0, 255, 0);
beginShape();
vertex(random(5, 395), random(5, 395));
vertex(random(5, 395), random(5, 395));
vertex(jointX, jointY);
endShape(CLOSE);
}
void shapeB() {
fill(0, 0, 255);
beginShape();
vertex(random(5, 395), random(5, 395));
vertex(random(5, 395), random(5, 395));
vertex(jointX, jointY);
endShape(CLOSE);
}