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

Instance Method Summary collapse

Instance Method Details

#closeObject

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.

Returns:



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

Parameters:

  • distance (Number)

    The distance to move in the -Y direction



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

Parameters:

  • x (Number)

    the x-coordinate to move to



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

Parameters:

  • distance (Number)

    The distance to move in the -X direction



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

Parameters:

  • The (Point)

    Point to draw a line to



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

Parameters:

  • distance (Number)

    The distance to move



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

Parameters:

  • distance (Number)

    The distance to move



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

Parameters:

  • distance (Number)

    The distance to move in the +X direction



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

Raises:



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

Parameters:

  • distance (Number)

    The distance to move in the +Y direction



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

Parameters:

  • y (Number)

    the y-coordinate to move to



89
90
91
# File 'lib/geometry/dsl/polyline.rb', line 89

def vertical_to(y)
		push Point[last.x, y]
end