# Collect circle using square

**URL:** <https://discourse.processing.org/t/collect-circle-using-square/10773>\
**Category:** Coding Questions\
**Created:** [April 30, 2019, 12:39am UTC](https://discourse.processing.org/t/collect-circle-using-square/10773 "2019-04-30T00:39:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hello](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@Hello](https://discourse.processing.org/u/Hello)\
**Post date:** [April 30, 2019, 12:39am UTC](https://discourse.processing.org/t/collect-circle-using-square/10773/1 "2019-04-30T00:39:27Z")

</div>

so far i have this and i need help in making the circle get collected

```auto
      int x;
int y;
float circleX;
float circleY;
int points = 0;

 
void setup(){
  textSize(20);
  size (400, 400);
  x = width/2;
  y = height/2;
  circleX = random(0,400);
  circleY = random(0,400);
}
 
void draw(){
  background(0,255,0);
 fill(255,0,0);
  rect(x, y, 20,20);
        fill(255,255,0); 
 ellipse(circleX, circleY, 6,6);
fill(0);
text("Score :" + points, 0,20);
}
void keyPressed() {
   if ((keyPressed == true) && (key == CODED)) {
    if (keyCode == UP){
     y --;
    } else if (keyCode == DOWN) {
      y ++;
    } else if (keyCode == LEFT){
      x --;
    } else if (keyCode == RIGHT) {
      x ++;
    }  
    
      

    }
//collision with circle
if((x >= circleX && y >= circleY)){
}else if( x <= circleX && y <= circleY){
   ellipse(circleX, circleY, 6,6);
points++;

  }
}

```

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [April 30, 2019, 12:48am UTC](https://discourse.processing.org/t/collect-circle-using-square/10773/2 "2019-04-30T00:48:38Z")

</div>

@jeremydouglass has an old post at this [thread](https://forum.processing.org/two/discussion/20769/detecting-collision-between-rectangle-and-circle) linking to this [page](http://www.jeffreythompson.org/collision-detection/circle-rect.php) which you should probably read

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [April 30, 2019, 4:51am UTC](https://discourse.processing.org/t/collect-circle-using-square/10773/3 "2019-04-30T04:51:57Z")

</div>

ok, this is the **complete** code.

```auto
int x;
int y;
float circleX;
float circleY;
int points = 0;

void setup() {
  textSize(20);
  size (400, 400);
  x = width/2;
  y = height/2;
  circleX = random(0, 400);
  circleY = random(0, 400);
  rectMode(CENTER);
}

void draw() {
  background(0, 255, 0);
  fill(255, 0, 0);
  rect(x, y, 20, 20);
  fill(255, 255, 0); 
  ellipse(circleX, circleY, 6, 6);
  fill(0);
  text("Score :" + points, 0, 20);
}
void keyPressed() {
  if ((keyPressed == true) && (key == CODED)) {
    if (keyCode == UP) {
      y --;
    } else if (keyCode == DOWN) {
      y ++;
    } else if (keyCode == LEFT) {
      x --;
    } else if (keyCode == RIGHT) {
      x ++;
    }
  }
  //collision with circle
  if(is_player_on_coin()){
    points++;
    circleX = random(width);
    circleY = random(height);
  }
}

boolean is_player_on_coin(){
  float d = dist(x, y, circleX, circleY);
  //we assuming the player as a circle
  // player radius = 10
  // circle radius = 3
  if(d < 3 + 10){ 
    return true;
  }
  return false;
}

```

---

<div class="post-metadata">

**Author:** ![Hello](https://avatars.discourse-cdn.com/v4/letter/h/7993a0/32.png) [@Hello](https://discourse.processing.org/u/Hello)\
**Post date:** [April 30, 2019, 5:05am UTC](https://discourse.processing.org/t/collect-circle-using-square/10773/4 "2019-04-30T05:05:08Z")

</div>

I still don’t get it

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 30, 2019, 5:06am UTC](https://discourse.processing.org/t/collect-circle-using-square/10773/5 "2019-04-30T05:06:10Z")

</div>

…what don’t you get?

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [April 30, 2019, 5:15am UTC](https://discourse.processing.org/t/collect-circle-using-square/10773/6 "2019-04-30T05:15:07Z")

</div>

this is how the distance between two circle calculated ![q1solnpic](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fc9a4d52722878425f5c52a0bd66aef580eefb97.png)

solve the distance using pythagorean theorem  
 ![download](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/aa642bd214f0043be7f219fb552936c45c3f30a7.png)

if the distance less than player radius + coin radius, then the player is on coin
