~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/graphics/elements/tangent.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# tangent.rb: code for drawing tangents
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
# General Public License for more details (in the COPYING file).
 
13
 
 
14
 
 
15
require 'ctioga2/graphics/elements/primitive'
 
16
 
 
17
# This module contains all the classes used by ctioga
 
18
module CTioga2
 
19
 
 
20
  Version::register_svn_info('$Revision: 171 $', '$Date: 2010-10-22 17:07:52 +0200 (Fri, 22 Oct 2010) $')
 
21
 
 
22
  module Graphics
 
23
 
 
24
    module Elements
 
25
 
 
26
      module Tangents
 
27
 
 
28
        TangentOptions = {
 
29
          'xfrom'   => 'float',
 
30
          'xto'     => 'float',
 
31
          'yfrom'   => 'float',
 
32
          'yto'     => 'float',
 
33
          'xextent' => 'float',
 
34
          'yextent' => 'float',
 
35
          'nbavg'  =>  'integer'
 
36
        }.update(TiogaPrimitiveCall::ArrowOptions)
 
37
      
 
38
        TiogaPrimitiveCall.
 
39
          primitive("tangent", "tangent", [ 'data-point'],
 
40
                    TangentOptions) do |t, point,options|
 
41
          options ||= {}
 
42
          nb = options['nbavg'] || 7
 
43
          x = point.x_val(nb)
 
44
          y = point.y_val(nb)
 
45
          slope = point.slope(nb)
 
46
 
 
47
          # Now, we parse the head/tail spec.
 
48
          if d = options['xextent']
 
49
            options['tail'] = [x, y]
 
50
            options['head'] = [x+d, y + d*slope]
 
51
          elsif d = options['yextent']
 
52
            options['tail'] = [x, y]
 
53
            options['head'] = [x+d/slope, y + d]
 
54
          elsif options['xfrom'] || options['yfrom'] || 
 
55
              options['xto'] || options['yto']
 
56
            if xf = options['xfrom']
 
57
              options['tail'] = [xf, y - (x - xf)*slope]
 
58
            elsif yf = options['yfrom']
 
59
              options['tail'] = [x - (y-yf)/slope, yf]
 
60
            else
 
61
              options['tail'] = [x,y]
 
62
            end
 
63
 
 
64
            if xt = options['xto']
 
65
              options['head'] = [xt, y - (x - xt)*slope]
 
66
            elsif yt = options['yto']
 
67
              options['head'] = [x - (y-yt)/slope, yt]
 
68
            else
 
69
              options['head'] = [x,y]
 
70
            end
 
71
          else
 
72
            # We don't bother too much about the head/tail
 
73
            options['head'] = [x, y]
 
74
            dx = point.dx(nb) * 10
 
75
            options['tail'] = [x-dx, y - dx*slope]
 
76
            options['line_width'] = 0
 
77
            options['tail_marker'] = "None"
 
78
          end
 
79
 
 
80
          # We look for any color argument:
 
81
          if ! (options['color'] || options['tail_color'] || 
 
82
                options['head_color'])
 
83
            options['color'] = $last_curve_style.line.color
 
84
          end
 
85
          
 
86
          # Now, we delete elements from the hash that don't have
 
87
          # anything to do there:
 
88
          for k in TangentOptions.keys - 
 
89
              TiogaPrimitiveCall::ArrowOptions.keys
 
90
            options.delete k
 
91
          end
 
92
          
 
93
          t.show_arrow(options)
 
94
        end
 
95
      end
 
96
      
 
97
    end
 
98
  end
 
99
end