001         package com.croftsoft.core.gui.plot;
002    
003         import java.awt.*;
004         import java.lang.Math;
005    
006         /*********************************************************************
007         * Plots points on an XY chart.
008         *
009         * @version
010         *   2002-02-28
011         * @since
012         *   1998-12-27
013         * @author
014         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
015         *********************************************************************/
016    
017         public final class  PlotLib
018         //////////////////////////////////////////////////////////////////////
019         //////////////////////////////////////////////////////////////////////
020         {
021    
022         public static void  line (
023           double     m,
024           double     b,
025           Rectangle  r,
026           Graphics   g,
027           double     x0,
028           double     x1,
029           double     y0,
030           double     y1 )
031         //////////////////////////////////////////////////////////////////////
032         {
033           double  x_scale = ( double ) r.width  / ( double ) ( x1 - x0 );
034           double  y_scale = ( double ) r.height / ( double ) ( y1 - y0 );
035    
036           Rectangle  r_old = g.getClipBounds ( );
037           g.clipRect ( r.x, r.y, r.width, r.height );
038           g.drawLine ( r.x,
039             r.y + r.height - ( int ) ( ( m * x0 + b - y0 ) * y_scale ),
040             r.x + r.width ,
041             r.y + r.height - ( int ) ( ( m * x1 + b - y0 ) * y_scale ) );
042           g.clipRect ( r_old.x, r_old.y, r_old.width, r_old.height );
043         }
044    
045         public static Point  graphics_to_plot_transform (
046           Point      gPoint,
047           Rectangle  r,
048           Graphics   g,
049           double     axis_x0,
050           double     axis_x1,
051           double     axis_y0,
052           double     axis_y1 )
053         //////////////////////////////////////////////////////////////////////
054         {
055           double  x_scale
056             = ( double ) r.width  / ( double ) ( axis_x1 - axis_x0 );
057           double  y_scale
058             = ( double ) r.height / ( double ) ( axis_y1 - axis_y0 );
059           return new Point ( 
060             ( int ) ( axis_x0 + ( gPoint.x - r.x            ) / x_scale ),
061             ( int ) ( axis_y0 - ( gPoint.y - r.y - r.height ) / y_scale ) );
062         }
063    
064         public static Point  plot_to_graphics_transform (
065           Point      plot_Point,
066           Rectangle  r,
067           Graphics   g,
068           double     axis_x0,
069           double     axis_x1,
070           double     axis_y0,
071           double     axis_y1 )
072         //////////////////////////////////////////////////////////////////////
073         {
074           double  x_scale
075             = ( double ) r.width  / ( double ) ( axis_x1 - axis_x0 );
076           double  y_scale
077             = ( double ) r.height / ( double ) ( axis_y1 - axis_y0 );
078           return new Point (
079             r.x +            ( int ) ( ( plot_Point.x - axis_x0 ) * x_scale ),
080             r.y + r.height - ( int ) ( ( plot_Point.y - axis_y0 ) * y_scale ) );
081         }
082    
083         public static void  xy (
084           Color      c,
085           double     x,
086           double     y,
087           Rectangle  r,
088           Graphics   g,
089           double     x0,
090           double     x1,
091           double     y0,
092           double     y1,
093           int        oval_size )
094         //////////////////////////////////////////////////////////////////////
095         {
096           double  x_scale = ( double ) r.width  / ( double ) ( x1 - x0 );
097    
098           double  y_scale = ( double ) r.height / ( double ) ( y1 - y0 );
099    
100           Color  c_old = g.getColor ( );
101    
102           g.setColor ( c );
103    
104           g.fillOval ( r.x + ( int ) ( ( x - x0 ) * x_scale - oval_size / 2 ),
105             r.y + r.height - ( int ) ( ( y - y0 ) * y_scale + oval_size / 2 ),
106             oval_size, oval_size );
107    
108           g.setColor ( c_old );
109         }
110    
111         public static void  xy (
112           Color      c,
113           double     x,
114           double     y,
115           Rectangle  r,
116           Graphics   g,
117           double     x0,
118           double     x1,
119           double     y0,
120           double     y1,
121           int        ovalSizeMin,
122           boolean    scaleOvalSize )
123         //////////////////////////////////////////////////////////////////////
124         {
125           if ( scaleOvalSize )
126           {
127             double  x_scale = ( double ) r.width  / ( double ) ( x1 - x0 );
128    
129             double  y_scale = ( double ) r.height / ( double ) ( y1 - y0 );
130    
131             ovalSizeMin = ( int )
132               Math.max ( Math.min ( x_scale, y_scale ), ovalSizeMin );
133           }
134    
135           xy ( c, x, y, r, g, x0, x1, y0, y1, ovalSizeMin );
136         }
137    
138         //////////////////////////////////////////////////////////////////////
139         //////////////////////////////////////////////////////////////////////
140    
141         private  PlotLib ( ) { }
142    
143         //////////////////////////////////////////////////////////////////////
144         //////////////////////////////////////////////////////////////////////
145         }