# Algorithm of an arc based on the midpoint algorithm

**URL:** <https://discourse.processing.org/t/algorithm-of-an-arc-based-on-the-midpoint-algorithm/29502>\
**Category:** Coding Questions\
**Created:** [April 19, 2021, 7:46pm UTC](https://discourse.processing.org/t/algorithm-of-an-arc-based-on-the-midpoint-algorithm/29502 "2021-04-19T19:46:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![S1993](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@S1993](https://discourse.processing.org/u/S1993)\
**Post date:** [April 19, 2021, 7:46pm UTC](https://discourse.processing.org/t/algorithm-of-an-arc-based-on-the-midpoint-algorithm/29502/1 "2021-04-19T19:46:04Z")

</div>

I have to create an algorithm to produce a circular segment, so I have to give the radius, the center, and the initial and final angle.  
I have been working on the midpoint circle algorithm but I don know how to change it to produce a circular segment.

int xc;  
int yc;

int r;

private color colorC;  
Circulo2(int x, int y, int ra) {  
xc=x;  
yc=y;  
r=ra;  
colorC=color(0,0,0);  
}

void dibujar(int xc,int yc,int x,int y){  
int X= xc+x;  
int Y= yc+y;  
pixels[X+Y_width]=colorC;  
X= xc-x;  
Y= yc+y;  
pixels[X+Y_width]=colorC;  
X= xc+x;  
Y= yc-y;  
pixels[X+Y_width]=colorC;  
X= xc-x;  
Y= yc-y;  
pixels[X+Y_width]=colorC;  
X= xc+y;  
Y= yc+x;  
pixels[X+Y_width]=colorC;  
X= xc-y;  
Y= yc+x;  
pixels[X+Y_width]=colorC;  
X= xc+y;  
Y= yc-x;  
pixels[X+Y_width]=colorC;  
X= xc-y;  
Y= yc-x;  
pixels[X+Y_width]=colorC;  
}

void circ(int xc, int yc, int r){  
loadPixels();  
int x,y,z;  
x=0;  
y=r;  
z=1-r;

dibujar(xc,yc,x,y);

while(x\<y){  
x+=1;  
if(z\<0){  
z=z+2_x+3;  
}else{  
y-=1;  
z=z+2_(x-y)+5;  
}  
dibujar(xc,yc,x,y);  
}

updatePixels();  
}  
I would appreciate it if you can help me

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 19, 2021, 7:53pm UTC](https://discourse.processing.org/t/algorithm-of-an-arc-based-on-the-midpoint-algorithm/29502/2 "2021-04-19T19:53:22Z")

</div>

Hello,

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 20, 2021, 10:24am UTC](https://discourse.processing.org/t/algorithm-of-an-arc-based-on-the-midpoint-algorithm/29502/4 "2021-04-20T10:24:54Z")

</div>

> [@S1993](#):
>
> I have to create an algorithm to produce a circular segment, so I have to give the radius, the center, and the initial and final angle.

Is this a homework question?

I would start here:

> **[Midpoint circle algorithm](https://en.wikipedia.org/wiki/Midpoint_circle_algorithm)**
>
> In computer graphics, the midpoint circle algorithm is an algorithm used to determine the points needed for rasterizing a circle. It's a generalization of Bresenham's line algorithm. The algorithm can be further generalized to conic sections.
> This algorithm draws all eight octants simultaneously, starting from each cardinal direction (0°, 90°, 180°, 270°) and extends both ways to reach the nearest multiple of 45° (45°, 135°, 225°, 315°). It can determine where to stop because when y = x, it has...

That should be straightforward to implement.

Consider doing this with just point() on the canvas and then adapt it to you pixel array

References:  
_[https://processing.org/tutorials/pixels/](https://processing.org/tutorials/pixels/)_  
_[point() / Reference / Processing.org](https://processing.org/reference/point_.html)_

`:)`
