math_graphics.c

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *    Graphic library
00003  *    Copyright (C) 1998-2001 Enpc/Jean-Philippe Chancelier
00004  *    jpc@cermics.enpc.fr 
00005  --------------------------------------------------------------------------*/
00006 
00007 #include "math_graphics.h"
00008 
00009 /* 
00010  * we use spConfig.h for machine constants 
00011  * XXX : spConfig should be merged and unified 
00012  *       with other machine constant scilab code 
00013  */
00014 
00015 #define spINSIDE_SPARSE
00016 #include "../../sparse/includes/spConfig.h"
00017 
00018 
00019 double Mini(double *vect, integer n);
00020 double Maxi(double *vect,integer n);
00021 
00022 
00023 double Mini(double *vect, integer n)
00024 {
00025   int i;
00026   double vmin;
00027   vmin = LARGEST_REAL;
00028   for (i = 0 ; i < n ; i++)
00029     /*    if ( isinf(vect[i])== 0 && isnan(vect[i])==0 && vect[i] < vmin)  */
00030     if ( finite(vect[i])== 1 && vect[i] < vmin) 
00031       vmin=vect[i];
00032   return(vmin);
00033 }
00034 
00035 double Maxi(double *vect,integer n)
00036 {
00037   int i;
00038   double maxi;
00039   maxi= - LARGEST_REAL;
00040   for (i =0 ; i < n ; i++)
00041     /* if ( isinf(vect[i])== 0 && isnan(vect[i])==0 && vect[i] > maxi) */
00042     if ( finite(vect[i])== 1 && vect[i] > maxi) 
00043       maxi=vect[i];
00044   return(maxi);
00045 }
00046 
00047 /*----------------------------------------------------------------------------*/
00048 
00049 /* perform the rotation of point from to point dest  */
00050 void rotate2D( double from[2], double center[2], double angle, double dest[2] )
00051 {
00052   rotate2Dim( from, center, cos( angle ), sin( angle ), dest ) ;
00053 }
00054 
00055 /*----------------------------------------------------------------------------*/
00056 /* perform the rotation of point from to point to. */
00057 /* the angle is directly given with its sine and cosine for speed */
00058 void rotate2Dim( double from[2]   ,
00059                  double center[2] ,
00060                  double cosAngle  ,
00061                  double sinAngle  ,
00062                  double dest[2]    )
00063 {
00064   double diff[2] ;
00065 
00066   /* put the center to (0,0) */
00067   diff[0] = from[0] - center[0] ;
00068   diff[1] = from[1] - center[1] ;
00069 
00070   /* turn and translate back */
00071   dest[0] = diff[0] * cosAngle - diff[1] * sinAngle + center[0] ;
00072   dest[1] = diff[0] * sinAngle + diff[1] * cosAngle + center[1] ;
00073 }
00074 /*----------------------------------------------------------------------------*/
00075 /* perform the rotation of point from to point dest given in int with angle in radian  */
00076 void iRotate2D( int from[2], int center[2], double angle, int dest[2] )
00077 {
00078   iRotate2Dim( from, center, cos( angle ), sin( angle ), dest ) ;
00079 }
00080 
00081 /*----------------------------------------------------------------------------*/
00082 /* perform the rotation of point from to point to. */
00083 /* the angle is directly given with its sine and cosine for speed */
00084 void iRotate2Dim( int    from[2]   ,
00085                   int    center[2] ,
00086                   double cosAngle  ,
00087                   double sinAngle  ,
00088                   int    dest[2]    )
00089 {
00090   int diff[2] ;
00091 
00092   /* put the center to (0,0) */
00093   diff[0] = from[0] - center[0] ;
00094   diff[1] = from[1] - center[1] ;
00095 
00096   /* turn and translate back */
00097   dest[0] = round( diff[0] * cosAngle - diff[1] * sinAngle + center[0] ) ;
00098   dest[1] = round( diff[0] * sinAngle + diff[1] * cosAngle + center[1] ) ;
00099 }
00100 /*----------------------------------------------------------------------------*/
00101 /* perform an homethety point from to point dest. The 2 factors stand for the ration */
00102 /* along the 2 coordinates */
00103 void homothety2D( double from[2], double center[2], double factors[2], double dest[2] )
00104 {
00105   dest[0] = center[0] + factors[0] * ( from[0] - center[0] ) ;
00106   dest[1] = center[1] + factors[1] * ( from[1] - center[1] ) ;
00107 }
00108 /*----------------------------------------------------------------------------*/
00109 /* perform an homethety point from to point dest given in pixels. */
00110 /* The 2 factors stand for the ration along the 2 coordinates */
00111 void iHomothety2D( int from[2], int center[2], double factors[2], int dest[2] )
00112 {
00113   dest[0] = round( center[0] + factors[0] * ( from[0] - center[0] ) ) ;
00114   dest[1] = round( center[1] + factors[1] * ( from[1] - center[1] ) ) ;
00115 }
00116 /*----------------------------------------------------------------------------*/
00117 /* perform the translation of point from to point to with vector trans */
00118 void translate2D( double from[2], double trans[2], double dest[2] )
00119 {
00120   dest[0] = from[0] + trans[0] ;
00121   dest[1] = from[1] + trans[1] ;
00122 }
00123 /*----------------------------------------------------------------------------*/
00124 void iTranslate2D( int from[2], int trans[2], int dest[2] )
00125 {
00126   dest[0] = from[0] + trans[0] ;
00127   dest[1] = from[1] + trans[1] ;
00128 }
00129 /*----------------------------------------------------------------------------*/
00130 void normalize2d( double vect[2] )
00131 {
00132   double norm = sqrt( vect[0] * vect[0] + vect[1] * vect[1] ) ;
00133   vect[0] /= norm ;
00134   vect[1] /= norm ;
00135 }
00136 /*----------------------------------------------------------------------------*/
00137 void iNormalize2d( int vect[2] )
00138 {
00139   double norm = sqrt( vect[0] * vect[0] + vect[1] * vect[1] ) ;
00140   vect[0] = round( vect[0] / norm ) ;
00141   vect[1] = round( vect[1] / norm ) ;
00142 }
00143 /*----------------------------------------------------------------------------*/
00144 /* check if two values can be considered equal given an accurracy */
00145 int safeEqual( double val1, double val2, double accuracy )
00146 {
00147   /* the val1 == val2 is put to avoid division by 0 */
00148   return ( val1 == val2 ) || ( Abs( val1 - val2 ) < accuracy * Max( Abs(val1), Abs(val2 ) ) ) ;
00149 }
00150 /*----------------------------------------------------------------------------*/

Generated on Sun Mar 4 15:03:52 2007 for Scilab [trunk] by  doxygen 1.5.1