# How to do this programmatically (Combinatorics?)

**URL:** <https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847>\
**Category:** Coding Questions\
**Created:** [September 18, 2022, 10:18pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847 "2022-09-18T22:18:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [September 18, 2022, 10:18pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/1 "2022-09-18T22:18:40Z")

</div>

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.

```auto
        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:

```auto
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 😉

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

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 18, 2022, 11:28pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/2 "2022-09-18T23:28:21Z")

</div>

Hello @vkbr,

I just cooked this up:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/8/c8e4848e684d7a4630b160a5d51b3609c31db31c.png)

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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/7/6770d79576dfc4b5a8c31d117895a5af313d5d74.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/a/6af79cf21e7c2e42a310ca9b290415149f128d4a.png)

Some random points:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/c/3ce9cca908164ad3712fca5eab54ce3228cb524a.png)

`:)`

---

<div class="post-metadata">

**Author:** ![vkbr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vkbr/32/16844_2.png) [@vkbr](https://discourse.processing.org/u/vkbr)\
**Post date:** [September 18, 2022, 11:31pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/3 "2022-09-18T23:31:08Z")

</div>

Simple and Beautiful!!  
thanks!!!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 18, 2022, 11:45pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/4 "2022-09-18T23:45:09Z")

</div>

Thanks @vkbr for the topic!

Cool!

Adding this to my notes:

> **[Combinatorics](https://en.wikipedia.org/wiki/Combinatorics)**
>
> Combinatorics is an area of mathematics primarily concerned with counting, both as a means and an end in obtaining results, and certain properties of finite structures. It is closely related to many other areas of mathematics and has many applications ranging from logic to statistical physics and from evolutionary biology to computer science.
> Combinatorics is well known for the breadth of the problems it tackles. Combinatorial problems arise in many areas of pure mathematics, notably in algebra...

Another useful tool:

> **[Combinations Calculator (nCr)](https://www.calculatorsoup.com/calculators/discretemathematics/combinations.php)**
>
> Find the number of ways of choosing r unordered outcomes from n possibilities as nCr (or nCk). Combinations calculator or binomial coefficient calcator and combinations formula. Free online combinations calculator.

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [September 19, 2022, 1:23am UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/5 "2022-09-19T01:23:17Z")

</div>

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.

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

void setup() {
  size(400,400);
  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++){
    println(i,j);
    line(100 + v[i].x*100, v[i].y*100, 100 + v[j].x*100, v[j].y*100);
  }
}

}

```

 ![img](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/a/8a27acd0afca88bc9a1b396bbb2945fdf3fc8ea5.png)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 15, 2024, 1:40pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/6 "2024-06-15T13:40:47Z")

</div>

> [@svan](#):
>
> Would be nice to see the source code for those images.

> [@glv](#):
>
> The line was joining points in an an array of _PVectors_ around a circle.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/8/8/881938a79a2375432f54608447ec26a306575d12.png)

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/6/e6c080c94d36d0693cf646f621f0293ed38b3d93.png)

`:)`

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [June 15, 2024, 5:01pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/7 "2024-06-15T17:01:11Z")

</div>

If you modify this line: `float angle = i*TAU/num - TAU/num-1;` you can do shapes other than a pentagon.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [June 15, 2024, 8:30pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/8 "2024-06-15T20:30:29Z")

</div>

Effectively the algorithm finds every unique pair of values in a set. I have used this algorithm a lot when performing collision detection.

Since there is no processing to be done when `i == num - 1` I have adjusted the outer loop to remove redundant processing.

```auto
size(300, 300);
translate(width/2, height/2);
strokeWeight(3);

int num = 5;
PVector [] v = new PVector[num];
float r = 100;

for (int p = 0; p < num; p++) {
  float angle = p * TAU/num;
  v[p] = new PVector(r * cos(angle), r * sin(angle));
}

// Outer loop limit changed 
for (int i = 0; i < num - 1; i++) {
  for (int j = i + 1; j < num; j++) {
    line(v[i].x, v[i].y, v[j].x, v[j].y);
  }
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 17, 2024, 12:08pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/9 "2024-06-17T12:08:28Z")

</div>

> [@svan](#):
>
> If you modify this line: `float angle = i*TAU/num - TAU/num-1;` you can do shapes other than a pentagon.

Thanks for catching that!

The _num_ was in my final edit of code but not in screen grab:

`float angle = i*TAU/num - TAU/4;`

The TAU/4 is an offset so 0 degrees is vertical.

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 17, 2024, 12:16pm UTC](https://discourse.processing.org/t/how-to-do-this-programmatically-combinatorics/38847/10 "2024-06-17T12:16:39Z")

</div>

Hello,

My original code with some updates from feedback:

```auto
/*
 Project: Polygons
 Author: GLV
 Date: 2024-06-17
 Version: 1.0.1
*/

size(300, 300);
translate(width/2, height/2);

int num = 7;
PVector [] v = new PVector [num];
int r = 100;

for(int i = 0; i<num; i++)
  {
  float angle = i*TAU/num - TAU/4;  
  float x = round(r*cos(angle));
  float y = round(r*sin(angle)); 
  v[i] = new PVector(x, y);
  }

strokeWeight(3);

for(int i = 0; i < num-1; i++) // Updated
  {
  for(int j = i+1; j < num; j++)
    {
    println(i,j, ':', v[i], v[j]);
    line(v[i].x, v[i].y, v[j].x, v[j].y);
    }
  }

```

Rounding was for a clean console output (less decimal places) for the screen grab and rounding to nearest pixel will not be noticeable.

I had some fun with this back when and once again and posted in the gallery:

> [@Polygon Exploration](https://discourse.processing.org/t/polygon-exploration/44606):
>
> Some fun with polygons! /\* Project: Polygons Author: GLV Date: 2024-06-17 Version: 1.0.0 \*/ int choice, seed; int id, idlast, sb, num, mxvs; int counter; PVector[] v; boolean warning = true; void settings() { size(400, 400, P2D); } void setup() { background(0); strokeWeight(2); noLoop(); } void draw() { background(0); translate(width/2, height/2); int vs = 10; // 1 to max vertices int sp = 5; …

`:)`
