We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Normalizing X and Y for the Screen
Page Index Toggle Pages: 1
Normalizing X and Y for the Screen (Read 274 times)
Normalizing X and Y for the Screen
Sep 7th, 2008, 7:48pm
 
Hi,

In the past I've gotten some great help here so thank you.

One thing I've been working on is plotting equations. One problem I've encountered is not knowing what x or y values will be produced by an equation over a range of values. Anyway in the course of working this problem I created a class that will do the normalization for you. Following is the skeleton for setup() just to illustrate how the class is used and then the full code for the class.


Code:

void setup() {
Normalize setvals; // Normalize class defined below
float x=0,y=0;
float xp=0,yp=0;
size(700,700); background(255);
stroke(0); strokeWeight(2);

setvals= new Normalize();

// first collect data
for(float theta =1; theta < 360; theta++) {
x= 10*cos(theta) + 5*cos(4*theta);
y= 10*sin(theta) - 5*sin(4*theta);
setvals.minmax(x,y); // find min and max values
} // end loop

// now determine values to use for normalization
setvals.calcVals(width, height);
setvals.printVals();

// now output plot
for(float theta =1; theta < 360; theta++) {
x= 10*cos(theta) + 5*cos(4*theta);
y= 10*sin(theta) - 5*sin(4*theta);

// normalize x,y so they appear in screen
xp= (setvals.xAdd() + x)*setvals.xMul();
yp= (setvals.yAdd() + y)*setvals.yMul();
point(xp,yp);

}

} // end setup()


class Normalize {
float xmax = MIN_FLOAT; // holds largext x coordinate
float xmin = MAX_FLOAT; // holds smallest x coordinate
float ymax = MIN_FLOAT; // holds largest y coordinate
float ymin = MAX_FLOAT; // holds smallest y coordinate
float xrange=0; // range between xmin and xmax
float yrange=0; // range between ymin and ymax
float xadd=0; // amount to add to X to raise xmin to zero
float yadd=0; // amount to add to Y to raise ymin to zero
float xmul=0; // amount to multiply X by to fill screen
float ymul=0; // amount to muliply Y by to fill screen

public Normalize() {
} // end constructor

void minmax(float xn, float yn){
xmin=(xn<xmin) ? xn : xmin; xmax=(xn>xmax) ? xn : xmax;
ymin=(yn<ymin) ? yn : ymin; ymax=(yn>ymax) ? yn : ymax;
} // end minmax

void calcVals(int width, int height) {
xrange=abs(xmax-xmin);
yrange=abs(ymax-ymin);
xadd= -xmin;
xmul= (.98*width)/xrange; // note: hard coded screen percentage width
yadd= -ymin;
ymul= (.98*height)/yrange; // note: hard coded screen percentage height
} // end calcVals

void printVals() {
println("xmax=" +xmax+ " xmin=" +xmin);
println("ymax=" +ymax+ " ymin=" +ymin);
println("x range=" +xrange+ " yrange=" +yrange);
println("xadd=" +xadd+ " xmul=" +xmul);
println("yadd=" +yadd+ " ymul=" +ymul);
} // end printVals

float xAdd() { return xadd; }
float yAdd() { return yadd; }
float xMul() { return xmul; }
float yMul() { return ymul; }


} // end class


Feel free to take it and use it as you see fit. Just my way of saying thanks to the folks here.


Page Index Toggle Pages: 1