Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

sxy_utils.cpp

Go to the documentation of this file.
00001 /* ------------------------------------------------------------------------- */
00002 /* ------------------------------------------------------------------------- */
00003 /* ------------------------------------------------------------------------- */
00004 
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <stdio.h>
00008 #include <assert.h>
00009 #include <ctype.h>
00010 #include <math.h>
00011 #include <float.h>
00012 
00013 #include <cd.h>
00014 
00015 #include "sxy_utils.h"
00016 
00017 #define min(a,b) ( ((a)<(b)) ? (a) : (b) )
00018 #define max(a,b) ( ((a)>(b)) ? (a) : (b) )
00019 
00020 /* ------------------------------------------------------------------------- */
00021 
00022 /* ------------------------------------------------------------------------- */
00023 
00024 /* ......................................................................... */
00032 double SXYUtil::makeInterpolation( double x1, double y1, 
00033     double x2, double y2, double x ) {
00034     return ((((x-x1)*(y2-y1))/(x2-x1))+y1);
00035 }
00036 
00037 /* ......................................................................... */
00043 void SXYUtil::calcZoomOut(double &xmin, double &xmax) {
00044    double dx = (xmax - xmin) / 10.0;
00045    xmin -= dx; xmax += dx;
00046 }
00047 
00048 /* ......................................................................... */
00049 
00050 void SXYUtil::assertValues(double &min, double &max) {
00051   if (min == FLT_MAX) {
00052     if (max == -FLT_MAX) {
00053         max = 1000;
00054         min = 0;
00055     }
00056     else min = max - 10;
00057   }
00058   if (max == -FLT_MAX) max = min + 10;
00059 }
00060 
00061 /* ......................................................................... */
00069 void SXYUtil::calcMinMax(double &xmin, double &xmax, 
00070 double &ymin, double &ymax) {
00071   
00072   SXYUtil::assertValues(xmin, xmax);
00073   SXYUtil::assertValues(ymin, ymax);
00074 
00075   if (xmin >= xmax) {
00076       double tmp = xmin;
00077       xmin = xmax - 10;
00078       xmax = tmp + 10;
00079   }
00080   if (ymin >= ymax) {
00081       double tmp = ymin;
00082       ymin = ymax - 10;
00083       ymax = tmp + 10;
00084   }
00085 }
00086 
00087 /* ......................................................................... */
00093 int SXYUtil::calcPrecision(double min, double max) {
00094   double diff = fabs(max - min);
00095   assert (diff >= 0);
00096   if (diff > 100) return 0;
00097   else return -((int)log10(diff)) + 2;
00098 }
00099 
00100 /* ......................................................................... */
00116 int SXYUtil::intersectPoints( int is_log_x, int is_log_y,
00117 double ax1, double ay1, double ax2, double ay2,
00118 double bx1, double by1, double bx2, double by2,
00119 double& ix, double& iy ) {
00120 
00121   if (is_log_x) {
00122      if ( (ax1<1e-15) || (ax2<1e-15) || (bx1<1e-15) || (bx2<1e-15) ) return 0;
00123      ax1 = log10(ax1); ax2 = log10(ax2);
00124      bx1 = log10(bx1); bx2 = log10(bx2);
00125   }
00126 
00127   if (is_log_y) {
00128      if ( (ay1<1e-15) || (ay2<1e-15) || (by1<1e-15) || (by2<1e-15) ) return 0;
00129      ay1 = log10(ay1); ay2 = log10(ay2);
00130      by1 = log10(by1); by2 = log10(by2);
00131   }
00132 
00133   // Cálculo dos coeficientes (alfa e beta) dos segmentos A e B 
00134   double dy_a = ay2-ay1;
00135   double dy_b = by2-by1;
00136   double dx_a = ax2-ax1;
00137   double dx_b = bx2-bx1;
00138 
00139   // Retas verticais e paralelas
00140   int va = fabs(dx_a) < 1e-5;
00141   int vb = fabs(dx_b) < 1e-5;
00142   if (va && vb) return 0;
00143 
00144   if (va) {
00145      ix = ax1;
00146      iy = SXYUtil::makeInterpolation(bx1,by1,bx2,by2,ix);
00147   }
00148   else if (vb) {
00149      ix = bx1;
00150      iy = SXYUtil::makeInterpolation(ax1,ay1,ax2,ay2,ix);
00151   }
00152   else {
00153      double alfa_a = dy_a / dx_a;
00154      double alfa_b = dy_b / dx_b;
00155      double beta_a = ay1 - alfa_a*ax1;
00156      double beta_b = by1 - alfa_b*bx1;
00157 
00158      // Teste de retas paralelas
00159      if ( fabs(alfa_a - alfa_b) < 1e-5 ) return 0;
00160 
00161      ix = (-beta_a + beta_b) / (alfa_a - alfa_b);
00162      iy = alfa_a * ix + beta_a;
00163   }
00164 
00165   ix = is_log_x ? pow(10,ix) : ix;
00166   iy = is_log_y ? pow(10,iy) : iy;
00167 
00168   if (is_log_x) {
00169      ax1 = pow(10,ax1); ax2 = pow(10,ax2);
00170      bx1 = pow(10,bx1); bx2 = pow(10,bx2);
00171   }
00172 
00173   if (is_log_y) {
00174      ay1 = pow(10,ay1); ay2 = pow(10,ay2);
00175      by1 = pow(10,by1); by2 = pow(10,by2);
00176   }
00177 
00178   double laminx = min(ax1,ax2);
00179   double laminy = min(ay1,ay2);
00180   double lamaxx = max(ax1,ax2);
00181   double lamaxy = max(ay1,ay2);
00182   if ( ix<laminx || ix>lamaxx || iy<laminy || iy>lamaxy ) return 0;
00183 
00184   double lbminx = min(bx1,bx2);
00185   double lbminy = min(by1,by2);
00186   double lbmaxx = max(bx1,bx2);
00187   double lbmaxy = max(by1,by2);
00188   if ( ix<lbminx || ix>lbmaxx || iy<lbminy || iy>lbmaxy ) return 0;
00189 
00190   return 1;
00191 }
00192 

SXY
Tecgraf / PUC-Rio - Computer Graphics Technology Group