Module: Geometry::DSL::Polyline
- Included in:
- Sketch::Builder::Polyline
- Defined in:
- lib/geometry/dsl/polyline.rb
Overview
When you want to draw things that are made of lots and lots of lines, this is how you do it.
Requirements
This module is intended to be included into a Class, and that Class must provide some infrastructure. It must provide a push method for pushing new elements, a first method that returns the first vertex in the Polyline, and a last method that returns the last vertex.
Usage
begin
start_at [0,0] move_y 1 # Go up 1 unit right 1 down 1 left 1 # Close the box close # Unnecessary in this case
end
Constant Summary collapse
- BuildError =
Class.new(StandardError)
Relative Movement collapse
-
#down(distance) ⇒ Object
Move the specified distance along the -Y axis.
-
#horizontal_to(x) ⇒ Object
(also: #left_to, #right_to)
Draw a horizontal line to the given x-coordinate while preserving the y-coordinate of the previous Point.
-
#left(distance) ⇒ Object
Move the specified distance along the -X axis.
-
#right(distance) ⇒ Object
Move the specified distance along the +X axis.
-
#up(distance) ⇒ Object
Move the specified distance along the +Y axis.
-
#vertical_to(y) ⇒ Object
(also: #down_to, #up_to)
Draw a vertical line to the given y-coordinate while preserving the x-coordinate of the previous Point.
Instance Method Summary collapse
-
#close ⇒ Object
Close the Polyline with a Line, if it isn’t already closed.
-
#closed? ⇒ Bool
True if the Polyline is closed, otherwise false.
-
#move_to(point) ⇒ Object
Draw a line to the given Point.
-
#move_x(distance) ⇒ Object
Move the specified distance along the X axis.
-
#move_y(distance) ⇒ Object
Move the specified distance along the Y axis.
-
#start_at(point) ⇒ Object
Specify a starting point.
Instance Method Details
#close ⇒ Object
Close the Geometry::DSL::Polyline with a Line, if it isn’t already closed
26 27 28 |
# File 'lib/geometry/dsl/polyline.rb', line 26 def close move_to(first) unless closed? end |
#closed? ⇒ Bool
Returns True if the Geometry::DSL::Polyline is closed, otherwise false.
31 32 33 |
# File 'lib/geometry/dsl/polyline.rb', line 31 def closed? first == last end |
#down(distance) ⇒ Object
Move the specified distance along the -Y axis
70 71 72 |
# File 'lib/geometry/dsl/polyline.rb', line 70 def down(distance) move_y -distance end |
#horizontal_to(x) ⇒ Object Also known as: left_to, right_to
Draw a horizontal line to the given x-coordinate while preserving the y-coordinate of the previous Point
98 99 100 |
# File 'lib/geometry/dsl/polyline.rb', line 98 def horizontal_to(x) push [x, last.y] end |
#left(distance) ⇒ Object
Move the specified distance along the -X axis
76 77 78 |
# File 'lib/geometry/dsl/polyline.rb', line 76 def left(distance) move_x -distance end |
#move_to(point) ⇒ Object
Draw a line to the given Point
37 38 39 |
# File 'lib/geometry/dsl/polyline.rb', line 37 def move_to(point) push Point[point] end |
#move_x(distance) ⇒ Object
Move the specified distance along the X axis
43 44 45 |
# File 'lib/geometry/dsl/polyline.rb', line 43 def move_x(distance) push (last || PointZero) + Point[distance, 0] end |
#move_y(distance) ⇒ Object
Move the specified distance along the Y axis
49 50 51 |
# File 'lib/geometry/dsl/polyline.rb', line 49 def move_y(distance) push (last || PointZero) + Point[0, distance] end |
#right(distance) ⇒ Object
Move the specified distance along the +X axis
82 83 84 |
# File 'lib/geometry/dsl/polyline.rb', line 82 def right(distance) move_x distance end |
#start_at(point) ⇒ Object
Specify a starting point. Can’t be specified twice, and only required if no other entities have been added. #param [Point] point The starting point
55 56 57 58 |
# File 'lib/geometry/dsl/polyline.rb', line 55 def start_at(point) raise BuildError, "Can't specify a start point more than once" if first push Point[point] end |
#up(distance) ⇒ Object
Move the specified distance along the +Y axis
64 65 66 |
# File 'lib/geometry/dsl/polyline.rb', line 64 def up(distance) move_y distance end |
#vertical_to(y) ⇒ Object Also known as: down_to, up_to
Draw a vertical line to the given y-coordinate while preserving the x-coordinate of the previous Point
89 90 91 |
# File 'lib/geometry/dsl/polyline.rb', line 89 def vertical_to(y) push Point[last.x, y] end |