IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 18, 2010, 12:49:05 PM (16 years ago)
Author:
eugene
Message:

merging changes from trunk into branches/pap

Location:
branches/pap
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/pap

  • branches/pap/Ohana

  • branches/pap/Ohana/src/kapa2/src/DrawFrame.c

    r27603 r28003  
    11# include "Ximage.h"
    2 # define MIN_RANGE 1e-10
     2
    33# define DrawLine(X,Y,DX,DY) (XDrawLine (graphic->display, graphic->window, graphic->gc, (int)(X), (int)(Y), (int)(X+DX), (int)(Y+DY)))
    44 
    5 static Graphic *graphic;
    6 
    7 void DrawFrame (KapaGraphWidget *graph) {
    8  
    9   int i, fx, fy, dfx, dfy, P, IsLabel, IsMajor;
    10   double range, major, minor, first, next, lweight;
     5int DrawFrame (KapaGraphWidget *graph) {
     6 
     7  int i, j, Nticks, P;
     8  double fx, fy, dfx, dfy, lweight;
     9  Graphic *graphic;
     10  TickMarkData *ticks;
     11  int color;
    1112
    1213  graphic = GetGraphic();
    1314
    14   /* each axis is drawn independently, but ticks and labels are placed according to
    15      perpendicular distance. */
     15  /* each axis is drawn independently, but ticks and labels are placed according to perpendicular distance. */
    1616  for (i = 0; i < 4; i++) {
    17     fx = graph[0].axis[i].fx;
    18     fy = graph[0].axis[i].fy;
     17    fx  = graph[0].axis[i].fx;
     18    fy  = graph[0].axis[i].fy;
    1919    dfx = graph[0].axis[i].dfx;
    2020    dfy = graph[0].axis[i].dfy;
    21     P = hypot ((double)graph[0].axis[(i+1)%2].dfx, (double)graph[0].axis[(i+1)%2].dfy);
     21    P = hypot (graph[0].axis[(i+1)%2].dfx, graph[0].axis[(i+1)%2].dfy);
    2222
    2323    lweight = MAX (0, MIN (10, graph[0].axis[i].lweight));
     24    color = MAX (0, MIN (15, graph[0].axis[i].color));
     25
    2426    XSetLineAttributes (graphic->display, graphic->gc, lweight, LineSolid, CapNotLast, JoinMiter);
    25    
    26     graph[0].axis[i].color = MAX (0, MIN (15, graph[0].axis[i].color));
    27 
    28     XSetForeground (graphic->display, graphic->gc, graphic->color[graph[0].axis[i].color]);
    29     DrawRotTextInit (graphic->display, graphic->window, graphic->gc, graphic->color[graph[0].axis[i].color], graphic->back);
    30 
    31     // fprintf (stderr, "axis: %d, color: %d, %ld, weight: %f\n", i, graph[0].axis[i].color, graphic->color[graph[0].axis[i].color], lweight);
     27    XSetForeground (graphic->display, graphic->gc, graphic->color[color]);
     28    DrawRotTextInit (graphic->display, graphic->window, graphic->gc, graphic->color[color], graphic->back);
    3229
    3330    if (graph[0].axis[i].isaxis) {
    3431      DrawLine (fx, fy, dfx, dfy);
    35       // fprintf (stderr, "%d : %d %d - %d %d\n", i, fx, fy, dfx, dfy);
    36       // if (i == 0) {
    37       //        int j;
    38       //        for (j = fx; j < fx + dfx; j+= 10) {
    39       //          DrawLine (j, fy-10, 0, 20);
    40       //        }
    41       // }
    4232    }
    4333   
    4434    if (graph[0].axis[i].areticks) {
    45       if (isnan(graph[0].axis[i].min) || isinf(graph[0].axis[i].min)) continue;
    46       if (isnan(graph[0].axis[i].max) || isinf(graph[0].axis[i].max)) continue;
    47 
    48       range = graph[0].axis[i].max - graph[0].axis[i].min;
    49       if (fabs(range) < MIN_RANGE) continue;
    50 
    51       if (fabs(range) < MIN_RANGE) {
    52         range = (range < 0) ? -MIN_RANGE : +MIN_RANGE;
     35      ticks = CreateAxisTicks (&graph[0].axis[i], &Nticks);
     36      for (j = 0; j < Nticks; j++) {
     37        DrawTick (graphic, &graph[0].axis[i], P, &ticks[j], i);
    5338      }
    54 
    55       AxisTickScale (&graph[0].axis[i], &major, &minor);
    56 
    57       first = minor*((int)(graph[0].axis[i].min/minor));
    58       if ((range > 0) && (first < graph[0].axis[i].min)) {
    59         first += minor;
    60       }
    61       if ((range < 0) && (first > graph[0].axis[i].min)) {
    62         first -= minor;
    63       }
    64       for (next = first; ((range > 0) && (next <= graph[0].axis[i].max)) || ((range < 0) && (next >= graph[0].axis[i].max));) {
    65         IsMajor = FALSE;
    66         IsMajor |= (fabs((int)(next/major) - (next/major)) < 0.5*(minor/major));
    67         IsMajor |= (fabs((int)((next + 0.5*minor)/major) - (next/major)) < 0.5*(minor/major));
    68         IsMajor |= (fabs ((int)((next - 0.5*minor)/major) - (next/major)) < 0.5*(minor/major));
    69         IsLabel = (IsMajor && graph[0].axis[i].islabel);
    70         DrawTick (fx, fy, dfx, dfy, P, graph[0].axis[i].min, graph[0].axis[i].max, next, IsLabel, IsMajor, i);
    71         if (range > 0)
    72           next += minor;
    73         else
    74           next -= minor;
    75       }
     39      FREE (ticks);
    7640    }
    7741  }
    78 }
    79 
    80 void DrawTick (int fx, int fy, int dfx, int dfy,
    81                int P, double min, double max,
    82                double value, int IsLabel, int IsMajor, int naxis) {
    83  
    84   int x, y, dx, dy, pos, dir, fontsize;
    85   double size, n;
     42  return (TRUE);
     43}
     44
     45void DrawTick (Graphic *graphic, Axis *axis, int P, TickMarkData *tick, int naxis) {
     46 
     47  double x, y, dx, dy;
     48  int pos, dir, fontsize;
     49  double size, n, pad;
    8650  char string[64], *fontname;
    8751
    88   if (IsMajor) {
     52  double fx  = axis->fx;
     53  double fy  = axis->fy;
     54  double dfx = axis->dfx;
     55  double dfy = axis->dfy;
     56
     57  double min = axis->min;
     58  double max = axis->max;
     59
     60  double value = tick->value;
     61
     62  pos = size = 0;
     63
     64  if (tick->IsMajor) {
    8965    size = MAX (0.02, 7.0 / P);
    9066  } else {
     
    9268  }
    9369 
    94   n = P / sqrt ((double)(dfx*dfx + dfy*dfy));
     70  n = P / hypot(dfx, dfy);
    9571  x = fx + (value-min)*dfx/(max - min);
    9672  y = fy + (value-min)*dfy/(max - min);
    9773
    98   dir = +1;
    99   if ((naxis == 0) || (naxis == 1)) dir *= -1;
     74  if ((naxis == 0) || (naxis == 2)) {
     75    x = MAX(x, fx);
     76  }
     77  //   y = MAX(y, fy);
     78  // }
     79
     80  dir = ((naxis == 0) || (naxis == 1)) ? -1 : +1;
    10081  dx = dir*size*dfy*n; 
    10182  dy = dir*size*dfx*n;
     
    10384  DrawLine (x, y, dx, dy);
    10485
    105 # ifdef IN_AND_OUT_TICKS
    106 # define GAP 0.03
    107 # else
    108 # define GAP 0.01
    109 # endif
    110 
    111   if (IsLabel) {
     86  if (tick->IsLabel) {
     87    int xt, yt;
     88
    11289    fontname = GetRotFont (&fontsize);
    113     pos = 0;
     90   
     91    pad = !isnan(axis->ticktextPad) ? axis->ticktextPad*fontsize : fontsize + 4.0;
     92    // pad = !isnan(axis->ticktextPad) ? axis->ticktextPad*fontsize : 0.8*fontsize + 1.0;
    11493   
    11594    /* temporarily assume rectilinear axes */
    116     if (naxis == 0) { dx = 0; dy = -dir*(0.8*fontsize + 1); pos = 1; }
    117     if (naxis == 2) { dx = 0; dy = -dir*(0.8*fontsize + 1); pos = 7; }
    118 
    119     if (naxis == 1) { dy = 0; dx = -(0.8*fontsize + 1); pos = 3; }
    120     if (naxis == 3) { dy = 0; dx = +(0.8*fontsize + 1); pos = 5; }
    121 
    122     x = fx + (value-min)*dfx/(max - min) + dx;
    123     y = fy + (value-min)*dfy/(max - min) + dy;
     95    if (naxis == 0) { dx = 0; dy = +1.0*pad; pos = 7; }
     96    if (naxis == 2) { dx = 0; dy = -1.0*pad; pos = 1; }
     97
     98    if (naxis == 1) { dy = 0; dx = -0.5*pad; pos = 3; }
     99    if (naxis == 3) { dy = 0; dx = +0.5*pad; pos = 5; }
     100
     101    xt = fx + (value-min)*dfx/(max - min) + dx;
     102    yt = fy + (value-min)*dfy/(max - min) + dy;
    124103    if (fabs(value/(max-min)) < 0.001) { value = 0.0; }
    125104    sprintf (string, "%4g", value);
    126     DrawRotText (x, y, string, pos, 0.0);
    127   }
    128  
    129 }
    130 
     105    DrawRotText (xt, yt, string, pos, 0.0);
     106  }
     107}
     108
     109# define MIN_RANGE 1e-10
    131110
    132111void AxisTickScale (Axis *axis, double *major, double *minor) {
     
    192171  }
    193172}
     173
     174TickMarkData *CreateAxisTicks (Axis *axis, int *nticks) {
     175
     176  TickMarkData *ticks;
     177  double range, major, minor, first, value, dPixels, overshoot;
     178  int i, NTICKS, nPixels, done, ifirst;
     179
     180  *nticks = 0;
     181
     182  if (isnan(axis->min) || isinf(axis->min)) return NULL;
     183  if (isnan(axis->max) || isinf(axis->max)) return NULL;
     184 
     185  range = axis->max - axis->min;
     186
     187  // not sure why I skip out here instead of using the MIN_RANGE value
     188  if (fabs(range) < MIN_RANGE) return NULL;
     189 
     190  if (fabs(range) < MIN_RANGE) {
     191    range = (range < 0) ? -MIN_RANGE : +MIN_RANGE;
     192  }
     193
     194  // length of the axis in pixels
     195  nPixels = hypot(axis->dfx, axis->dfy);
     196  dPixels = nPixels / range; // axis pixel-scale
     197
     198  AxisTickScale (axis, &major, &minor);
     199
     200  // be a little generous
     201  NTICKS = (int)(fabs(range / minor)) + 2;
     202  ALLOCATE (ticks, TickMarkData, NTICKS);
     203
     204  // value of the tick mark
     205  ifirst = (axis->min < 0.0) ? (axis->min/minor - 0.05) : (axis->min/minor + 0.05);
     206  first = minor*ifirst;
     207 
     208  // allow undershoot by 1 pixel
     209  overshoot = (axis->min - first) * dPixels;
     210  // if (overshoot > 0.5) {
     211  //   if (range > 0) {
     212  //     first += minor;
     213  //   } else {
     214  //     first -= minor;
     215  //   }
     216  // }
     217
     218  // loop to find the ticks
     219  done = FALSE;
     220  value = first;
     221  for (i = 0; !done && (i < NTICKS); i++) {
     222    ticks[i].IsMajor = FALSE;
     223    ticks[i].IsMajor |= (fabs((int)(value/major) - (value/major)) < 0.5*(minor/major));
     224    ticks[i].IsMajor |= (fabs((int)((value + 0.5*minor)/major) - (value/major)) < 0.5*(minor/major));
     225    ticks[i].IsMajor |= (fabs((int)((value - 0.5*minor)/major) - (value/major)) < 0.5*(minor/major));
     226    ticks[i].IsLabel = (ticks[i].IsMajor && axis->islabel);
     227    ticks[i].value = value;
     228    if (range > 0)
     229      value += minor;
     230    else
     231      value -= minor;
     232   
     233    done |= (range > 0) && (value > axis->max);
     234    done |= (range < 0) && (value < axis->max);
     235  }
     236
     237  *nticks = i;
     238  return (ticks);
     239}
Note: See TracChangeset for help on using the changeset viewer.