> 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

<figure><img src="/files/kFsqDSFNa3XnlNitRwNU" 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="/files/gXK08yYeVKdLOzckYEYC" 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="/files/vivRNkGJiDzK6Y2RZeAW" alt="Spaced out objects."><figcaption><p>This image shows a successful distribution of objects throughout the area.</p></figcaption></figure>

<figure><img src="/files/ga6l1eFTVAIveQ0bBzI0" 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)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://wiki.thescriptersguild.com/knowledge/scripting/guides-and-info/find-desired-amount-of-furthest-away-objects-from-each-other.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
