Setting up the coordinates

Computer technology determines the coordinates that processing uses. The distance between two pixels is the unit length and the origin lies at the upper left corner. This is not what we need in geometry and mathematics. Fortunately, processing has all tools needed to correct this.

With “translate(width/2,height/2)” we can put the origin in the center of the screen. With “scale(unitLength,-unitLength)” we can choose how many pixels correspond to one unit of length. Using a negative sign for the second parameter of “scale” we get a y-axis that goes up for increasing y, as usual. The only catch is that the width of the lines gets scaled too. We can correct this with a compensating division in “strokeWeight(…)”.

Finally, it is important to know which points are visible on the screen. Their x-coordinates go from -xRange to +xRange, where xRange=(width/2)/unitLength. The visible range of the y-axis is similarly defined.

And this is it:

float unitLength;
float xRange,yRange;     // visible coordinates from -(xy)Range to +(xy)Range


void setup(){
  size(400,400);
  unitLength=10;
  ellipseMode(CENTER);
}

void draw(){
  setupCoordinates();
  ellipse(0,0,2,2);
  line(0,0,0,10);
  noLoop();
  ellipse(xRange,yRange,2,2);
}

void setupCoordinates(){  
  translate(width/2,height/2);
  scale(unitLength,-unitLength);
  trueStrokeWeight(1.);
  xRange=0.5*width/unitLength;
  yRange=0.5*height/unitLength;
}

void trueStrokeWeight(float weight){
 strokeWeight(weight/unitLength);
}

 

 

This entry was posted in programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s