class EduDraw::Pen

A Pen is a drawing tool that provides basic drawing functionalities on a 2d {Sheet}.

Constants

PIXEL_TOLERANCE

Tolerance with which to consider two points the same

Attributes

angle[RW]

@return [Fixnum] Direction of the pen in degree where 0 points to the right

color[RW]

@return [Gosu::Color] Color of the pen

shapes[R]

Shapes to be drawn by sheet @private

x[RW]

@return [Fixnum] x-coordinate of position in pixel where left is 0

y[RW]

@return [Fixnum] y-coordinate of position in pixel where top is 0

Public Class Methods

new(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN) click to toggle source

Creates a new Pen

@param x [Fixnum] Initial value of {#x} @param y [Fixnum] Initial value of {#y} @param angle [Fixnum] Initial value of {#angle} @param color [Gosu::Color] Initial value of {#color}

# File lib/edu_draw/pen.rb, line 31
def initialize(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN)
        @x = x
        @y = y
        @angle = angle
        @color = color
        @shapes = []
        @to_be_filled = []

        down!
end

Public Instance Methods

angle_in_rad() click to toggle source

@return [Fixnum] angle in radians instead of degree

# File lib/edu_draw/pen.rb, line 98
def angle_in_rad
        angle * Math::PI / 180.0
end
down!() click to toggle source

Sticks the pen to the {Sheet}

@see up? @see down?

# File lib/edu_draw/pen.rb, line 81
def down!
        @up = false
end
down?() click to toggle source

If the pen is down, it touches the {Sheet} and is drawing when moved.

# File lib/edu_draw/pen.rb, line 65
def down?
        !up?
end
fill() { || ... } click to toggle source

Draws filled shapes.

@note If the pen does not end where it started, the

pen will assume a connection between start and end point.
This does not affect the actual position and direction
of the pen.

@example

  # This draws a triangle
  pen.fill do
          pen.move 10
          pen.turn_right 90
          pen.move 10
end
# File lib/edu_draw/pen.rb, line 116
def fill(&block)
        @to_be_filled << [x,y]
        yield
        if starting_point_is_end_point?
                @to_be_filled.delete_at(0)
        end
        origin_x, origin_y = @to_be_filled.first
        @to_be_filled[1..@to_be_filled.length-1].each_cons(2) do |point_a, point_b|
                @shapes << [:draw_triangle,
                        origin_x, origin_y, color,
                        point_a[0], point_a[1], color,
                        point_b[0], point_b[1], color]
        end
        @to_be_filled.clear
end
move(length) click to toggle source

Moves the stick an amount of pixel into its direction. Draws when it is {#down?}

@param length [Fixnum] Amount of pixels moved

# File lib/edu_draw/pen.rb, line 89
def move(length)
        target_x = x + Math.cos(angle_in_rad) * length
        target_y = y + Math.sin(angle_in_rad) * length
        handle_movement x, y, target_x, target_y
        @x = target_x
        @y = target_y
end
turn_left(degree) click to toggle source

Changes the direction of self to be turned to the left The {#angle} stays within 0..364

@param degree [Fixnum] Amount of degree to be turned

# File lib/edu_draw/pen.rb, line 46
def turn_left(degree)
        @angle -= degree
        @angle = (@angle + 360) % 360 # Make sure angle is always in 0..359
end
turn_right(degree) click to toggle source

Changes the direction of self to be turned to the right The {#angle} stays within 0..364

@param (see turn_left)

# File lib/edu_draw/pen.rb, line 55
def turn_right(degree)
        turn_left -degree
end
up!() click to toggle source

Lifts the pen from the {Sheet}

@see up? @see down?

# File lib/edu_draw/pen.rb, line 73
def up!
        @up = true
end
up?() click to toggle source

If the pen is up, it does not touch the {Sheet} and is not drawing. It can still be moved.

# File lib/edu_draw/pen.rb, line 60
def up?
        @up
end
update() click to toggle source

Hook method @private

# File lib/edu_draw/pen.rb, line 134
def update
        # nothing to do since nothing ever changes
end

Private Instance Methods

fill_mode?() click to toggle source

True, if pen is currently in a {#fill}-block

# File lib/edu_draw/pen.rb, line 142
def fill_mode?
        @to_be_filled.length > 0
end
handle_movement(x, y, target_x, target_y) click to toggle source

Does whatever needs to be done upon movement. This depends on the state of the pen. up or down? fill mode or regular mode?

# File lib/edu_draw/pen.rb, line 149
def handle_movement(x, y, target_x, target_y)
        if down?
                if fill_mode?
                        @to_be_filled << [target_x, target_y]
                else
                        @shapes << [:draw_line, x, y, color, target_x, target_y, color]
                end
        end
end
starting_point_is_end_point?() click to toggle source

Checks whether the pen came back to its starting point during the {#fill}-block.

# File lib/edu_draw/pen.rb, line 161
def starting_point_is_end_point?
        start_x = @to_be_filled.first[0]
        start_y = @to_be_filled.first[1]
        end_x = @to_be_filled.last[0]
        end_y = @to_be_filled.last[1]
        (start_x - end_x).abs <= PIXEL_TOLERANCE and
                (start_y - end_y).abs <= PIXEL_TOLERANCE
end