Class: EduDraw::Pen
- Inherits:
-
Object
- Object
- EduDraw::Pen
- 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
Instance Attribute Summary collapse
-
#angle ⇒ Fixnum
Direction of the pen in degree where 0 points to the right.
-
#color ⇒ Gosu::Color
Color of the pen.
-
#shapes ⇒ Object
readonly
Shapes to be drawn by sheet.
-
#x ⇒ Fixnum
X-coordinate of position in pixel where left is 0.
-
#y ⇒ Fixnum
Y-coordinate of position in pixel where top is 0.
Instance Method Summary collapse
-
#angle_in_rad ⇒ Fixnum
Angle in radians instead of degree.
-
#down! ⇒ Object
Sticks the pen to the Sheet.
-
#down? ⇒ Boolean
If the pen is down, it touches the Sheet and is drawing when moved.
-
#fill(&block) ⇒ Object
Draws filled shapes.
-
#initialize(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN) ⇒ Pen
constructor
Creates a new Pen.
-
#move(length) ⇒ Object
Moves the stick an amount of pixel into its direction.
-
#turn_left(degree) ⇒ Object
Changes the direction of self to be turned to the left The #angle stays within 0..364.
-
#turn_right(degree) ⇒ Object
Changes the direction of self to be turned to the right The #angle stays within 0..364.
-
#up! ⇒ Object
Lifts the pen from the Sheet.
-
#up? ⇒ Boolean
If the pen is up, it does not touch the Sheet and is not drawing.
-
#update ⇒ Object
Hook method.
Constructor Details
#initialize(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN) ⇒ Pen
Creates a new Pen
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
#angle ⇒ Fixnum
Returns 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 |
#color ⇒ Gosu::Color
Returns Color of the pen.
23 24 25 |
# File 'lib/edu_draw/pen.rb', line 23 def color @color end |
#shapes ⇒ Object (readonly)
Shapes to be drawn by sheet
11 12 13 |
# File 'lib/edu_draw/pen.rb', line 11 def shapes @shapes end |
#x ⇒ Fixnum
Returns 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 |
#y ⇒ Fixnum
Returns 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_rad ⇒ Fixnum
Returns 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
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.
65 66 67 |
# File 'lib/edu_draw/pen.rb', line 65 def down? !up? end |
#fill(&block) ⇒ Object
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.
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?
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
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
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
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.
60 61 62 |
# File 'lib/edu_draw/pen.rb', line 60 def up? @up end |
#update ⇒ Object
Hook method
134 135 136 |
# File 'lib/edu_draw/pen.rb', line 134 def update # nothing to do since nothing ever changes end |