The Artima Developer Community
Sponsored Link

Java Answers Forum
How to plot 2D graphs using java

2 replies on 1 page. Most recent reply: Apr 4, 2002 11:24 PM by steve strongheart

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
girish

Posts: 1
Nickname: captain
Registered: Mar, 2002

How to plot 2D graphs using java Posted: Mar 31, 2002 5:21 PM
Reply to this message Reply
Advertisement
HI,
Could you let me know as to how to plot a 2D graph in java if you have a set of values for x and y coordinates.
If any one could send a sample program, nothing like it.My email address is girish_kamthe2002@yahoo.com
Thanks in advance.
Girish.


steve strongheart

Posts: 12
Nickname: stronghear
Registered: Apr, 2002

Re: How to plot 2D graphs using java Posted: Apr 4, 2002 11:12 PM
Reply to this message Reply
Dimension SS = getToolkit().getSceeenSize();
/* for fullscreen graph */
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...
// 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= (cx-mp)/horz+mx;
double dy= (cy-mq)/vert+my;
return new Point2D(dx,dy);
}

steve strongheart

Posts: 12
Nickname: stronghear
Registered: Apr, 2002

Re: How to plot 2D graphs using java Posted: Apr 4, 2002 11:24 PM
Reply to this message Reply
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);
}
 

Flat View: This topic has 2 replies on 1 page
Topic: CLASS PATH Previous Topic   Next Topic Topic: Goblet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use