Mumfordwiz
Mumfordwiz

Reputation: 1545

Processing - creating circles from current pixels

I'm using processing, and I'm trying to create a circle from the pixels i have on my display. I managed to pull the pixels on screen and create a growing circle from them.

However i'm looking for something much more sophisticated, I want to make it seem as if the pixels on the display are moving from their current location and forming a turning circle or something like this.

This is what i have for now:

    int c = 0;
    int radius = 30;
    allPixels = removeBlackP();
    void draw {
    loadPixels();
    for (int alpha = 0; alpha < 360; alpha++)
   {

      float xf =  350 + radius*cos(alpha);
      float yf =  350 + radius*sin(alpha);
      int x = (int) xf;
      int y = (int) yf;
      if (radius > 200) {radius =30;break;}
      if (c> allPixels.length) {c= 0;}
      pixels[y*700 +x] = allPixels[c];
      updatePixels();
  }
  radius++;
  c++;
  }

the function removeBlackP return an array with all the pixels except for the black ones.

This code works for me. There is an issue that the circle only has the numbers as int so it seems like some pixels inside the circle won't fill, i can live with that. I'm looking for something a bit more complex like I explained.

Thanks!

Upvotes: 1

Views: 949

Answers (2)

MBo
MBo

Reputation: 80187

Fill all pixels of scanlines belonging to the circle. Using this approach, you will paint all places inside the circle. For every line calculate start coordinate (end one is symmetric). Pseudocode:

for y = center_y - radius; y <= center_y + radius; y++
    dx = Sqrt(radius * radius - y * y)
    for x = center_x - dx; x <= center_x + dx; x++
          fill a[y, x] 

When you find places for all pixels, you can make correlation between initial pixels places and calculated ones and move them step-by-step.

For example, if initial coordinates relative to center point for k-th pixel are (x0, y0) and final coordinates are (x1,y1), and you want to make M steps, moving pixel by spiral, calculate intermediate coordinates:

calc values once:
r0 = Sqrt(x0*x0 + y0*y0)  //Math.Hypot if available
r1 = Sqrt(x1*x1 + y1*y1) 
fi0 = Math.Atan2(y0, x0)
fi1 = Math.Atan2(y1, x1)
if fi1 < fi0 then
    fi1 = fi1 + 2 * Pi;

for i = 1; i <=M ; i++
    x = (r0 + i / M * (r1 - r0)) * Cos(fi0 + i / M * (fi1 - fi0))
    y = (r0 + i / M * (r1 - r0)) * Sin(fi0 + i / M * (fi1 - fi0))
    shift by center coordinates    

Upvotes: 3

George Profenza
George Profenza

Reputation: 51837

The way you go about drawing circles in Processing looks a little convoluted.

The simplest way is to use the ellipse() function, no pixels involved though:

If you do need to draw an ellipse and use pixels, you can make use of PGraphics which is similar to using a separate buffer/"layer" to draw into using Processing drawing commands but it also has pixels[] you can access.

Let's say you want to draw a low-res pixel circle circle, you can create a small PGraphics, disable smoothing, draw the circle, then render the circle at a higher resolution. The only catch is these drawing commands must be placed within beginDraw()/endDraw() calls:

PGraphics buffer;

void setup(){
  //disable sketch's aliasing
  noSmooth();

  buffer = createGraphics(25,25);
  buffer.beginDraw();
  //disable buffer's aliasing
  buffer.noSmooth();
  buffer.noFill();
  buffer.stroke(255);
  buffer.endDraw();
}
void draw(){
  background(255);

  //draw small circle
  float circleSize = map(sin(frameCount * .01),-1.0,1.0,0.0,20.0);  
  buffer.beginDraw();
  buffer.background(0);
  buffer.ellipse(buffer.width / 2,buffer.height / 2, circleSize,circleSize);
  buffer.endDraw();

  //render small circle at higher resolution (blocky - no aliasing)
  image(buffer,0,0,width,height);
}

If you want to manually draw a circle using pixels[] you are on the right using the polar to cartesian conversion formula (x = cos(angle) * radius, y = sin(angle) * radius).Even though it's focusing on drawing a radial gradient, you can find an example of drawing a circle(a lot actually) using pixels in this answer

Upvotes: 0

Related Questions