# How to find location of random object

**URL:** <https://discourse.processing.org/t/how-to-find-location-of-random-object/18061>\
**Category:** Coding Questions\
**Created:** [February 24, 2020, 2:49am UTC](https://discourse.processing.org/t/how-to-find-location-of-random-object/18061 "2020-02-24T02:49:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NotConsumable](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@NotConsumable](https://discourse.processing.org/u/NotConsumable)\
**Post date:** [February 24, 2020, 2:49am UTC](https://discourse.processing.org/t/how-to-find-location-of-random-object/18061/1 "2020-02-24T02:49:05Z")

</div>

so I am trying to make a hot and cold game and i have a randomly generated ellipse and i was wondering how to find the location of the ellipse in relation to my mouse so whenever i click it shows me how far away i am from the randomly generated invisible ellipse. here is the code i have now, any help would be much appreciated as i am very new to processing.

```auto
final int ballDiam = 30;
float posX;
float posY;
final int coldDetector = 400;
final int warmDetector = 250;
final int hotDetector = 100;
final int hotColor = color(255,0,0);
final int warmColor = color(255,100,0);
final int coldColor = color(0,0,255);

void setup()
{
 size(500,500); 
 posX = random(width);
  posY = random(height);
}

void draw()
{
  background(200);
 //add stroke (stroke(200); when progrm is finished
  fill(200);
  ellipse(posX,posY,ballDiam,ballDiam);
  noFill();
  ellipse(mouseX,mouseY,50,50);
}

```

---

<div class="post-metadata">

**Author:** ![tribblesontoast](https://avatars.discourse-cdn.com/v4/letter/t/e99b99/32.png) [@tribblesontoast](https://discourse.processing.org/u/tribblesontoast)\
**Post date:** [February 24, 2020, 3:13am UTC](https://discourse.processing.org/t/how-to-find-location-of-random-object/18061/2 "2020-02-24T03:13:32Z")

</div>

Hi NotConsumable,  
Have a look at the [dist()](https://processing.org/reference/dist_.html) function and the [mouseReleased()](https://processing.org/reference/mouseReleased_.html) functions.

Also, for future reference, please format your code by selecting it and pressing ctrl + shift + c or by clicking the “\</\>” button in the editor.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 24, 2020, 10:54am UTC](https://discourse.processing.org/t/how-to-find-location-of-random-object/18061/3 "2020-02-24T10:54:23Z")

</div>

You could indicate how far away from the hidden target (the treasure) the mouse was clicked

Basically get the distance with dist

And the use lerpColor to make a color

Apply the color with fill()

And then say ellipse (mouseX,mouseY, 9,9);

See the reference for details

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [February 24, 2020, 11:57am UTC](https://discourse.processing.org/t/how-to-find-location-of-random-object/18061/4 "2020-02-24T11:57:01Z")

</div>

you can also use noCursor(); to hide the mouse cursor

here is my version

```auto

final int ballDiam = 30;
float posX;
float posY;
final int coldDetector = 400;
final int warmDetector = 250;
final int hotDetector = 100;
final int hotColor = color(255, 0, 0);
final int warmColor = color(255, 100, 0);
final int coldColor = color(0, 0, 255);

void setup()
{
  size(500, 500);
  posX = random(ballDiam, width-ballDiam);
  posY = random(ballDiam, height-ballDiam);

  noCursor();
}

void draw()
{

  background(200);
  //add stroke (stroke(200); when progrm is finished
  fill(200);
  stroke(0); 
  ellipse(posX, posY, 
    ballDiam, ballDiam);

  noFill();
  stroke(255, 0, 0); 
  ellipse(mouseX, mouseY, 
    50, 50);
}

```
