# Processing: 'Under the hood' - Circles and Arcs

**URL:** <https://discourse.processing.org/t/processing-under-the-hood-circles-and-arcs/39473>\
**Category:** Development\
**Created:** [October 28, 2022, 3:23pm UTC](https://discourse.processing.org/t/processing-under-the-hood-circles-and-arcs/39473 "2022-10-28T15:23:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nevermnd](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Nevermnd](https://discourse.processing.org/u/Nevermnd)\
**Post date:** [October 28, 2022, 3:23pm UTC](https://discourse.processing.org/t/processing-under-the-hood-circles-and-arcs/39473/1 "2022-10-28T15:23:40Z")

</div>

Hi,

So I have been trying to develop a separate application that relies heavy on the generation of pixel-by-pixel generation of circles and ellipses. However, do note 1) I am doing this app in Python, not Processing and 2) I am actually not so interested displaying a circle on the screen, but rather making a List/Stack of the (x,y) points that comprise that circle.

I’ve already tried out a number of the standard algorithms like Mid-Point and Bresenham’s (and even non-standard ones), and yes, they do produce a fine circle, but the particular way I am using them-- It is just not adding up right.

However, if I just ‘demo’ that same functionality in Processing, there is no problem.

In any case, seeing as Processing _is_ open source, I just want to take a peak at the circle() and arc() functions.

I tried poking around the Processing V4 Github… But I can’t seem to find exactly where these functions reside (i.e. directory / file), or more importantly the calculations used.

Might someone direct me?

Best,  
-A

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [October 28, 2022, 6:49pm UTC](https://discourse.processing.org/t/processing-under-the-hood-circles-and-arcs/39473/2 "2022-10-28T18:49:43Z")

</div>

The Processing code is unlikely to have what you’re looking for because it uses Java or OpenGL to do the actual rasterization of the geometry.

What is it about mid-point or Bresenham’s algorithms that doesn’t work for what you need? They can be tweaked to provide thick-edged, thin-edged, anti-aliased, or arbitrarily-centered circles. What’s the particular way that you want to use them?

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [October 29, 2022, 11:00am UTC](https://discourse.processing.org/t/processing-under-the-hood-circles-and-arcs/39473/3 "2022-10-29T11:00:28Z")

</div>

Hi @Nevermnd,

I agree with @scudly, you won’t find the actual implementation of the drawing algorithms in the Processing source code because it is at a lower level.

> [@Nevermnd](#):
>
> I tried poking around the Processing V4 Github… But I can’t seem to find exactly where these functions reside (i.e. directory / file), or more importantly the calculations used.

After cloning the repository you go up the stack of function calls:

- `circle()` Papplet.java → [https://github.com/processing/processing4/blob/main/core/src/processing/core/PApplet.java#L11665](https://github.com/processing/processing4/blob/main/core/src/processing/core/PApplet.java#L11665)

- `circle()` PGraphics.java → [https://github.com/processing/processing4/blob/main/core/src/processing/core/PGraphics.java#L3064](https://github.com/processing/processing4/blob/main/core/src/processing/core/PGraphics.java#L3064)

- `ellipse()` PGraphics.java → [https://github.com/processing/processing4/blob/main/core/src/processing/core/PGraphics.java#L2912](https://github.com/processing/processing4/blob/main/core/src/processing/core/PGraphics.java#L2912)

Then you have different implementations (OpenGL and Java2D):

- `ellipseImpl()` PGraphicsJava2D.java → [https://github.com/processing/processing4/blob/main/core/src/processing/awt/PGraphicsJava2D.java#L1249](https://github.com/processing/processing4/blob/main/core/src/processing/awt/PGraphicsJava2D.java#L1249)

Which uses java.awt.Graphics2D function to draw an ellipse shape [https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#draw(java.awt.Shape)](https://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#draw(java.awt.Shape))

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [October 29, 2022, 1:50pm UTC](https://discourse.processing.org/t/processing-under-the-hood-circles-and-arcs/39473/4 "2022-10-29T13:50:45Z")

</div>

I wrote a simple function creating the points of a ellipse. (I’m not that good in Python so I wrote it in Processing instead) but you should be able to adapt it:

```auto
java.util.List<Float> x=new ArrayList<Float>();
java.util.List<Float> y=new ArrayList<Float>();
void setup() {
  size(400, 400);
  genEllipse(100, 100, 40, 30);
  background(255);
  for (int i=0; i<x.size(); i++) {
    point(x.get(i), y.get(i));
    println(x.get(i), y.get(i));
  }
}
void draw() {
}
float f(float xi, float r1, float r2) {
  return r2*sqrt(1-(xi/r1)*(xi/r1));
}
float df(float xi, float r1, float r2) {
  if(sqrt(1-(xi/r1)*(xi/r1))==0) return 1000;
  return 2*r2*1/sqrt(1-(xi/r1)*(xi/r1))/r1/r1*xi;
}
void genEllipse(float xp, float yp, float r1, float r2) {
  for (float xi=-r1; xi<=r1; xi+=min(abs(1/df(xi,r1,r2)),1)) {
    x.add(xi+xp);
    x.add(xi+xp);
    float yoffset=r2*sqrt(1-(xi/r1)*(xi/r1));
    y.add(yp+yoffset);
    y.add(yp-yoffset);
  }
}

```

The idea behind this code is using that the plot of the function sqrt(1-x^2) is basically a half circle. Now the two radi are used to stretch this circle.  
Also to make the graph more consistent I used the derivative to estimate how big of a step I should make to not make it look like the ellipse has a hole.

---

<div class="post-metadata">

**Author:** ![Nevermnd](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@Nevermnd](https://discourse.processing.org/u/Nevermnd)\
**Post date:** [October 29, 2022, 2:19pm UTC](https://discourse.processing.org/t/processing-under-the-hood-circles-and-arcs/39473/5 "2022-10-29T14:19:00Z")

</div>

Thanks @scudly, @NumericPrime

That also explains why I couldn’t find anything when looking through the source.

I guess I didn’t realize Processing had such dependencies in that way. Makes sense though.
