# Scaling VS. Clipping

• 11-14-2011, 05:10 AM
JojoDiaz
Scaling VS. Clipping
Hey everyone at the Forums,

I'm supposed to alter this code so that the values are scaled rather than clipped. If the values are in the range [lo,hi], then the lengths of the bars should be scaled so that we never exceed the allowed MAXWIDTH.
The smallest f(x) value should have 0 stars and the largest should have MAXWIDTH stars.

I need to use the function f(x) = x/20 sin(0.35x)

Code:

private static final int MAXROWS = 40;
private static final int MAXWIDTH = 60;
//------------------------------------…
public static void main ( String [] args ) throws Exception
{
makeGraph(q);
}
//------------------------------------…

private static void makeGraph ( Function1D f )
{
for ( int i = 0 ; i < MAXROWS ; i++ )
{
System.out.printf("%5.1f %5.1f",(double)i,f.valueAt(i));
int width = (int)f.valueAt(i);
if ( width > MAXWIDTH ) width = MAXWIDTH;
if ( width < 0 ) width = 0;
for ( int j = 0 ; j < width ; j++ ) System.out.print("+");
System.out.println();
}
}
class Quadratic implements Function1D
{
private double a0, a1, a2;
//------------------------------------…
public Quadratic ( double a0, double a1, double a2 )
{
this.a0 = a0;
this.a1 = a1;
this.a2 = a2;
}
//------------------------------------…
public double valueAt ( double x ) { return (a0*x) * Math.sin(a1*x) +a2; }
//------------------------------------…
} // end class Quadratic
//////////////////////////////////////…
interface Function1D
{
//------------------------------------…
public double valueAt ( double x );
//------------------------------------…
} // end interface Function1D

Any help on how i can do this would be greatly appreciated.