# Putting a point to the nearest grid intersection

**URL:** <https://discourse.processing.org/t/putting-a-point-to-the-nearest-grid-intersection/14999>\
**Category:** Coding Questions\
**Created:** [October 29, 2019, 6:31am UTC](https://discourse.processing.org/t/putting-a-point-to-the-nearest-grid-intersection/14999 "2019-10-29T06:31:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnsmith17](https://avatars.discourse-cdn.com/v4/letter/j/958977/32.png) [@johnsmith17](https://discourse.processing.org/u/johnsmith17)\
**Post date:** [October 29, 2019, 6:31am UTC](https://discourse.processing.org/t/putting-a-point-to-the-nearest-grid-intersection/14999/1 "2019-10-29T06:31:47Z")

</div>

Hi im trying to make a grid with 10 columns and 10 rows and when mouse pressed a circle is placed to the nearest grid intersection. any idea how i can do this using arrays and loops to compare the distance between each circle and grid intersection?

---

<div class="post-metadata">

**Author:** ![philipplehmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/philipplehmann/32/4111_2.png) [@philipplehmann](https://discourse.processing.org/u/philipplehmann)\
**Post date:** [October 29, 2019, 10:31am UTC](https://discourse.processing.org/t/putting-a-point-to-the-nearest-grid-intersection/14999/2 "2019-10-29T10:31:28Z")

</div>

Do you really need to calculate the distance or would it be enough just to round the value to your grid size? Something like this?

```auto
let gridX = round(mouseX / gridSize) * gridSize;
let gridY = round(mouseY / gridSize) * gridSize;

```

Here is an example:  
[https://editor.p5js.org/zuvala/sketches/wU5fBq\_n1](https://editor.p5js.org/zuvala/sketches/wU5fBq_n1)

If this simple solution doesn’t help, look into vectors. There is a function to calculate the distance between two points.  
[https://p5js.org/reference/#/p5.Vector/dist](https://p5js.org/reference/#/p5.Vector/dist)
