Class: EduDraw::Pen

Inherits:
Object
  • Object
show all
Defined in:
lib/edu_draw/pen.rb

Overview

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

Direct Known Subclasses

AnimationPen

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN) ⇒ Pen

Creates a new Pen

Parameters:

  • x (Fixnum) (defaults to: 0)

    Initial value of #x

  • y (Fixnum) (defaults to: 0)

    Initial value of #y

  • angle (Fixnum) (defaults to: 0)

    Initial value of #angle

  • color (Gosu::Color) (defaults to: Gosu::Color::GREEN)

    Initial value of #color



31
32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#angleFixnum

Returns Direction of the pen in degree where 0 points to the right.

Returns:

  • (Fixnum)

    Direction of the pen in degree where 0 points to the right



20
21
22
# File 'lib/edu_draw/pen.rb', line 20

def angle
  @angle
end

#colorGosu::Color

Returns Color of the pen.

Returns:

  • (Gosu::Color)

    Color of the pen



23
24
25
# File 'lib/edu_draw/pen.rb', line 23

def color
  @color
end

#shapesObject (readonly)

Shapes to be drawn by sheet



11
12
13
# File 'lib/edu_draw/pen.rb', line 11

def shapes
  @shapes
end

#xFixnum

Returns x-coordinate of position in pixel where left is 0.

Returns:

  • (Fixnum)

    x-coordinate of position in pixel where left is 0



14
15
16
# File 'lib/edu_draw/pen.rb', line 14

def x
  @x
end

#yFixnum

Returns y-coordinate of position in pixel where top is 0.

Returns:

  • (Fixnum)

    y-coordinate of position in pixel where top is 0



17
18
19
# File 'lib/edu_draw/pen.rb', line 17

def y
  @y
end

Instance Method Details

#angle_in_radFixnum

Returns angle in radians instead of degree.

Returns:

  • (Fixnum)

    angle in radians instead of degree



98
99
100
# File 'lib/edu_draw/pen.rb', line 98

def angle_in_rad
	angle * Math::PI / 180.0
end

#down!Object

Sticks the pen to the Sheet

See Also:



81
82
83
# File 'lib/edu_draw/pen.rb', line 81

def down!
	@up = false
end

#down?Boolean

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

Returns:

  • (Boolean)


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

def down?
	!up?
end

#fill(&block) ⇒ Object

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.

Draws filled shapes.

Examples:

# This draws a triangle
pen.fill do
	pen.move 10
	pen.turn_right 90
	pen.move 10
 end


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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) ⇒ Object

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

Parameters:

  • length (Fixnum)

    Amount of pixels moved



89
90
91
92
93
94
95
# 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) ⇒ Object

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

Parameters:

  • degree (Fixnum)

    Amount of degree to be turned



46
47
48
49
# 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) ⇒ Object

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

Parameters:

  • degree (Fixnum)

    Amount of degree to be turned



55
56
57
# File 'lib/edu_draw/pen.rb', line 55

def turn_right(degree)
	turn_left -degree
end

#up!Object

Lifts the pen from the Sheet

See Also:



73
74
75
# File 'lib/edu_draw/pen.rb', line 73

def up!
	@up = true
end

#up?Boolean

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

Returns:

  • (Boolean)


60
61
62
# File 'lib/edu_draw/pen.rb', line 60

def up?
	@up
end

#updateObject

Hook method



134
135
136
# File 'lib/edu_draw/pen.rb', line 134

def update
	# nothing to do since nothing ever changes
end