Class: Sketch
- Inherits:
-
Object
- Object
- Sketch
- Defined in:
- lib/sketch.rb,
lib/sketch/dsl.rb,
lib/sketch/group.rb,
lib/sketch/point.rb,
lib/sketch/layout.rb,
lib/sketch/builder.rb,
lib/sketch/builder/path.rb,
lib/sketch/builder/polygon.rb,
lib/sketch/builder/polyline.rb
Overview
Direct Known Subclasses
Defined Under Namespace
Modules: DSL Classes: Builder, Group, Layout, Point
Constant Summary collapse
- Arc =
Geometry::Arc
- Circle =
Geometry::Circle
- Line =
Geometry::Line
- Rectangle =
Geometry::Rectangle
- Square =
Geometry::Square
- Path =
Geometry::Path
- Polygon =
Geometry::Polygon
- Polyline =
Geometry::Polyline
Accessors collapse
- #bounds ⇒ Object readonly
- #first ⇒ Object readonly
- #geometry ⇒ Object readonly
- #last ⇒ Object readonly
- #max ⇒ Point readonly
- #min ⇒ Point readonly
- #minmax ⇒ Array<Point> readonly
- #size ⇒ Size readonly
Instance Attribute Summary collapse
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
-
#transformation ⇒ Object
Returns the value of attribute transformation.
Geometry creation collapse
-
#add_arc(*args) ⇒ Arc
Create and append a new Arc object.
-
#add_circle(*args) ⇒ Circle
Create and append a new Circle object given a center point and radius.
-
#add_line(*args) ⇒ Object
Create a Line using any arguments that work for Geometry::Line.
-
#add_point(*args) ⇒ Object
Create a Point with any arguments that work for Geometry::Point.
-
#add_rectangle(*args) ⇒ Object
Create a Rectangle.
-
#add_square(length) ⇒ Object
Create a Square with sides of the given length.
-
#add_triangle(*args) ⇒ Object
Create and add a Triangle.
Class Method Summary collapse
-
.define_parameter(name, &block) ⇒ Object
Define a class parameter.
Instance Method Summary collapse
-
#define_parameter(name, &block) ⇒ Object
Define an instance parameter.
-
#initialize(&block) ⇒ Sketch
constructor
A new instance of Sketch.
- #push(element, *args) ⇒ Sketch
Constructor Details
#initialize(&block) ⇒ Sketch
Returns a new instance of Sketch.
19 20 21 22 |
# File 'lib/sketch.rb', line 19 def initialize(&block) @elements = [] instance_eval(&block) if block_given? end |
Instance Attribute Details
#bounds ⇒ Object (readonly)
47 48 49 |
# File 'lib/sketch.rb', line 47 def bounds Rectangle.new(*minmax) end |
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
10 11 12 |
# File 'lib/sketch.rb', line 10 def elements @elements end |
#first ⇒ Object (readonly)
53 54 55 |
# File 'lib/sketch.rb', line 53 def first elements.first end |
#geometry ⇒ Object (readonly)
59 60 61 |
# File 'lib/sketch.rb', line 59 def geometry @elements end |
#last ⇒ Object (readonly)
65 66 67 |
# File 'lib/sketch.rb', line 65 def last elements.last end |
#minmax ⇒ Array<Point> (readonly)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sketch.rb', line 83 def minmax return [nil, nil] unless @elements.size != 0 memo = @elements.map {|e| e.minmax }.reduce {|memo, e| [Point[[memo.first.x, e.first.x].min, [memo.first.y, e.first.y].min], Point[[memo.last.x, e.last.x].max, [memo.last.y, e.last.y].max]] } if self.transformation if self.transformation.has_rotation? # If the transformation has a rotation, convert the minmax into a bounding rectangle, rotate it, then find the new minmax point1, point3 = Point[memo.last.x, memo.first.y], Point[memo.first.x, memo.last.y] points = [memo.first, point1, memo.last, point3].map {|point| self.transformation.transform(point) } points.reduce([points[0], points[2]]) {|memo, e| [Point[[memo.first.x, e.x].min, [memo.first.y, e.y].min], Point[[memo.last.x, e.x].max, [memo.last.y, e.y].max]] } else memo.map {|point| self.transformation.transform(point) } end else memo end end |
#size ⇒ Size (readonly)
103 104 105 |
# File 'lib/sketch.rb', line 103 def size Size[self.minmax.reverse.reduce(:-).to_a] end |
#transformation ⇒ Object
Returns the value of attribute transformation.
11 12 13 |
# File 'lib/sketch.rb', line 11 def transformation @transformation end |
Class Method Details
.define_parameter(name, &block) ⇒ Object
Define a class parameter
27 28 29 30 31 32 |
# File 'lib/sketch.rb', line 27 def self.define_parameter name, &block define_method name do @parameters ||= {} @parameters.fetch(name) { |k| @parameters[k] = instance_eval(&block) } end end |
Instance Method Details
#add_arc(*args) ⇒ Arc
Create and append a new Arc object
130 131 132 |
# File 'lib/sketch.rb', line 130 def add_arc(*args) @elements.push(Arc.new(*args)).last end |
#add_circle(*args) ⇒ Circle
Create and append a new Circle object given a center point and radius
138 139 140 141 |
# File 'lib/sketch.rb', line 138 def add_circle(*args) @elements.push Circle.new(*args) @elements.last end |
#add_line(*args) ⇒ Object
Create a Line using any arguments that work for Geometry::Line
144 145 146 147 |
# File 'lib/sketch.rb', line 144 def add_line(*args) @elements.push Line[*args] @elements.last end |
#add_point(*args) ⇒ Object
Create a Point with any arguments that work for Geometry::Point
150 151 152 153 |
# File 'lib/sketch.rb', line 150 def add_point(*args) @elements.push Point[*args] @elements.last end |
#add_rectangle(*args) ⇒ Object
Create a Rectangle
156 157 158 159 |
# File 'lib/sketch.rb', line 156 def add_rectangle(*args) @elements.push Rectangle.new(*args) @elements.last end |
#add_square(length) ⇒ Object
Create a Square with sides of the given length
163 164 165 166 |
# File 'lib/sketch.rb', line 163 def add_square(length) push Geometry::CenteredSquare.new [0,0], length @elements.last end |
#add_triangle(*args) ⇒ Object
Create and add a Triangle
170 171 172 |
# File 'lib/sketch.rb', line 170 def add_triangle(*args) push Geometry::Triangle.new *args end |
#define_parameter(name, &block) ⇒ Object
Define an instance parameter
37 38 39 40 41 42 |
# File 'lib/sketch.rb', line 37 def define_parameter name, &block singleton_class.send :define_method, name do @parameters ||= {} @parameters.fetch(name) { |k| @parameters[k] = instance_eval(&block) } end end |
#push(element, *args) ⇒ Sketch
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sketch.rb', line 113 def push(element, *args) , args = args.partition {|a| a.is_a? Hash} = .reduce({}, :merge) if and (.size != 0) and (element.respond_to? :transformation) element.transformation = Geometry::Transformation.new end @elements.push(element) self end |