Class: CTioga2::Graphics::Styles::SingleLineFillPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/graphics/styles/fill.rb

Overview

@

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(an = 0, dst = nil, lw = nil) ⇒ SingleLineFillPattern

Returns a new instance of SingleLineFillPattern.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ctioga2/graphics/styles/fill.rb', line 100

def initialize(an = 0,dst = nil, lw = nil)
  @distance = if dst
                if dst.is_a? Types::Dimension
                  dst
                else
                  Types::Dimension::from_text(dst, :x, :bp)
                end
              else
                Types::Dimension.new(:bp, 5)
              end
  @line_width = lw ? lw.to_f : 0.8
  @angle = an.to_f
end

Instance Attribute Details

#angleObject

Angle of the lines



98
99
100
# File 'lib/ctioga2/graphics/styles/fill.rb', line 98

def angle
  @angle
end

#distanceObject

Separation between the lines, a dimension



95
96
97
# File 'lib/ctioga2/graphics/styles/fill.rb', line 95

def distance
  @distance
end

#line_widthObject

Line width (in line widths units ?)



92
93
94
# File 'lib/ctioga2/graphics/styles/fill.rb', line 92

def line_width
  @line_width
end

Instance Method Details

#do(t, color, secondary = nil) ⇒ Object



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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ctioga2/graphics/styles/fill.rb', line 114

def do(t, color, secondary = nil)
  # Secondary is not used

  t.context do
    t.stroke_color = color
    t.line_width = @line_width
    # Make figure coordinates page coordinates
    t.set_bounds([t.convert_frame_to_page_x(0),
                  t.convert_frame_to_page_x(1),
                  t.convert_frame_to_page_y(1),
                  t.convert_frame_to_page_y(0)])

    # Now we can work
    dx = -@distance.to_figure(t, :x) * 
      Math.sin(Math::PI/180 * @angle)
    sx = @distance.to_figure(t, :x) * 
      Math.cos(Math::PI/180 * @angle)
    dy = @distance.to_figure(t, :y) * 
      Math.cos(Math::PI/180 * @angle)
    sy = @distance.to_figure(t, :y) * 
      Math.sin(Math::PI/180 * @angle)

    if dy < 0
      dy = -dy
      dx = -dx
    end

    if dx.abs < 1e-12          # Horizontal lines
      y = 0
      while y <= 1
        t.stroke_line(0, y, 1, y)
        y += dy
      end
    elsif dy.abs < 1e-12
      x = 0
      dx = dx.abs
      while x <= 1
        t.stroke_line(x, 0, x, 1)
        x += dx
      end
    else
      if dx > 0
        line = Line.new(0, 0, sx, sy)
      else
        line = Line.new(1, 0, sx, sy)
      end
      segs = [ Segment.new(0,0,1,0), Segment.new(1,0,1,1),
               Segment.new(1,1,0,1), Segment.new(0,1,0,0)]
      while true
        ints = []
        for s in segs
          v = s.intersection(line)
          ints << v if v 
        end
        if ints.size == 0
          break
        elsif ints.size == 2
          t.stroke_line(ints[0][0], ints[0][1],
                        ints[1][0], ints[1][1])
        elsif ints.size == 3
          # Rare case but must be handled anyway
          if ints[0][0] == ints[1][0]
            ints.shift
          end
          t.stroke_line(ints[0][0], ints[0][1],
                        ints[1][0], ints[1][1])
        end
        line.x += dx
        line.y += dy
      end
    end
  end
  
end