> For the complete documentation index, see [llms.txt](https://wiki.thescriptersguild.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wiki.thescriptersguild.com/knowledge/scripting/guides-and-info/find-desired-amount-of-furthest-away-objects-from-each-other.md).

# Find Desired Amount of Furthest Away Objects From Each Other

A method for selecting a specific number of objects that maintain maximum spacing from one another using a spatial sampling algorithm.

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-74e137057d6f7b94c14119a6cbc4254a8f2c8ecf%2Fcover-tsg-placeholder.jpg?alt=media" alt="Cover image"><figcaption></figcaption></figure>

This method provides a way to select a specific number of objects that are spread out as evenly as possible across an area. By utilizing a variation of "Farthest Point Sampling," the algorithm ensures that every selected object maintains maximum "breathing room" from the rest of the group.

## Spatial Sampling Logic

A common mistake when attempting to find widely spaced objects is searching for the "maximum distance to any single object." This can result in the algorithm selecting a point that is very far from one existing object but touching another.

To achieve proper distribution, the logic must instead find the point whose **closest neighbor** is as far away as possible from the entire existing set. This is known as Maximizing the Minimum Distance.

### Implementation Workflow

The algorithm builds the set one point at a time through a three-level nested loop structure.

1. **Setup:** Use [Get All Spawn Points](/scripting/nodes/objects/get-all-spawn-points.md) (or a similar object retrieval node) to populate an `All Points` list. Use `Create Empty List` to initialize a `Selected Points` list and define a `Target Count`.
2. **Initial Selection:** Use `Get Random Item` from `All Points` to add the first point to the `Selected Points` list, then use `Remove Item` to take it out of `All Points`.
3. **Main Loop:** While the `Count` of `Selected Points` is less than the `Target Count`, perform the following:

* **Outer Loop:** For each `Candidate` in `All Points`:
* **Inner-Inner Loop:** For each `Selected Point` in `Selected Points`:
* Calculate the `Distance` between the `Candidate` and the `Selected Point`.
* Track the smallest distance found; this is the candidate's "safety buffer."
* **Comparison:** Once the Inner-Inner Loop finishes, check if this candidate's smallest distance is greater than the current `Max Min Distance`. If it is, update `Max Min Distance` and set this candidate as the `Best Candidate`.
* **Finalize Round:** After the Outer Loop completes, add the `Best Candidate` to `Selected Points` and remove it from `All Points`.

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-7078f5335a2bbe1a7ab5f81d4a7ce007dcae453b%2Ffurthest-objects-from-each.webp?alt=media" alt="Node graph for farthest point sampling."><figcaption><p>The node graph illustrates the nested loop structure required for this sampling logic.</p></figcaption></figure>

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-7b7c16b9265c3f96be33989a27528a97f1067bb4%2Fdesired-object-distribution1.webp?alt=media" alt="Spaced out objects."><figcaption><p>This image shows a successful distribution of objects throughout the area.</p></figcaption></figure>

<figure><img src="https://692276616-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FN06Vv6nlwviUQ4hI2Kkd%2Fuploads%2Fgit-blob-51ffc1aadc13ce675d1d6a9c5ff15fa913ddb9ea%2Fdesired-object-distribution2.webp?alt=media" alt="Spaced out objects."><figcaption><p>The selected objects are spread across the available space.</p></figcaption></figure>

## Variable Management

To ensure the algorithm correctly identifies the most isolated candidate, variables must be reset at different stages of the loop hierarchy.

| Variable           | Reset Value | When to Reset               | Purpose                                                                      |
| ------------------ | ----------- | --------------------------- | ---------------------------------------------------------------------------- |
| `Max Min Distance` | `0`         | Start of the **Main Loop**  | Tracks the largest "safety buffer" found during the current selection round. |
| `Min Distance`     | `10000`     | Start of the **Outer Loop** | Tracks the distance to the closest neighbor for the current candidate.       |

{% hint style="info" %}
Using a very high number (like `10000`) for the initial `Min Distance` ensures that the first distance calculated for a new candidate will always be smaller than the starting value.
{% endhint %}

***

## Source Data

* Discord thread: [Find Desired Amount of Furthest Away Objects From Each Other](https://discord.com/channels/220766496635224065/1508892619319545966/1508892619319545966)

#### <mark style="color:green;">Contributors</mark>

Okom\
Guild Archivist\
swagonflyyyy (Mr. Blackwell)
