Class: Ranicoma::Rect

Inherits:
Struct
  • Object
show all
Defined in:
lib/ranicoma/rect.rb

Overview

矩形

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hObject

Returns the value of attribute h

Returns:

  • (Object)

    the current value of h



6
7
8
# File 'lib/ranicoma/rect.rb', line 6

def h
  @h
end

#wObject

Returns the value of attribute w

Returns:

  • (Object)

    the current value of w



6
7
8
# File 'lib/ranicoma/rect.rb', line 6

def w
  @w
end

#xObject

Returns the value of attribute x

Returns:

  • (Object)

    the current value of x



6
7
8
# File 'lib/ranicoma/rect.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y

Returns:

  • (Object)

    the current value of y



6
7
8
# File 'lib/ranicoma/rect.rb', line 6

def y
  @y
end

Instance Method Details

#bottomObject

下端の座標



75
76
77
# File 'lib/ranicoma/rect.rb', line 75

def bottom
  y+h
end

#centerObject

中心の座標。x, y の順



49
50
51
# File 'lib/ranicoma/rect.rb', line 49

def center
  [cx, cy]
end

#cxObject

中心の x 座標



54
55
56
# File 'lib/ranicoma/rect.rb', line 54

def cx
  x+w/2.0
end

#cyObject

中心の y 座標



59
60
61
# File 'lib/ranicoma/rect.rb', line 59

def cy
  y+h/2.0
end

#hsplit(*rel_ws) ⇒ Object

水平(左右)に分割する

rel_ws

相対的な幅のリスト



23
24
25
26
27
28
29
30
31
32
# File 'lib/ranicoma/rect.rb', line 23

def hsplit( *rel_ws )
  rel_sum = rel_ws.sum
  abs_x = x.to_f
  rel_ws.map{ |rel_w|
    abs_w = rel_w.to_f * w / rel_sum
    rc = Rect.new( abs_x, y, abs_w, h )
    abs_x += abs_w
    rc
  }
end

#reduce(ratio) ⇒ Object

自分を縮小した矩形を返す。中心を維持して、上下左右から同じ長さを減じる。

ratio

縮小する割合。0.1 とかを想定。



36
37
38
39
# File 'lib/ranicoma/rect.rb', line 36

def reduce( ratio )
  len = [w,h].min*ratio/2
  Rect.new( x+len, y+len, w-len*2, h-len*2 )
end

#reduce_h(ratio) ⇒ Object

自分を左右方向に縮小した矩形を返す。中心を維持して、左右から同じ長さを減じる。

ratio

縮小する割合。0.1 とかを想定。



43
44
45
46
# File 'lib/ranicoma/rect.rb', line 43

def reduce_h( ratio )
  len = w*ratio/2
  Rect.new( x+len, y, w-len*2, h )
end

#rightObject

右端の座標



70
71
72
# File 'lib/ranicoma/rect.rb', line 70

def right
  x+w
end

#rotObject

自分を90度回した矩形。回転の中心は矩形の中心。



64
65
66
67
# File 'lib/ranicoma/rect.rb', line 64

def rot
  cx, cy = center
  Rect.new( cx-h/2, cy-w/2, h, w )
end

#vsplit(*rel_hs) ⇒ Object

垂直(上下)に分割する

rel_hs

相対的な高さのリスト



10
11
12
13
14
15
16
17
18
19
# File 'lib/ranicoma/rect.rb', line 10

def vsplit( *rel_hs )
  rel_sum = rel_hs.sum
  abs_y = y.to_f
  rel_hs.map{ |rel_h|
    abs_h = rel_h.to_f * h / rel_sum
    rc = Rect.new( x, abs_y, w, abs_h )
    abs_y += abs_h
    rc
  }
end