Class: CTioga2::Graphics::Types::Rect

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/graphics/types/point.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tl, br) ⇒ Rect

Returns a new instance of Rect.



121
122
123
124
# File 'lib/ctioga2/graphics/types/point.rb', line 121

def initialize(tl, br)
  @tl = tl
  @br = br
end

Instance Attribute Details

#brObject

Returns the value of attribute br.



119
120
121
# File 'lib/ctioga2/graphics/types/point.rb', line 119

def br
  @br
end

#tlObject

Returns the value of attribute tl.



118
119
120
# File 'lib/ctioga2/graphics/types/point.rb', line 118

def tl
  @tl
end

Instance Method Details

#dimensions(t) ⇒ Object

Returns the [height, width] of the rectangle in postscript points



127
128
129
130
131
132
133
# File 'lib/ctioga2/graphics/types/point.rb', line 127

def dimensions(t)
  xl, yt = @tl.to_figure_xy(t)
  xr, yb = @br.to_figure_xy(t)

  return [t.convert_figure_to_output_dx(xr - xl) * t.scaling_factor,
          t.convert_figure_to_output_dy(yb - yt) * t.scaling_factor]
end

#make_corners(t, swap = false, ratio_pol = :ignore, ratio = nil) ⇒ Object

Returns an array of [ul, ll, lr] coordinates. If an aspect ratio is specified, the coordinates will be expanded or contracted to fit the aspect ratio (keeping the center identical).



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
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ctioga2/graphics/types/point.rb', line 139

def make_corners(t, swap = false, ratio_pol = :ignore, 
                 ratio = nil)
  
  ul = @tl.to_figure_xy(t)
  lr = @br.to_figure_xy(t)

  width, height = *dimensions(t)

  # First, swap the coords
  if swap
    if width < 0
      ul[0], lr[0] = lr[0], ul[0]
    end
    if height > 0
      ul[1], lr[1] = lr[1], ul[1]
    end
  end

  # Now, we take the aspect ratio into account
  if ratio && ratio_pol != :ignore

    xc = 0.5 * (ul[0] + lr[0])
    yc = 0.5 * (ul[1] + lr[1])
    dx = lr[0] - ul[0]
    dy = lr[1] - ul[1]

    fact = ((width/height) / ratio).abs

    what = nil

    if ratio_pol == :expand
      if fact > 1       # must increase height
        what = :y
      else
        fact = 1/fact
        what = :x
      end
    elsif ratio_pol == :contract
      if fact > 1       # must decrease width
        what = :x
        fact = 1/fact
      else
        what = :y
      end
    else
      raise "Unkown aspect ratio policy: #{ratio_pol}"
    end

    if what == :y
      lr[1] = yc + fact * 0.5 * dy
      ul[1] = yc - fact * 0.5 * dy
    else
      lr[0] = xc + fact * 0.5 * dx
      ul[0] = xc - fact * 0.5 * dx
    end
  end

  ll = [ul[0], lr[1]]

  return [ul, ll, lr]
end