# Am i using equation of circle right?

**URL:** <https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318>\
**Category:** Coding Questions\
**Created:** [July 2, 2020, 7:47am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318 "2020-07-02T07:47:27Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 2, 2020, 7:47am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/1 "2020-07-02T07:47:27Z")

</div>

I want to move an object around a circle therefore, am using equation of circle to every quadrant…  
I wrote this:  
if(keyDown(“up”) && this.guy.x \<= 600 && this.guy.x \<= 400){

```
            //using equation of a circle

```

```auto
> this.guy.y -= 1;
> 
> this.guy.x = (450^2 - (this.guy.y - 300)^2)^(1/2) + 400;
> 
> // (this.guy.x - 400)^2 + (this.guy.y - 300)^2 = 450^2;   
> 
> this.guy.rotation += 0.323;

```

But it is going haywire, how to write it correctly???

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 2, 2020, 7:48am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/2 "2020-07-02T07:48:09Z")

</div>

> [@ARNAV\_A\_GUPTA](#):
>
> ` // (this.guy.x - 400)^2 + (this.guy.y - 300)^2 = 450^2`

wrote this for reference, it is the equation of circle

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 2, 2020, 8:15am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/3 "2020-07-02T08:15:01Z")

</div>

Hi @ARNAV_A_GUPTA,

The equation of a circle can be written as follow:  
`(x - cx )² + (y - cy)² = r²`  
where `(cx, cy)` is the center of your cricle and `r` is the radius. All the pairs `(x, y)` that fit the equation will be a point of the circle.

You can indeed get the x value by rearranging the above equation **BUT** you will have to deal with solving a second degree equation that can have none, 1 or 2 solutions.

You can get that intuition visually:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/de340b4b1d71c3a71974eeb46447c0b1c0f374e5.png)

Given a Y value, you can be in the case of the blue line (no solution), the red line (1 solution) or the orange line (2solutions)

Now, instead of working with that equation of a circle, we usually work with its parametric form. Given a center `(cx, cy)`, a radius `r` and an angle `alpha`, you can get the `(x, y)` coordinates of the point with the following equations:

```auto
x = cx + r * cos(alpha)
y = cy + r * sin(alpha)

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 2, 2020, 8:28am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/4 "2020-07-02T08:28:03Z")

</div>

Try this code if you want:

```auto
let cx, cy, r;

function setup() {
  createCanvas(400, 400);
  angleMode(DEGREES);
  
  cx = 200;
  cy = 200;
  r = 100
  
  angleSlider = createSlider(0, 360, 0);
  angleSlider.position(50, 350);
  angleSlider.size(300, 20);
  
}

function draw() {
  background(20);
  noFill();
  stroke(200);
  strokeWeight(2);
  
  // Draw a circle at position (cx, cy) and radius r
  ellipse(cx, cy, 2*r, 2*r);
  
  // Place a point on the circle based on the angle
  let x = cx + r * cos(angleSlider.value());
  let y = cy + r * sin(angleSlider.value());
  
  fill(230, 10, 10);
  noStroke();
  ellipse(x, y, 20, 20);
}

