Class: CGRect

Inherits:
Object
  • Object
show all
Defined in:
lib/project/cg_rect.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.golden_section(options) ⇒ CGRect

Returns the new object.

Returns:



9
10
11
# File 'lib/project/cg_rect.rb', line 9

def self.golden_section(options)
Golden_Sections::handler(Golden_Sections::CGRect_relations, options, &Golden_Sections::CGRect_proc)
end

Instance Method Details

#golden_section(direction = :both, exp = 1) ⇒ CGRect

direction [Symbol] :width, :height, or :both, the dfeault

Parameters:

  • exp (Integer) (defaults to: 1)

    the exponent of the golden mean, deafults to 1

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/project/cg_rect.rb', line 18

def golden_section(direction=:both,  exp=1)
rect=self.dup
fixed=false
if direction==:width||direction==:both
rect.width=rect.width.golden_section(exp)
fixed=true
end
if direction==:height||direction==:both
rect.height=rect.height.golden_section(exp)
fixed=true
end
  raise "CGRect#golden_section: unknown direction: #{direction}" unless fixed
rect
end

#golden_split(direction, greater = :first) ⇒ Array

Splits a rectangle into two, with the proportion of the golden section

Parameters:

  • direction (Symbol)

    :width or :height, this controls how to split the rectangle. Imagine folding a piece of paper along its width or length.

  • greater (Symbol) (defaults to: :first)

    controls which rectangle has the greater area, :first or :last. Defaults to :first.

Returns:

  • (Array)

    an array of the two CGRect objects



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/project/cg_rect.rb', line 37

def golden_split(direction, greater=:first)
  raise "Golden_Section#golden_split: unknown direction #{direction}" unless [:width, :height].member?(direction)
  raise "Golden_Section.golden_split: Unknown greater section #{greater}" unless [:first, :last].member?(greater)
if greater==:first
exp=1
else
exp=2
end
rect1=self.golden_section(direction,exp)
if greater==:first
exp=1
else
exp=-1
end
if direction==:width
rect2=rect1.beside.width(rect1.width.golden_section(exp))
else
rect2=rect1.below.height(rect1.height.golden_section(exp))
end
[rect1,rect2]
end