PhysicalAxis.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 PhysicalAxis.cs
00005 Copyright (C) 2003
00006 Matt Howlett, Paolo Pierini
00007 
00008 Redistribution and use of NPlot or parts there-of in source and
00009 binary forms, with or without modification, are permitted provided
00010 that the following conditions are met:
00011 
00012 1. Re-distributions in source form must retain at the head of each
00013    source file the above copyright notice, this list of conditions
00014    and the following disclaimer.
00015 
00016 2. Any product ("the product") that makes use NPlot or parts 
00017    there-of must either:
00018   
00019     (a) allow any user of the product to obtain a complete machine-
00020         readable copy of the corresponding source code for the 
00021         product and the version of NPlot used for a charge no more
00022         than your cost of physically performing source distribution,
00023         on a medium customarily used for software interchange, or:
00024 
00025     (b) reproduce the following text in the documentation, about 
00026         box or other materials intended to be read by human users
00027         of the product that is provided to every human user of the
00028         product: 
00029    
00030               "This product includes software developed as 
00031               part of the NPlot library project available 
00032               from: http://www.nplot.com/" 
00033 
00034         The words "This product" may optionally be replace with 
00035         the actual name of the product.
00036 
00037 ------------------------------------------------------------------------
00038 
00039 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00040 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00041 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00042 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00043 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00044 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00045 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00046 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00047 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00048 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00049 
00050 */
00051 
00052 using System;
00053 using System.Drawing;
00054 using System.Drawing.Drawing2D;
00055 using System.Collections;
00056 
00057 namespace NPlot
00058 {
00078         public class PhysicalAxis
00079         {
00080 
00084                 private PhysicalAxis()
00085                 {
00086                 }
00087 
00088 
00095                 public PhysicalAxis( Axis a, Point physicalMin, Point physicalMax )
00096                 {
00097                         this.Axis = a;
00098                         this.PhysicalMin = physicalMin;
00099                         this.PhysicalMax = physicalMax;
00100                 }
00101 
00102 
00107                 public virtual Rectangle GetBoundingBox()
00108                 {
00109                         System.Drawing.Bitmap scratchArea_ = new System.Drawing.Bitmap( 1, 1 );
00110                         Graphics g = Graphics.FromImage( scratchArea_ );
00111                         Rectangle bounds;
00112                         this.Draw( g, out bounds );
00113                         return bounds;
00114                 }
00115 
00116         
00123                 public virtual void Draw( System.Drawing.Graphics g, out Rectangle boundingBox )
00124                 {
00125                         this.Axis.Draw( g, PhysicalMin, PhysicalMax, out boundingBox );
00126                 }
00127 
00128 
00136                 public PointF WorldToPhysical( double coord, bool clip )
00137                 {
00138                         return Axis.WorldToPhysical( coord, PhysicalMin, PhysicalMax, clip );
00139                 }
00140 
00141 
00152                 public double PhysicalToWorld( Point p, bool clip )
00153                 {
00154                         return Axis.PhysicalToWorld( p, PhysicalMin, PhysicalMax, clip );
00155                 }
00156 
00157 
00164                 public void SetWorldLimitsFromPhysical( Point min, Point max )
00165                 {
00166                         double minc;
00167                         double maxc;
00168                         if (Axis != null)
00169                         {
00170                                 minc = Axis.WorldMin;
00171                                 maxc = Axis.WorldMax;
00172                                 if ( !Axis.Reversed ) 
00173                                 {
00174                                         double tmp = this.PhysicalToWorld(min,true);
00175                                         Axis.WorldMax = this.PhysicalToWorld(max,true);
00176                                         Axis.WorldMin = tmp;
00177                                 }
00178                                 else
00179                                 {
00180                                         double tmp = this.PhysicalToWorld(min,true);
00181                                         Axis.WorldMin = this.PhysicalToWorld(max,true);
00182                                         Axis.WorldMax = tmp;
00183                                 }
00184                                 // need to trap somehow if the user selects an 
00185                                 // arbitrarily small range. Otherwise the GDI+ 
00186                                 // drawing routines lead to an overflow in painting 
00187                                 // the picture. This may be not the optimal solution,
00188                                 // but if the GDI+ draw leads to an overflow the
00189                                 // graphic surface becomes unusable anymore and I
00190                                 // had difficulty to trap the error.
00191                                 double half = (Axis.WorldMin + Axis.WorldMax)/2;
00192                                 double width = Axis.WorldMax - Axis.WorldMin;
00193                                 if (Math.Abs(half/width) > 1.0e12)
00194                                 {
00195                                         Axis.WorldMin = minc;
00196                                         Axis.WorldMax = maxc;
00197                                 }
00198                         }
00199                 }
00200 
00201 
00205                 public Point PhysicalMin
00206                 {
00207                         get
00208                         {
00209                                 return physicalMin_;
00210                         }
00211                         set
00212                         {
00213                                 physicalMin_ = value;
00214                         }
00215                 }
00216                 private Point physicalMin_;
00217 
00218 
00222                 public Point PhysicalMax
00223                 {
00224                         get
00225                         {
00226                                 return physicalMax_;
00227                         }
00228                         set
00229                         {
00230                                 physicalMax_ = value;
00231                         }
00232                 }
00233                 private Point physicalMax_;
00234 
00235 
00239                 public Axis Axis
00240                 {
00241                         get
00242                         {
00243                                 return axis_;
00244                         }
00245                         set
00246                         {
00247                                 axis_ = value;
00248                         }
00249                 }
00250                 private Axis axis_;
00251         
00252 
00256                 public int PhysicalLength
00257                 {
00258                         get
00259                         {
00260                                 return Utils.Distance( PhysicalMin, PhysicalMax );
00261                         }
00262                 }
00263 
00267                 public double PixelWorldLength
00268                 {
00269                         get
00270                         {
00271                                 return this.Axis.WorldLength / this.PhysicalLength;
00272                         }
00273                 }
00274 
00275         }
00276 }

Generated on Sat Nov 5 01:04:06 2005 for NPlot by  doxygen 1.4.5