|
Re: How to plot 2D graphs using java
|
Posted: Apr 4, 2002 11:24 PM
|
|
This is a gem of a converter. You need to scale real coordinates into pixel coordinates to draw, and mouse actions back into the graph space. precalculating the variables horz and vert saves some arithmetic by a subtraction and a multiplication/div of doubles.
/**@author Strongheart */
Dimension SS = getToolkit().getSceeenSize();
/*
for fullscreen graph ,
or cut a Rectangle out of your monitor,
new Rectangle(mp,mq,W,H);
*/
int mp =0,mq=0 ,W= SS.width ,H.height;
/* the min and max values of the graph in real values */
double mx -10,my=-7.5 ,MX =10, MY =7.5;
double
horz = (W -mp)/(MX-mx ),
vert = (H -mq)/(MY-my );
/* convert real chart coords into pixel coords. */
Point toIntPoints(double x, double y)
{
int ix,iy;
ix= (x-mx) * horz + mp;
iy= (y-my) * vert + mq;
// for flipped coordinates use one or both of these instead. ...
// ix= (MX-x) * horz +mp;
// iy= (MY-y) * vert +mq;
return new Point(ix,iy);
}
/* converts mouse coordinates into graph space. */
Point2D toRealPoints( int p, int q)
{
double dx= (p-mp)/horz+mx;
double dy= (q-mq)/vert+my;
return new Point2D(dx,dy);
}
|
|