How to do this programatically (Combinatorics?)

A math question. Sorry if it’s a dumb one, but I couldn’t solve it.

I wrote this with this for four points. All possibles lines, no repeating.

        pvline(p0, p1);
        pvline(p0, p2);
        pvline(p0, p3);

        pvline(p1, p2);
        pvline(p1, p3);
        pvline(p2, p3);

pvline() just unpack values from a p5.Vector and apply them:

function pvline(pv1, pv2) {
    line(pv1.x, pv1.y, pv2.x, pv2.y);

If I had a lot of points, how to program to draw all possibles lines without repeating. Ho to make the loop? Recursion?

Thanks for any help :wink:

ps: Forgot to mention they are stored in an array points' this.points = [p0, p1, p2, p3 ];`

Hello @vkbr,

I just cooked this up:

The line was joining points in an an array of PVectors around a circle.

Some random points:



Simple and Beautiful!!

1 Like

Thanks @vkbr for the topic!


Adding this to my notes:

Another useful tool:


1 Like

Would be nice to see the source code for those images. I get something totally different when I use the vectors in the original post. I had to scale it up x100 for each point and move it over into the center of the window to get the image shown below. It’s flipped compared to what you would plot on graph paper because the computer screen’s origin is left, top.

int num = 6;
PVector[] v = new PVector[num];

void setup() {
  v[0] = new PVector(0,1);
  v[1] = new PVector(0,2);
  v[2] = new PVector(0,3);
  v[3] = new PVector(1,2);
  v[4] = new PVector(1,3);
  v[5] = new PVector(2,3);
for(int i = 0; i < num; i++){
  for(int j = i+1; j < num; j++){
    line(100 + v[i].x*100, v[i].y*100, 100 + v[j].x*100, v[j].y*100);