AxesConstraint.cs

Go to the documentation of this file.
00001 /*
00002 NPlot - A charting library for .NET
00003 
00004 AxesConstraint.cs
00005 Copyright (C) 2003
00006 Matt Howlett
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 
00056 namespace NPlot
00057 {
00058 
00064         public abstract class AxesConstraint
00065         {
00066 
00073                 public class XPixelWorldLength : AxesConstraint
00074                 {
00075                         private double pWorldLength_ = 0.0f;
00076                         private object holdFixedY_ = null;
00077 
00084                         public XPixelWorldLength( double p )
00085                         {
00086                                 this.pWorldLength_ = p;
00087                         }
00088                 
00098                         public XPixelWorldLength( double p, PlotSurface2D.YAxisPosition holdFixedY )
00099                         {
00100                                 this.pWorldLength_ = p;
00101                                 this.holdFixedY_ = holdFixedY;
00102                         }
00103 
00111                         public override void ApplyConstraint( 
00112                                 PhysicalAxis pXAxis1, PhysicalAxis pYAxis1, 
00113                                 PhysicalAxis pXAxis2, PhysicalAxis pYAxis2 )
00114                         {
00115                                 int desiredLength = (int)(pXAxis1.Axis.WorldLength / (double)this.pWorldLength_);
00116                                 int currentLength = pXAxis1.PhysicalLength;
00117                                 int delta = currentLength - desiredLength;
00118 
00119                                 int changeLeft = delta / 2;
00120                                 int changeRight = delta / 2;
00121                                 if (this.holdFixedY_ != null)
00122                                 {
00123                                         if ( (PlotSurface2D.YAxisPosition)this.holdFixedY_ == PlotSurface2D.YAxisPosition.Left )
00124                                         {
00125                                                 changeLeft = 0;
00126                                                 changeRight = delta;
00127                                         }
00128                                         else
00129                                         {
00130                                                 changeLeft = delta;
00131                                                 changeRight = 0;
00132                                         }
00133                                 }
00134 
00135                                 pXAxis1.PhysicalMin = new Point( pXAxis1.PhysicalMin.X+changeLeft, pXAxis1.PhysicalMin.Y );
00136                                 pXAxis1.PhysicalMax = new Point( pXAxis1.PhysicalMax.X-changeRight, pXAxis1.PhysicalMax.Y );
00137                                 pXAxis2.PhysicalMin = new Point( pXAxis2.PhysicalMin.X+changeLeft, pXAxis2.PhysicalMin.Y );
00138                                 pXAxis2.PhysicalMax = new Point( pXAxis2.PhysicalMax.X-changeRight, pXAxis2.PhysicalMax.Y );
00139 
00140                                 pYAxis1.PhysicalMin = new Point( pYAxis1.PhysicalMin.X+changeLeft, pYAxis1.PhysicalMin.Y );
00141                                 pYAxis1.PhysicalMax = new Point( pYAxis1.PhysicalMax.X+changeLeft, pYAxis1.PhysicalMax.Y );
00142                                 pYAxis2.PhysicalMin = new Point( pYAxis2.PhysicalMin.X-changeRight, pYAxis2.PhysicalMin.Y );
00143                                 pYAxis2.PhysicalMax = new Point( pYAxis2.PhysicalMax.X-changeRight, pYAxis2.PhysicalMax.Y );
00144                 
00145                         }
00146                 }
00147 
00148 
00155                 public class YPixelWorldLength : AxesConstraint
00156                 {
00157                         private double pWorldLength_ = 0.0;
00158                         private object holdFixedX_ = null;
00159 
00166                         public YPixelWorldLength( double p )
00167                         {
00168                                 this.pWorldLength_ = p;
00169                         }
00170                 
00178                         public YPixelWorldLength( double p, PlotSurface2D.XAxisPosition holdFixedX )
00179                         {
00180                                 this.pWorldLength_ = p;
00181                                 this.holdFixedX_ = holdFixedX;
00182                         }
00183 
00184 
00192                         public override void ApplyConstraint( 
00193                                 PhysicalAxis pXAxis1, PhysicalAxis pYAxis1, 
00194                                 PhysicalAxis pXAxis2, PhysicalAxis pYAxis2 )
00195                         {
00196 
00197                                 int desiredLength = (int)(pYAxis1.Axis.WorldLength / this.pWorldLength_);
00198                                 int currentLength = pYAxis1.PhysicalLength;
00199                                 int delta = currentLength - desiredLength;
00200 
00201                                 int changeBottom = -delta / 2;
00202                                 int changeTop = -delta / 2;
00203                                 if (this.holdFixedX_ != null)
00204                                 {
00205                                         if ( (PlotSurface2D.XAxisPosition)this.holdFixedX_ == PlotSurface2D.XAxisPosition.Bottom )
00206                                         {
00207                                                 changeBottom = 0;
00208                                                 changeTop = -delta;
00209                                         }
00210                                         else
00211                                         {
00212                                                 changeBottom = -delta;
00213                                                 changeTop = 0;
00214                                         }
00215                                 }
00216 
00217                                 pYAxis1.PhysicalMin = new Point( pYAxis1.PhysicalMin.X, pYAxis1.PhysicalMin.Y+changeBottom );
00218                                 pYAxis1.PhysicalMax = new Point( pYAxis1.PhysicalMax.X, pYAxis1.PhysicalMax.Y-changeTop );
00219                                 pYAxis2.PhysicalMin = new Point( pYAxis2.PhysicalMin.X, pYAxis2.PhysicalMin.Y+changeBottom );
00220                                 pYAxis2.PhysicalMax = new Point( pYAxis2.PhysicalMax.X, pYAxis2.PhysicalMax.Y-changeTop );
00221 
00222                                 pXAxis1.PhysicalMin = new Point( pXAxis1.PhysicalMin.X, pXAxis1.PhysicalMin.Y+changeBottom );
00223                                 pXAxis1.PhysicalMax = new Point( pXAxis1.PhysicalMax.X, pXAxis1.PhysicalMax.Y+changeBottom );
00224                                 pXAxis2.PhysicalMin = new Point( pXAxis2.PhysicalMin.X, pXAxis2.PhysicalMin.Y-changeTop );
00225                                 pXAxis2.PhysicalMax = new Point( pXAxis2.PhysicalMax.X, pXAxis2.PhysicalMax.Y-changeTop );
00226 
00227                         }
00228                 }
00229 
00230 
00236                 public class AxisPosition : AxesConstraint
00237                 {
00238 
00239                         private object xAxisPosition_;
00240                         private object yAxisPosition_;
00241                         private int position_;
00242 
00243                         
00250                         public AxisPosition( PlotSurface2D.XAxisPosition axis, int yPosition )
00251                         {
00252                                 position_ = yPosition;
00253                                 xAxisPosition_ = axis;
00254                         }
00255 
00256 
00263                         public AxisPosition( PlotSurface2D.YAxisPosition axis, int xPosition )
00264                         {
00265                                 position_ = xPosition;
00266                                 yAxisPosition_ = axis;
00267                         }
00268 
00276                         public override void ApplyConstraint( 
00277                                 PhysicalAxis pXAxis1, PhysicalAxis pYAxis1, 
00278                                 PhysicalAxis pXAxis2, PhysicalAxis pYAxis2 )
00279                         {
00280 
00281                                 if ( xAxisPosition_ != null )
00282                                 {
00283 
00284                                         if ((PlotSurface2D.XAxisPosition)xAxisPosition_ == PlotSurface2D.XAxisPosition.Bottom)
00285                                         {
00286                                                 pXAxis1.PhysicalMin = new Point( pXAxis1.PhysicalMin.X, position_ );
00287                                                 pXAxis1.PhysicalMax = new Point( pXAxis1.PhysicalMax.X, position_ );
00288                         
00289                                                 pYAxis1.PhysicalMin = new Point( pYAxis1.PhysicalMin.X, position_ );
00290                                                 pYAxis2.PhysicalMin = new Point( pYAxis2.PhysicalMin.X, position_ );
00291                                         }
00292                                         else
00293                                         {
00294                                                 pXAxis2.PhysicalMin = new Point( pXAxis2.PhysicalMin.X, position_ );
00295                                                 pXAxis2.PhysicalMax = new Point( pXAxis2.PhysicalMax.X, position_ );
00296 
00297                                                 pYAxis1.PhysicalMax = new Point( pYAxis1.PhysicalMax.X, position_ );
00298                                                 pYAxis2.PhysicalMax = new Point( pYAxis2.PhysicalMax.X, position_ );
00299                                         }
00300 
00301                                 }
00302                                 else if (yAxisPosition_ != null )
00303                                 {
00304 
00305                                         if ((PlotSurface2D.YAxisPosition)yAxisPosition_ == PlotSurface2D.YAxisPosition.Left)
00306                                         {
00307                                                 pYAxis1.PhysicalMin = new Point( position_, pYAxis1.PhysicalMin.Y );
00308                                                 pYAxis1.PhysicalMax = new Point( position_, pYAxis1.PhysicalMax.Y );
00309 
00310                                                 pXAxis1.PhysicalMin = new Point( position_, pXAxis1.PhysicalMin.Y );
00311                                                 pXAxis2.PhysicalMin = new Point( position_, pXAxis2.PhysicalMin.Y );
00312                                         }
00313                                         else
00314                                         {
00315                                                 pYAxis2.PhysicalMin = new Point( position_, pYAxis2.PhysicalMin.Y );
00316                                                 pYAxis2.PhysicalMax = new Point( position_, pYAxis2.PhysicalMax.Y );
00317                                 
00318                                                 pXAxis1.PhysicalMax = new Point( position_, pXAxis1.PhysicalMax.Y );
00319                                                 pXAxis2.PhysicalMax = new Point( position_, pXAxis2.PhysicalMax.Y );
00320                                         }
00321 
00322                                 }
00323 
00324                         }
00325 
00326                 }
00327 
00328 
00342                 public class AspectRatio : AxesConstraint
00343                 {
00344                         private double a_;
00345                         private object holdFixedX_ = null;
00346                         private object holdFixedY_ = null;
00347 
00352                         public AspectRatio( double a )
00353                         {
00354                                 this.a_ = a;
00355                         }
00356 
00365                         public AspectRatio( double a, PlotSurface2D.XAxisPosition holdFixedX )
00366                         {
00367                                 this.a_ = a;
00368                                 this.holdFixedX_ = holdFixedX;
00369                         }
00370 
00379                         public AspectRatio( double a, PlotSurface2D.YAxisPosition holdFixedY )
00380                         {
00381                                 this.a_ = a;
00382                                 this.holdFixedY_ = holdFixedY;
00383                         }
00384 
00391                         public AspectRatio( 
00392                                 double a,
00393                                 PlotSurface2D.XAxisPosition holdFixedX, 
00394                                 PlotSurface2D.YAxisPosition holdFixedY )
00395                         {
00396                                 this.a_ = a;
00397                                 this.holdFixedX_ = holdFixedX;
00398                                 this.holdFixedY_ = holdFixedY;
00399                         }
00400                                                         
00408                         public override void ApplyConstraint( 
00409                                 PhysicalAxis pXAxis1, PhysicalAxis pYAxis1, 
00410                                 PhysicalAxis pXAxis2, PhysicalAxis pYAxis2 )
00411                         {
00412                                 double xWorldRange = Math.Abs( pXAxis1.Axis.WorldMax - pXAxis1.Axis.WorldMin );
00413                                 double xPhysicalRange = Math.Abs( pXAxis1.PhysicalMax.X - pXAxis1.PhysicalMin.X );
00414                                 double xDirPixelSize =  xWorldRange / xPhysicalRange;
00415                         
00416                                 double yWorldRange = Math.Abs( pYAxis1.Axis.WorldMax - pYAxis1.Axis.WorldMin );
00417                                 double yPhysicalRange = Math.Abs( pYAxis1.PhysicalMax.Y - pYAxis1.PhysicalMin.Y );
00418                                 double yDirPixelSize =  yWorldRange / yPhysicalRange;
00419 
00420                                 double currentAspectRatio = yDirPixelSize / xDirPixelSize;
00421 
00422                                 // we want to change the current aspect ratio to be the desired.
00423                                 // to do this, we may only add the world pixel lengths.
00424 
00425                                 if ( this.a_ > currentAspectRatio )
00426                                 {
00427                                         // want to increase aspect ratio. Therefore, want to add some amount
00428                                         // to yDirPixelSize (numerator).
00429 
00430                                         double toAdd = ( this.a_ - currentAspectRatio ) * xDirPixelSize;
00431                                         int newHeight =
00432                                                 (int)( Math.Abs(pYAxis1.Axis.WorldMax - pYAxis1.Axis.WorldMin) / (yDirPixelSize + toAdd) );
00433                                         int changeInHeight = (int)yPhysicalRange - newHeight;
00434 
00435                                         int changeBottom = changeInHeight/2;
00436                                         int changeTop = changeInHeight/2;
00437                                         if (this.holdFixedX_ != null)
00438                                         {
00439                                                 if ( (PlotSurface2D.XAxisPosition)this.holdFixedX_ == PlotSurface2D.XAxisPosition.Bottom )
00440                                                 {
00441                                                         changeBottom = 0;
00442                                                         changeTop = changeInHeight;
00443                                                 }
00444                                                 else
00445                                                 {
00446                                                         changeBottom = changeInHeight;
00447                                                         changeTop = 0;
00448                                                 }
00449                                         }
00450 
00451                                         pYAxis1.PhysicalMin = new Point( pYAxis1.PhysicalMin.X, pYAxis1.PhysicalMin.Y-changeBottom );
00452                                         pYAxis1.PhysicalMax = new Point( pYAxis1.PhysicalMax.X, pYAxis1.PhysicalMax.Y+changeTop );
00453                                         pYAxis2.PhysicalMin = new Point( pYAxis2.PhysicalMin.X, pYAxis2.PhysicalMin.Y-changeBottom );
00454                                         pYAxis2.PhysicalMax = new Point( pYAxis2.PhysicalMax.X, pYAxis2.PhysicalMax.Y+changeTop );
00455 
00456                                         pXAxis1.PhysicalMin = new Point( pXAxis1.PhysicalMin.X, pXAxis1.PhysicalMin.Y-changeBottom );
00457                                         pXAxis1.PhysicalMax = new Point( pXAxis1.PhysicalMax.X, pXAxis1.PhysicalMax.Y-changeBottom );
00458                                         pXAxis2.PhysicalMin = new Point( pXAxis2.PhysicalMin.X, pXAxis2.PhysicalMin.Y+changeTop );
00459                                         pXAxis2.PhysicalMax = new Point( pXAxis2.PhysicalMax.X, pXAxis2.PhysicalMax.Y+changeTop );
00460 
00461                                 }
00462 
00463                                 else 
00464                                 {
00465 
00466                                         // want to decrease aspect ratio. Therefore, want to add some amount
00467                                         // to xDirPixelSize (denominator).
00468 
00469                                         double toAdd = yDirPixelSize / this.a_ - xDirPixelSize;
00470                                         int newWidth = 
00471                                                 (int)( Math.Abs(pXAxis1.Axis.WorldMax - pXAxis1.Axis.WorldMin) / (xDirPixelSize + toAdd) );
00472                                         int changeInWidth = (int)xPhysicalRange - newWidth;
00473 
00474                                         int changeLeft = changeInWidth / 2;
00475                                         int changeRight = changeInWidth / 2;
00476                                         if (this.holdFixedY_ != null)
00477                                         {
00478                                                 if ( (PlotSurface2D.YAxisPosition)this.holdFixedY_ == PlotSurface2D.YAxisPosition.Left )
00479                                                 {
00480                                                         changeLeft = 0;
00481                                                         changeRight = changeInWidth;
00482                                                 }
00483                                                 else
00484                                                 {
00485                                                         changeLeft = changeInWidth;
00486                                                         changeRight = 0;
00487                                                 }
00488                                         }
00489 
00490                                         pXAxis1.PhysicalMin = new Point( pXAxis1.PhysicalMin.X+changeLeft, pXAxis1.PhysicalMin.Y );
00491                                         pXAxis1.PhysicalMax = new Point( pXAxis1.PhysicalMax.X-changeRight, pXAxis1.PhysicalMax.Y );
00492                                         pXAxis2.PhysicalMin = new Point( pXAxis2.PhysicalMin.X+changeLeft, pXAxis2.PhysicalMin.Y );
00493                                         pXAxis2.PhysicalMax = new Point( pXAxis2.PhysicalMax.X-changeRight, pXAxis2.PhysicalMax.Y );
00494 
00495                                         pYAxis1.PhysicalMin = new Point( pYAxis1.PhysicalMin.X+changeLeft, pYAxis1.PhysicalMin.Y );
00496                                         pYAxis1.PhysicalMax = new Point( pYAxis1.PhysicalMax.X+changeLeft, pYAxis1.PhysicalMax.Y );
00497                                         pYAxis2.PhysicalMin = new Point( pYAxis2.PhysicalMin.X-changeRight, pYAxis2.PhysicalMin.Y );
00498                                         pYAxis2.PhysicalMax = new Point( pYAxis2.PhysicalMax.X-changeRight, pYAxis2.PhysicalMax.Y );
00499 
00500                                 }
00501 
00502                         }
00503 
00504                 }
00505                 
00506 
00514                 public abstract void ApplyConstraint( 
00515                         PhysicalAxis pXAxis1, PhysicalAxis pYAxis1, 
00516                         PhysicalAxis pXAxis2, PhysicalAxis pYAxis2 );
00517         }
00518 
00519 }

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