summaryrefslogtreecommitdiff
path: root/modules/graphics/macros/graduate.sci
blob: bdc26c56e2c340df3569859afc4a1bdd57310b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 1992-2010 - INRIA - Serge Steer
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution. The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function [xi,xa,np]=graduate( xmi, xma,n1,n2)
    // graduate - axis pretty graduations
    //%Syntax
    // [xa,xi,np]=graduate( xma, xmi,n1,n2)
    // [xa,xi,np]=graduate( xma, xmi)
    //%Parameters
    // xmi, xma : real scalars
    // n1 , n2  : integer scalars default values 3,10
    // xi , xa  : real scalars
    // np       :integer scalar
    //%Description
    // graduate looks for the mimimum interval [xi,xa] and a number of tics np
    // such that:
    //           xi <= xmi <= xma <= xa
    //           xa - xi / np  =  k(10**n)  k in [1 3 5]   for an integer n
    //           n1 <= np <= n2
    //%Exemple
    //  y=0:0.33:145.78
    //  clf();plot2d1('enn',0,y)
    //  [ymn,ymx,np]=graduate(min(y),max(y))
    //  rect=[1,ymn,prod(size(y)),ymx];
    //  clf();plot2d1('enn',0,y,-1,'011',' ',rect,[10,3,10,np])

    kadm=[1,2,5];nadm=prod(size(kadm))

    // test
    // ----
    //
    [lhs,rhs]=argn(0)
    if rhs <2 then
        error(msprintf(gettext("%s: Wrong number of input argument(s): At least %d expected.\n"), "graduate", 2));
    end
    if rhs <4 then
        n1=3
        n2=10
    end
    if  n1 == 0  &  n2 == 0  then
        k1 = 1
        k2 = 1
    else
        k1 = min ( abs(n1) , abs(n2) )
        k1 = max (    1    ,     k1  )
        k2 = max ( abs(n1) , abs(n2) )
    end
    if xma == xmi then
        if xma==0 then
            xma=0.1;xmi=-0.1
        else
            xma=xma+xmi/10
            xmi=xmi-xmi/10
        end
    end

    xx0 = max ( xma , xmi )
    xx1 = min ( xma , xmi )
    del=abs(xx1-xx0)
    if abs(xx0-xx1)<=1d-6*max(xx0,xx1) then
        xa = xma
        xi = xmi
        np=1
        return
    end

    //
    // boucle sur les pas possibles
    // ----------------------------
    //
    for npi = k1:k2

        //
        // recherche de l'intervalle [ x1 , x0 ] tel que :
        //           x1 < xmi < xma < x0
        //           x0 - x1 / npi  =  k.10**n   k = 1,.,9   n  entier
        //
        //
        // recherche du pas
        // ----------------
        // il est compris entre  10**ipa-1  et  10**ipa
        //
        if xx0*xx1<0 then
            pas=max(abs([xx0 xx1])/npi)
        else
            pas = (xx0-xx1)/npi
        end
        ipa = int(log(pas)/log(10))
        if pas<1 then ipa = ipa - 1,end

        pa2 = 10**ipa
        //
        ik=find(pas<=kadm*pa2)
        if ik==[] then
            pa2 = 10.0d+00 * pa2
            ipa = kadm(1)
            pa1=ipa*pa2
        else
            ipa=kadm(ik(1))
            pa1=ipa*pa2
        end
        while %t
            //
            // recherche des extremites
            // ------------------------
            //
            if xx1*xx0<0 then
                x1 = xx1/pa1
                np1=int(x1)
                x1=np1*pa1
                if x1>xx1 then x1=x1-pa1,end
            else
                x1 = xx1/pa2
                np1=int(x1)
                x1=np1*pa2
                if x1>xx1 then x1=x1-pa1,end
            end
            x0 = x1+npi*pa1


            //
            // test
            // ----
            //
            if x0<xx0 then
                ik=find(kadm==ipa)
                if ik<nadm then
                    ipa = kadm(ik+1)
                    pa1 = ipa * pa2
                else
                    ipa = kadm(1)
                    pa1 = 10.0d+00 * pa2
                    pa2 = pa1
                end
            else
                break
            end
        end
        if npi==k1 then
            xl=x0-x1
            xa=x0
            xi=x1
            np=k1
        else
            if  (x0-x1)< xl then
                np = npi
                xl = x0 - x1
                xa = x0
                xi = x1
            end
        end
    end
endfunction