~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/xplt/src/gist/tick60.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * TICK60.C
 
3
 *
 
4
 * $Id: tick60.c,v 1.1 2003/03/08 15:26:45 travo Exp $
 
5
 *
 
6
 * Implement base 60 ticks and labels for coordinate systems.
 
7
 *
 
8
 */
 
9
/*    Copyright (c) 1996.  The Regents of the University of California.
 
10
                    All rights reserved.  */
 
11
 
 
12
#include "gist.h"
 
13
 
 
14
/* Use sprintf function in labeling functions */
 
15
#include <stdio.h>
 
16
 
 
17
extern double ceil(double);
 
18
 
 
19
/* Here are the possible intervals for a base 60 scale.
 
20
   The 720 interval is skipped if the 1800 was present, otherwise all
 
21
   these intervals will be present in the tick hierarchy.  */
 
22
#define N_CUTS 7
 
23
static GpReal cutoffs[N_CUTS]= { 1800., 720., 360., 180., 90., 30., 10. };
 
24
 
 
25
int Base60Ticks(GpReal lo, GpReal hi, GpReal nMajor, GpReal nMinor,
 
26
                GpReal *ticks, int nlevel[TICK_LEVELS])
 
27
     /* Puts ticks at multiples of 30, failing if lo<-3600 or hi>+3600.
 
28
        For ticks at multiples of 10 or less, the subdivisions are
 
29
        identical to the default decimal tick scheme.  */
 
30
{
 
31
  GpReal finest= (hi-lo)/nMajor;
 
32
  GpReal delta= cutoffs[0];
 
33
  GpReal tick0;
 
34
  int i0, ntot, nlevs, phase, base, ndivs;
 
35
 
 
36
  if (lo<-3600. || hi>3600. ||
 
37
      finest<=cutoffs[N_CUTS-1] || finest>delta) return 1;
 
38
 
 
39
  for (i0=0 ; i0<N_CUTS && finest<=cutoffs[i0] ; i0++) delta= cutoffs[i0];
 
40
  tick0= ceil(lo/delta)*delta;
 
41
  for (ntot=0 ; tick0<=hi ; ntot++,tick0+=delta) ticks[ntot]= tick0;
 
42
  nlevel[0]= ntot;
 
43
  nlevs= 1;
 
44
 
 
45
  finest= (hi-lo)/nMinor;
 
46
 
 
47
  /* perform base 60 part of subdivision */
 
48
  for (; i0<N_CUTS && finest<=cutoffs[i0] ; i0++) {
 
49
    if (i0==1) {
 
50
      i0++;                 /* skip 720 starting from 1800 */
 
51
      if (finest>cutoffs[i0]) break;
 
52
      ndivs= 5;
 
53
    } else {
 
54
      ndivs= i0<5? 2 : 3;   /* 90->30 and 30->10 are 3, others all 2 */
 
55
    }
 
56
    delta= cutoffs[i0];
 
57
    tick0= ceil(lo/delta);
 
58
    phase= (int)(tick0-ceil(tick0/ndivs-.00001)*ndivs);
 
59
    tick0*= delta;
 
60
    for ( ; tick0<=hi ; tick0+=delta,phase=(phase+1)%ndivs)
 
61
      if (phase) ticks[ntot++]= tick0;
 
62
    nlevel[nlevs]= ntot;
 
63
    if (++nlevs>=TICK_LEVELS) return 0;
 
64
  }
 
65
  if (i0<N_CUTS || finest>5.) return 0;
 
66
 
 
67
  /* perform base 10 part of subdivision if necessary */
 
68
  delta= 5.;
 
69
  base= 5;
 
70
  ndivs= 2;
 
71
  for (; nlevs<TICK_LEVELS ; nlevs++) {
 
72
    tick0= ceil(lo/delta);
 
73
    phase= (int)(tick0-ceil(tick0/ndivs-.00001)*ndivs);
 
74
    tick0*= delta;
 
75
    for ( ; tick0<=hi ; tick0+=delta,phase=(phase+1)%ndivs)
 
76
      if (phase) ticks[ntot++]= tick0;
 
77
    nlevel[nlevs]= ntot;
 
78
    if (base==2) break;
 
79
    if (base==5) {
 
80
      delta*= 0.2;
 
81
      base= 1;
 
82
      ndivs= 5;
 
83
    } else if (finest<=0.1*delta) {
 
84
      delta*= 0.5;
 
85
      base= 5;
 
86
      ndivs= 2;
 
87
      continue;
 
88
    } else {
 
89
      delta*= 0.2;
 
90
      base= 2;
 
91
      ndivs= 5;
 
92
    }
 
93
    if (finest>delta) break;
 
94
  }
 
95
  return 0;
 
96
}
 
97
 
 
98
int DegreeLabels(char *label, GpReal value)
 
99
     /* Prints (value+180)%360-180 instead of just value.  */
 
100
{
 
101
  GpReal dv;
 
102
  int val;
 
103
  if (value<-3600. || value>3600.) return 1;
 
104
  dv= ceil(value-0.00001);
 
105
  val= (int)dv;
 
106
  if (dv<value) dv= value-dv;
 
107
  else dv= dv-value;
 
108
  if (dv>0.00001) return 1;
 
109
  if (!label) return 0;
 
110
  val= (val+180)%360;
 
111
  if (val<=0) val+= 360;
 
112
  sprintf(label, "%d", val-180);
 
113
  return 0;
 
114
}
 
115
 
 
116
int HourLabels(char *label, GpReal value)
 
117
     /* Prints hh:mm (or mm:ss) for value 60*hh+mm (or 60*mm+ss).  */
 
118
{
 
119
  GpReal dv;
 
120
  int hh, mm, neg;
 
121
  if (value<-3600. || value>3600.) return 1;
 
122
  dv= ceil(value-0.00001);
 
123
  neg= dv<0.;
 
124
  if (neg) hh= (int)(-dv);
 
125
  else hh= (int)dv;
 
126
  if (dv<value) dv= value-dv;
 
127
  else dv= dv-value;
 
128
  if (dv>0.00001) return 1;
 
129
  if (!label) return 0;
 
130
  mm= hh%60;
 
131
  hh/= 60;
 
132
  sprintf(label, "%s%02d:%02d", (neg?"-":""), hh, mm);
 
133
  return 0;
 
134
}