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

xyagrid.cpp

Go to the documentation of this file.
00001 /*****************************************************************************/
00002 /* Módulo: xyagrid.cpp                                                       */
00003 /* Autor: Camilo da Foseca Freire                                            */
00004 /* Data: 11 jul 96                                                           */
00005 /* Comentário:                                                               */
00006 /*    Implementação de métodos da classe que define um tipo especial de eixo */
00007 /* que desenha raios a partir da origem do eixo, em intervalos angulares de- */
00008 /* finidos pelo usuário.                                                       */
00009 /*****************************************************************************/
00010 
00011 #include <stdlib.h>
00012 #include <math.h>              
00013 #include <stdio.h>
00014 
00015 #include "xyagrid.h"
00016                                                                 
00017 const char* xy_id_xyagrid_cpp="$Id: xyagrid.cpp,v 1.8 1999/12/09 21:47:46 rborges Exp $";
00018                                                                 
00019 XYAngGrid::XYAngGrid (double radius,
00020                       double referenceAngle,
00021                             long color,
00022                       double size,
00023                             double rot,
00024                             double step,
00025                             xybool visible)
00026                             : XYAxis(0., radius, .5, .5, color, size, 0., step,
00027                                      xyfalse, visible),
00028                                     _radius(radius),
00029                                     _referenceAngle(referenceAngle),
00030                                     _first(rot),
00031                                           _style(continuous)
00032 {
00033 }
00034 
00035 void XYAngGrid::radius (double r)
00036 {
00037    _radius = r;
00038 }
00039 
00040 double XYAngGrid::radius (void) const
00041 {
00042    return _radius;
00043 }
00044 
00045 void XYAngGrid::referenceAngle (double r)
00046 {
00047    _referenceAngle = r;
00048 }
00049 
00050 double XYAngGrid::referenceAngle (void) const
00051 {
00052    return _referenceAngle;
00053 }
00054 
00055 void XYAngGrid::first (double f)
00056 {
00057    _first = f;
00058 }
00059 
00060 double XYAngGrid::first (void) const
00061 {
00062    return _first;
00063 }
00064 
00065 void XYAngGrid::style (int s)
00066 {
00067    _style = s;
00068 }
00069 
00070 int XYAngGrid::style (void) const
00071 {
00072    return _style;
00073 }
00074 
00075 xybool XYAngGrid::pointInGrid (int px, int py, int x0, int y0, int x1, int y1)
00076 {
00077    // distância entre os pontos extremos do segmento (x0, y0) - (x1, y1)
00078    double distance = mtDistance (x0, y0, x1, y1);
00079 
00080    // distância entre (x0, y0) e (px, py)
00081    double dist1 = mtDistance (x0, y0, px, py);
00082    // distância entre (px, py) e (x1, y1)
00083    double dist2 = mtDistance (px, py, x1, y1);
00084 
00085    // verifica se a soma das distâncias de (px, py) aos extremos do
00086    // intervalo é igual a distância entre esses extremos
00087    if (mtEqual(dist1 + dist2, distance, 0.1))
00088       return xytrue;
00089 
00090    return xyfalse;
00091 }
00092 
00093 xybool XYAngGrid::pick (int px, int py)
00094 {
00095    if (visible() == xyfalse)    // invisível!!!
00096       return xyfalse;
00097 
00098    // tamanho normalizado em relação ao canvas
00099    double s = _radius * _size / (_mx - _mn);
00100 
00101    // (x0, y0) posição inicial da grade
00102    double x0, y0;
00103    position (&x0, &y0);
00104 
00105    int px0, py0, px1, py1;
00106    double x1, y1;
00107 
00108    for (double p = 0.; p <= 360; p += _step)
00109    {
00110       x1 = x0 + (s * cos(p * XY_PI / 180.));
00111       y1 = y0 + (s * sin(p * XY_PI / 180.));
00112 
00113       // converte coordenadas do mundo para coordenadas em pixel
00114       wdWorld2Canvas (x0, y0, &px0, &py0);
00115       wdWorld2Canvas (x1, y1, &px1, &py1);
00116 
00117       // verifica se o ponto está sobre uma linha da grade
00118       if (pointInGrid(px, py, px0, py0, px1, py1))
00119          return xytrue;
00120    }
00121 
00122    return xyfalse;
00123 }
00124 
00125 void XYAngGrid::draw (void)
00126 {
00127    draw (_xmin, _ymin, _xmax, _ymax);
00128 }
00129 
00130 void XYAngGrid::draw (int xmin, int ymin, int xmax, int ymax) const
00131 {
00132    if (visible() == xyfalse)    // invisível!!!
00133       return;
00134 
00135    int mode = cdClip(CD_CLIPON);
00136 
00137    // guarda a região de clip corrente
00138    int oldclip_xmin, oldclip_xmax, oldclip_ymin, oldclip_ymax;
00139    cdGetClipArea (&oldclip_xmin, &oldclip_xmax, &oldclip_ymin, &oldclip_ymax);
00140    // define nova região de clip
00141    cdClipArea(xmin, xmax, ymin, ymax);
00142 
00143    // tamanho normalizado em relação ao canvas
00144    double s = _radius * _size / (_mx - _mn);
00145 
00146    // verifica se a grade é muito compacta
00147    if (_step <= 1.0)
00148       return;
00149 
00150    // (x0, y0) posição inicial da grade
00151    double x0, y0;
00152    position (&x0, &y0);
00153 
00154    // desenha grade com linha sólida mais fina possível e com cor definida
00155    // pelo usuário
00156    cdLineWidth  (1);
00157    cdLineStyle  (_style);
00158    cdForeground (_color);
00159 
00160    for (double p = _referenceAngle; p <= 360+_referenceAngle; p += _step)
00161       // desenha raio da grade
00162       wdLine(x0, y0, x0 + (s * cos(p * XY_PI / 180.)),
00163              y0 + (s * sin(p * XY_PI / 180.)));
00164 
00165    cdClip(mode);
00166    // restaura região de clip anterior
00167    cdClipArea(oldclip_xmin, oldclip_xmax, oldclip_ymin, oldclip_ymax);
00168 }
00169 
00170 void XYAngGrid::boundingBox (int& xmin, int& ymin, int& xmax, int& ymax) const
00171 {
00172    if (visible() == xyfalse)    // invisível!!!
00173       return;
00174 
00175    // tamanho normalizado em relação ao canvas
00176    double s = _radius * _size / (_mx - _mn);
00177 
00178    // verifica se a grade é muito compacta
00179    if (_step <= 1.0)
00180       return;
00181 
00182    // (x0, y0) posição inicial da grade
00183    double x0, y0;
00184    position (&x0, &y0);
00185 
00186    int bx1, by1;
00187    wdWorld2Canvas (x0, y0, &bx1, &by1);
00188 
00189    // inicializa boundingBox
00190    xmin = bx1;
00191    ymin = by1;
00192    xmax = bx1;
00193    ymax = by1;
00194 
00195    double x1, y1;
00196 
00197    for (double p = 0.; p <= 360; p += _step)
00198    {
00199       x1 = x0 + (s * cos(p * XY_PI / 180.));
00200       y1 = y0 + (s * sin(p * XY_PI / 180.));
00201 
00202       wdWorld2Canvas (x1, y1, &bx1, &by1);
00203 
00204       // boundingBox da grade
00205       xmin = MIN(bx1, xmin);
00206       ymin = MIN(by1, ymin);
00207       xmax = MAX(bx1, xmax);
00208       ymax = MAX(by1, ymax);
00209    }
00210 }
00211 

XY
Tecgraf / PUC-Rio - Computer Graphics Technology Group