# Spacegrid subset

**URL:** https://discourse.julialang.org/t/spacegrid-subset/118066
**Category:** Modelling & Simulations
**Tags:** agents
**Created:** [August 11, 2024, 5:41pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066 "2024-08-11T17:41:46Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![JCaro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcaro/32/209617_2.png) [@JCaro](https://discourse.julialang.org/u/JCaro)
#### Post date: [August 11, 2024, 5:41pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066/1 "2024-08-11T17:41:46Z")

</div>

I need to work on a simulation and i want to make a gridspace and sub-divide it, using random points in the grid and their respectivly voronoi polygons. Can someone give some directions on how/how not do it??

a image of my idea

![image](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b367a82bced506643f7600dc28328da9d16243f.png)

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [August 11, 2024, 6:15pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066/2 "2024-08-11T18:15:13Z")

</div>

You could start by working through the tutorials here, perhaps.

> **[GitHub - JuliaGeometry/DelaunayTriangulation.jl: Delaunay triangulations and Voronoi tessellations...](https://github.com/JuliaGeometry/DelaunayTriangulation.jl)**
>
> Delaunay triangulations and Voronoi tessellations in two dimensions.

---

<div class="post-metadata">

### Author: ![TravisB](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@TravisB](https://discourse.julialang.org/u/TravisB)
#### Post date: [August 13, 2024, 3:23pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066/3 "2024-08-13T15:23:56Z")

</div>

I made a short script that does something similar a while back. It’s not at all optimized, but can produce the below image using the Random, Plots, and Colors packages.

 ![polygons](https://global.discourse-cdn.com/julialang/original/3X/4/7/4722324a95980e1a2e4daa30ac5219e7b163c2a9.png)

Its implementation really comes down to creating a list of empty cells, dividing up the grid into very small intervals, computing the distance from each grid point to all other points, finding the closest random point, and storing it to the corresponding cell. The main calculation is the following:

```julia
distances = [sqrt((px - x)^2 + (py - y)^2) for (px, py) in points]
closest_point = argmin(distances)
if closest_point == length(all_cells) + 1
    push!(current_cell, (x, y))
end

```

Where px/py are the random points and x/y are the small intervals of your grid. Feel free to message me if you’d like more details on its implementation though.

If you’re looking for something more optimized, I’d look at the [DelauneyTriangulation.jl](https://github.com/JuliaGeometry/DelaunayTriangulation.jl) package already mentioned or the [VoronoiDelaunay.jl](https://github.com/JuliaGeometry/VoronoiDelaunay.jl) package (which appears to be under maintenance).

---

<div class="post-metadata">

### Author: ![JCaro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcaro/32/209617_2.png) [@JCaro](https://discourse.julialang.org/u/JCaro)
#### Post date: [August 14, 2024, 8:16pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066/4 "2024-08-14T20:16:24Z")

</div>

already working on the segmenytation using discetevoronoi. but using the voronoi-grid as data in the space type-object in the agents.jl is my real deal

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [August 14, 2024, 9:33pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066/5 "2024-08-14T21:33:38Z")

</div>

There is truly no difference in having this data as a model property instead of somehow having it in the space

CU!

George

---

<div class="post-metadata">

### Author: ![TravisB](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@TravisB](https://discourse.julialang.org/u/TravisB)
#### Post date: [August 15, 2024, 3:03pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066/6 "2024-08-15T15:03:04Z")

</div>

If all you care about is creating the diagrams, then it may be quicker to simply run the calculation and color the regions yourself rather than searching for existing packages. My script was only around 50 lines and seems to do everything you wanted in the original prompt: create and subdivide a gridspace, generate random points on that grid, and create a voronoi diagram.

If you tell us more about the goal of the simulation, and why you’d prefer to use agent based modeling like [Agents.jl](https://juliadynamics.github.io/Agents.jl/stable/), then maybe we can give more directed advice.

---

<div class="post-metadata">

### Author: ![JCaro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcaro/32/209617_2.png) [@JCaro](https://discourse.julialang.org/u/JCaro)
#### Post date: [August 15, 2024, 7:21pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066/7 "2024-08-15T19:21:02Z")

</div>

i really want to replicate a ecology paper that predicts certain properties of certain invasive species, and predict the outcomes of using diferents ways to manage it, and they do via ABM. In that simulation, the agents self-replicate (as any living being), and the rate of reproductions depends on where the agent is, in a subdivided grid of polygons. All poligons are rated from 1, as the smaller rate of reproduction ind/seconds to 9 as the bigger rate

---

<div class="post-metadata">

### Author: ![JCaro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcaro/32/209617_2.png) [@JCaro](https://discourse.julialang.org/u/JCaro)
#### Post date: [August 15, 2024, 7:22pm UTC](https://discourse.julialang.org/t/spacegrid-subset/118066/8 "2024-08-15T19:22:26Z")

</div>

already do manage to do the voronoi grid. looks cool.

 ![heatmap](https://global.discourse-cdn.com/julialang/original/3X/b/3/b31a780fefc6e92f33ae73124a55e8f22c91ae3a.png)
