Class: Sketch

Inherits:
Object
  • Object
show all
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

Layout is a container that arranges its child elements in the specified direction and, optionally, with the specified spacing.

For example, to create two adjacent squares along the X-axis, use:

Layout.new :horizontal do
    square size:5
    square size:6
end

Direct Known Subclasses

Group

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

Instance Attribute Summary collapse

Geometry creation collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#boundsObject (readonly)



47
48
49
# File 'lib/sketch.rb', line 47

def bounds
	Rectangle.new(*minmax)
end

#elementsObject (readonly)

Returns the value of attribute elements.



10
11
12
# File 'lib/sketch.rb', line 10

def elements
  @elements
end

#firstObject (readonly)



53
54
55
# File 'lib/sketch.rb', line 53

def first
	elements.first
end

#geometryObject (readonly)



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

def geometry
	@elements
end

#lastObject (readonly)



65
66
67
# File 'lib/sketch.rb', line 65

def last
	elements.last
end

#maxPoint (readonly)

Returns:



71
72
73
# File 'lib/sketch.rb', line 71

def max
	minmax.last
end

#minPoint (readonly)

Returns:



77
78
79
# File 'lib/sketch.rb', line 77

def min
	minmax.first
end

#minmaxArray<Point> (readonly)

Returns:



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

#sizeSize (readonly)

Returns The size of the Rectangle that bounds all of the Sketch‘s elements.

Returns:

  • (Size)

    The size of the Rectangle that bounds all of the Sketch‘s elements



103
104
105
# File 'lib/sketch.rb', line 103

def size
	Size[self.minmax.reverse.reduce(:-).to_a]
end

#transformationObject

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

Parameters:

  • name (Symbol)

    The name of the parameter

  • block (Proc)

    A block that evaluates to the desired value of the 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

Returns:



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

Parameters:

  • center (Point)

    The circle’s center point

  • radius (Number)

    The circle’s radius

Returns:



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

Parameters:

  • length (Numeric)

    The length of the sides of the square



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

Parameters:

  • name (Symbol)

    The name of the parameter

  • block (Proc)

    A block that evaluates to the desired value of the 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

Append the given Geometry element and return the Sketch

Parameters:

  • element (Geometry)

    the Geometry element to append

  • args (Array)

    optional transformation parameters

Returns:



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sketch.rb', line 113

def push(element, *args)
	options, args = args.partition {|a| a.is_a? Hash}
	options = options.reduce({}, :merge)

	if options and (options.size != 0) and (element.respond_to? :transformation)
 element.transformation = Geometry::Transformation.new options
	end

	@elements.push(element)
	self
end