class CTioga2::Graphics::Styles::SingleLineFillPattern

@

Attributes

angle[RW]

Angle of the lines

distance[RW]

Separation between the lines, a dimension

line_width[RW]

Line width (in line widths units ?)

Public Class Methods

new(an = 0,dst = nil, lw = nil) click to toggle source
# 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

Public Instance Methods

do(t, color, secondary = nil) click to toggle source
# 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