Axis.hpp

Go to the documentation of this file.
00001 #pragma ident "$Id: Axis.hpp 2091 2009-08-21 14:24:47Z afarris $"
00002 
00004 
00005 //============================================================================
00006 //
00007 //  This file is part of GPSTk, the GPS Toolkit.
00008 //
00009 //  The GPSTk is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU Lesser General Public License as published
00011 //  by the Free Software Foundation; either version 2.1 of the License, or
00012 //  any later version.
00013 //
00014 //  The GPSTk is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with GPSTk; if not, write to the Free Software Foundation,
00021 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //  
00023 //  Copyright 2004, The University of Texas at Austin
00024 //
00025 //============================================================================
00026 
00027 #ifndef VPLOT_AXIS_H
00028 #define VPLOT_AXIS_H
00029 
00030 #include <cmath>
00031 #include <cstdio>
00032 #include "AxisStyle.hpp"
00033 #include "GraphicsConstants.hpp"
00034 #include "Frame.hpp"
00035 
00039 namespace vplot
00040 {
00041   /*
00042    * I probably need a class for the following things:
00043    *   - Direction for axis
00044    *   - Bounds!
00045    *     - Bound generation from min/max (with buffer area? round?)
00046    * - If this is a seperate class it needs a set of COPY commands
00047    *   that copy to a different orientation.  Or a mirror setting that
00048    *   does so automagically.
00049    */
00050 
00056   class Axis
00057   {
00058     public:
00059       static const double RIGHT; // = 0;
00060       static const double UP   ; // = RIGHT+vdraw::HALF_PI;
00061       static const double LEFT ; // = UP+vdraw::HALF_PI;
00062       static const double DOWN ; // = LEFT+vdraw::HALF_PI;
00063       static const double N    ; // = UP;
00064       static const double E    ; // = RIGHT;
00065       static const double S    ; // = DOWN;
00066       static const double W    ; // = LEFT;
00067       static const double NORTH; // = N;
00068       static const double EAST ; // = E;
00069       static const double SOUTH; // = S;
00070       static const double WEST ; // = W;
00071 
00072       /*
00073        * Constructor/Destructor
00074        */
00075 
00087       Axis(double ix, double iy, double ex, double ey, double length, double imin, double imax, const AxisStyle &style=AxisStyle())
00088       {
00089         //Calculate direction
00090         /*
00091          * tan(dir) = opposite / adjacent;
00092          * arctan(opposite/adjacent) = dir;
00093          * opposite = (ey-iy);
00094          * adjacent = (ex-ix);
00095          */
00096         double direction;
00097         if(ex==ix) 
00098         {
00099           if(ey < iy) direction=vdraw::PI;
00100           else        direction=0;
00101         }
00102         else if(ey==iy)
00103         {
00104           if(ex < ix) direction=vdraw::PI+vdraw::HALF_PI;
00105           else        direction=vdraw::HALF_PI;
00106         }
00107         else
00108         {
00109           direction=atan((ey-iy)/(ex-ix));
00110           if(direction < 0) direction=vdraw::TWO_PI+direction;
00111         }
00112         init(ix,iy,direction,length,imin,imax,style);
00113       }
00114 
00125       Axis(double ix, double iy, double direction, double length, double imin, double imax, const AxisStyle &style=AxisStyle())
00126       {
00127         init(ix,iy,direction,length,imin,imax,style);
00128       }
00129 
00133       ~Axis() 
00134       {
00135       }
00136 
00137       /*
00138        * Accessors/Modifiers
00139        */
00141       inline void setPosition(double ix, double iy) { x=ix; y=iy; }
00142 
00144       inline void setLength(double length) { line_length=length; }
00145 
00147       inline void setRange(double imin, double imax) { min=imin; max=imax; axesMin=min; axesMax=max; }
00148 
00150       inline void setGap(double igap) { gap=igap; }
00151 
00153       void setAngle(double angle);
00154 
00155       /*
00156        * Methods
00157        */
00158 
00163       void drawToFrame(const vdraw::Frame &ff);
00164 
00172       bool pointFromValue(double &x, double &y, double value);
00173 
00182       inline bool labelPointAbove(double &x, double &y, double &rotation, double value)
00183       {
00184         return labelPoint(x,y,rotation,value,AxisStyle::ABOVE);
00185       }
00186 
00195       inline bool labelPointBelow(double &x, double &y, double &rotation, double value)
00196       {
00197         return labelPoint(x,y,rotation,value,AxisStyle::BELOW);
00198       }
00199 
00201       AxisStyle axis_style;
00202 
00203     protected:
00204       /*
00205        * Funtions
00206        */
00207 
00218       void init(double ix, double iy, double direction, double length, double imin, double imax, const AxisStyle &style);
00219 
00220       /*
00221        * Old idea for a helper to draw ticks.  I like my ascii art too much to
00222        * delete it for now :)
00223        *
00224        * For example:
00225        *        sr=.5   sv=0     |--1--|=fd
00226        * |-------50%-------|     |--5--|=rd
00227        * |-----|-----|-----|-----|-----|-----| <-- Actual Line
00228        * -3    -2    -1    0     1     2     3 <-- Actual labels
00229        * Would call as follows:
00230        * axisHelper(f,0.5,0,5,1)
00231        *
00232        * sr Start ratio (% of length to begin point)
00233        * sv Start value (in plot space)
00234        * rd Real distance (in page space)
00235        * fd Fake distance (in plot space)
00236        */
00237 
00244       inline void fromRatio(double &x, double &y, double ratio)
00245       {
00246         x = this->x+cosdir*line_length*ratio;
00247         y = this->y+sindir*line_length*ratio;
00248       }
00249 
00255       inline void axisTick(double ratio, double dist)
00256       {
00257         axisTickRecursive(
00258             true,
00259             axis_style.tick_recursion_depth,
00260             ratio,
00261             dist,
00262             axis_style.major_tick_length);
00263       }
00264 
00273       void axisTickRecursive(bool draw, int depth, double ratio, double dist, double length);
00274 
00280       void drawTick(double ratio, double length);
00281 
00291       void fromTic(double &x1, double &y1, double &x2, double &y2, 
00292           double distratio, double length);
00293 
00308       void guessTickInfo(double &startpos, double &startval, double &distpos, double &distval);
00309 
00316       bool label(double value, int direction, const vdraw::TextStyle &style);
00317 
00325       bool label(const char *str, double value, int direction, const vdraw::TextStyle &style);
00326 
00334       bool label(vdraw::Text &t, double value, int direction, const vdraw::TextStyle &style);
00335 
00340       bool labelPoint(double &x, double &y, double &rotation, double value, int direction);
00341 
00342       /*
00343        * This stuff is specific to the axis in its own right.
00344        */
00347       vdraw::Frame f;
00348 
00350       double x;
00351 
00353       double y;
00354 
00356       double line_length;
00357 
00359       double line_direction;
00361       double sindir;
00363       double cosdir;
00365       double sintic;
00367       double costic;
00368 
00369       /*
00370        * This stuff is related to the data.
00371        */
00373       double max;
00376       double axesMax;
00377 
00378 
00380       double min;
00383       double axesMin;
00384 
00385 
00388       double gap;
00389   };
00390 }
00391 
00392 #endif //VPLOT_AXIS_H
00393 

Generated on Wed Feb 8 03:30:57 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1