Test Pattern with Processing
We have a couple of students who needed a test pattern printed out. The pattern was basically a circle cut into a given number of pieces of pie. They initially requested 100 “pieces”, but weren’t exactly sure if that was going to be what they wanted. There was no chance I’d be able to draw this by hand, so I wanted to write something that would generate the pattern and be easy to edit so they could generate patterns with a different number of pieces. I thought that Processing would be a good way to do this and it turned out to be very easy, once I remembered two things. One, all angles in Processing should be in radians, so 360 degrees is 2 * PI radians. And two, that [0,0] is the upper left corner of the drawing. Once I had those settled, I wrote the following:
/* draw_segmented_circle_with_arc Printout of a circle broken up into 100 "pieces of pie" PROCESSING THINGS TO REMEMBER: all angles are in radians. PI radians = 180 degrees [0,0] is the upper left corner */ import processing.pdf.*; // These are globals since using in both setup and draw int xsize = 1000; int ysize = 1000; void setup() { // size defines our drawing size // Need to add the 10 for when print out size(xsize,ysize); // noLoop says to run the draw function below only once noLoop(); // white background background(255); // smooth our lines smooth(); // along with drawing the output on the screen, write it to this file beginRecord(PDF,"output.pdf"); } void draw() { // our stroke is black stroke(0); // setup some variables int i; float x, y, x2, y2; // positions on the circumference of circle int xcenter = 500; // x coordinate of center of circle int ycenter = 500; // y coordinate of center of circle int r = 450; // radius of circle float number_of_segments = 100.0; // number of segments to break circle into float angle = (2 * PI)/number_of_segments; // angle needed for each segment arc(xcenter, ycenter, 2*r, 2*r, 0, 2 * PI); for (i = 1; i <= number_of_segments; i = i + 1) { // calculate the x,y coordinate on the circle x = r * cos(angle * i) + xcenter; y = r * sin(angle * i) + ycenter; // draw a line from the center to that point line(xcenter, ycenter, x, y); } endRecord(); }
Just by changing the value in number_of_segments (line 43), they can generate a new test file.
Here’s what the original pattern looked like.