```

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 3, 2020, 9:49am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/5 "2020-07-03T09:49:01Z")

</div>

OK;  
Nice, thanks for your help…  
Have to adapt the code according to me coz, I have to change the x or y position and I should get the other coordinate…  
Will it work that way???

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 3, 2020, 9:53am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/6 "2020-07-03T09:53:54Z")

</div>

> [@jb4x](#):
>
> ```auto
> x = cx + r * cos(alpha)
> y = cy + r * sin(alpha)
> 
> ```

ALPHA???  
WHAT WOULD IT BE??

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [July 3, 2020, 10:17am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/7 "2020-07-03T10:17:28Z")

</div>

`alpha` is the angle from an initial state.

Because of how the corrdinates are setup in p5.js the initial position (alpha = 0) corresponds to a point on the right.

Then alpha increases in a clockwise manner.

By default p5.js uses radians instead of degrees so a full revolution around the circle correspond to an angle of `alpha` of 2 PI.

But you can change that with `angleMode(DEGREES)` to use degrees instead of radians. In this case 360° correspond to a full revolution.

Here another picture to help you understand:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cedc6f4201653fef8394d3936328dea10130e4e8.png)

Btw, no need for capital letters, we are not deaf over here 😉

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 3, 2020, 10:20am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/8 "2020-07-03T10:20:56Z")

</div>

> [@jb4x](#):
>
> we are not deaf over here

HMMMM… DID I SAY SOMETHIN’ 🤣🤣

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 3, 2020, 10:21am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/9 "2020-07-03T10:21:15Z")

</div>

```auto
> if(keyDown("up")){
> 
> if(this.guy.x < 400){
> 
> this.angle++;
> 
> } else {
> 
> this.angle--;
> 
> }
> 
> this.guy.x = this.position.x + 234 * cos(this.angle) ;
> 
> this.guy.Y = this.position.y + 234 * sin(this.angle);
> 
> // (this.guy.x - 400)^2 + (this.guy.y - 300)^2 = 450^2;   
> 
> this.guy.rotation += 0.323;
> 
> console.log("yo!");
> 
>                 
> 
> //ellipse(400,300,900,900);
> 
> }

```

this is not working, did I have done somethin’ wrong???

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 3, 2020, 10:23am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/10 "2020-07-03T10:23:21Z")

</div>

> [@ARNAV\_A\_GUPTA](#):
>
> WILL THIS WORK???

TRY IT AND SEE …

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 3, 2020, 10:25am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/11 "2020-07-03T10:25:34Z")

</div>

oh, sorry, didnt mean this… 😂😂… actually, this was not working.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [July 3, 2020, 10:34am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/12 "2020-07-03T10:34:22Z")

</div>

In your code  
`this.position` is the centre of the circle  
`234` is the radius of the circle

so looks good so far but then  
`this.angle++;` increments the angle by 1 _radian_ or ~57.3 _degrees_ which is huge try  
`this.angle += 0.01745;` which is approximately 1 degree

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 3, 2020, 10:36am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/13 "2020-07-03T10:36:29Z")

</div>

y is not incrementing

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 3, 2020, 10:38am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/14 "2020-07-03T10:38:11Z")

</div>

@quark, WHAT A SOLUTION, had written y in capital 😂

---

<div class="post-metadata">

**Author:** ![ARNAV\_A\_GUPTA](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/arnav_a_gupta/32/9660_2.png) [@ARNAV\_A\_GUPTA](https://discourse.processing.org/u/ARNAV_A_GUPTA)\
**Post date:** [July 3, 2020, 10:45am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/16 "2020-07-03T10:45:11Z")

</div>

sorry for the mess in the messages. Actually, am a very much beginner.

---

<div class="post-metadata">

**Author:** ![Randomhouse](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@Randomhouse](https://discourse.processing.org/u/Randomhouse)\
**Post date:** [June 14, 2021, 3:55am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/17 "2021-06-14T03:55:17Z")

</div>

Can you DM me? I’m new and it won’t allow me…

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [June 14, 2021, 4:21am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/18 "2021-06-14T04:21:12Z")

</div>

Why would you need me to dm you? If you have a question, post it on the forum. The point is that everyone can benefit from wathever help you need. Plus I might not be able to answer your question but other people could.

---

<div class="post-metadata">

**Author:** ![Randomhouse](https://avatars.discourse-cdn.com/v4/letter/r/a88e57/32.png) [@Randomhouse](https://discourse.processing.org/u/Randomhouse)\
**Post date:** [June 14, 2021, 4:54am UTC](https://discourse.processing.org/t/am-i-using-equation-of-circle-right/22318/19 "2021-06-14T04:54:03Z")

</div>

Thanks for responding. It was in reference to a paid project
