edu_draw 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +22 -0
- data/README.md +65 -0
- data/Rakefile +7 -0
- data/{example.rb → examples/lines.rb} +17 -4
- data/lib/edu_draw/pen.rb +42 -5
- data/lib/edu_draw/sheet.rb +24 -1
- data/test/edu_draw/pen_test.rb +62 -0
- data/test/edu_draw/sheet_test.rb +33 -0
- data/test/test_helper.rb +5 -0
- metadata +24 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd768abe04a69ab7b6157dfff3bcad5bb051464b
|
4
|
+
data.tar.gz: cfa2a99ee7eb1320b237ce207dd8d4c595cf3247
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ffca39137a42964697fb9bf6fe8de51caae460af6022f8cb3f3d90775ffa1c58949225743e1a6162929bc2c590fd240a0aef1a5bcff34ba14da2c80489def5ef
|
7
|
+
data.tar.gz: 1847a9ed2b554d930c2eab0da09aab7abcdeb7e4fb76106492558a6e24e6bbc0f87ee572fbe7e5f6da4bfafdbc053e9f79c1a1eabe0705f0ea1aa29542845506
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 AKnopf
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# edu_draw
|
2
|
+
Simple ruby drawing API based on gosu meant for educational purposes.
|
3
|
+
|
4
|
+
## Install
|
5
|
+
`gem install edu_draw`
|
6
|
+
|
7
|
+
If you run into any problems its most likely because of [gosu](https://github.com/jlnr/gosu/wiki/Ruby-Tutorial), as this is the only runtime dependency of edu_draw.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
There are only two classes: A sheet to draw on and a pen to draw with. The sheet is also the window. Unfortunately, the underlying engine only supports really one window, so you should only create one sheet per program. This is done as follows:
|
11
|
+
First, load edu_draw into your program
|
12
|
+
|
13
|
+
`require "edu_draw"`
|
14
|
+
|
15
|
+
Then, create the sheet
|
16
|
+
|
17
|
+
`sheet = EduDraw::Sheet.new x: 500, y: 600, title: "It works"`
|
18
|
+
|
19
|
+
Now that you have a sheet, that is 500 pixels wide and 600 pixels high, you can create pens for this sheet to draw on.
|
20
|
+
|
21
|
+
`green_pen = sheet.new_pen x: 100, y: 20`
|
22
|
+
|
23
|
+
Green is the default color for pens but you can have any color using the `color` parameter.
|
24
|
+
|
25
|
+
Now it's time to draw a rectangle!
|
26
|
+
|
27
|
+
```
|
28
|
+
green_pen.move 100
|
29
|
+
green_pen.turn_right 90
|
30
|
+
green_pen.move 80
|
31
|
+
green_pen.turn_right 90
|
32
|
+
green_pen.move 100
|
33
|
+
green_pen.turn_right 90
|
34
|
+
green_pen.move 80
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
When you are done drawing, you have to make one final call to the sheet, to actually display the window with your drawings.
|
39
|
+
|
40
|
+
`sheet.show`
|
41
|
+
|
42
|
+
The whole program looks like this:
|
43
|
+
|
44
|
+
```
|
45
|
+
require "edu_draw"
|
46
|
+
|
47
|
+
sheet = EduDraw::Sheet.new x: 500, y: 600, title: "It works"
|
48
|
+
green_pen = sheet.new_pen(x: 100, y: 20)
|
49
|
+
|
50
|
+
green_pen.move 100
|
51
|
+
green_pen.turn_right 90
|
52
|
+
green_pen.move 80
|
53
|
+
green_pen.turn_right 90
|
54
|
+
green_pen.move 100
|
55
|
+
green_pen.turn_right 90
|
56
|
+
green_pen.move 80
|
57
|
+
|
58
|
+
sheet.show
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
Feel free to contribute anything, may it be better tests, better documentation or more features. As you can see, I tried to keep the code tidy so it is appreciated if you'd do the same. Just add a pull request and we will make it work. Oh, and don't forget to have fun ;)
|
63
|
+
|
64
|
+
## Kudos
|
65
|
+
Kudos to the wonderful people behind [Gosu](http://www.libgosu.org/). You do some great work.
|
data/Rakefile
ADDED
@@ -1,21 +1,29 @@
|
|
1
|
-
|
1
|
+
require "edu_draw"
|
2
2
|
|
3
|
+
# Create a new Sheet. That's your window.
|
3
4
|
sheet = EduDraw::Sheet.new x: 500, y: 600, title: "It works"
|
5
|
+
|
6
|
+
# Create a new pen for this sheet at the position 100 pixel from the left and 20 pixel from the top
|
4
7
|
pen = sheet.new_pen(x: 100, y: 20)
|
8
|
+
|
9
|
+
# Draw a decagon
|
5
10
|
10.times do
|
6
11
|
pen.move 40
|
7
12
|
pen.turn_right 36
|
8
13
|
end
|
9
14
|
|
15
|
+
# Move to the right without drawing a line
|
10
16
|
pen.up!
|
11
17
|
pen.move 150
|
12
18
|
pen.down!
|
13
19
|
|
14
|
-
|
20
|
+
# Draw a octagon
|
21
|
+
8.times do
|
15
22
|
pen.move 40
|
16
|
-
pen.turn_right
|
23
|
+
pen.turn_right 45
|
17
24
|
end
|
18
25
|
|
26
|
+
# Move to the middle bottom of the sheet without drawing lines
|
19
27
|
pen.up!
|
20
28
|
pen.move -55
|
21
29
|
pen.turn_right 90
|
@@ -24,6 +32,7 @@ pen.turn_left 180
|
|
24
32
|
pen.down!
|
25
33
|
pen.move 10
|
26
34
|
|
35
|
+
# Define how to draw a tree recursively
|
27
36
|
def draw_tree(pen, length)
|
28
37
|
angle = 20
|
29
38
|
min_length = 4
|
@@ -38,9 +47,13 @@ def draw_tree(pen, length)
|
|
38
47
|
pen.move -length
|
39
48
|
end
|
40
49
|
|
50
|
+
# Draw 8 trees in a circle
|
41
51
|
8.times do
|
42
52
|
draw_tree pen, 40
|
43
53
|
pen.turn_left 45
|
44
54
|
end
|
45
55
|
|
46
|
-
|
56
|
+
# After all drawing is done, #show has to be called on the window to actually display everything
|
57
|
+
sheet.show
|
58
|
+
|
59
|
+
# Press Escape to close the window again
|
data/lib/edu_draw/pen.rb
CHANGED
@@ -1,12 +1,25 @@
|
|
1
1
|
module EduDraw
|
2
|
-
# A Pen is a drawing tool that provides basic drawing functionalities on a 2d
|
3
|
-
# It has a certain position (x,y) in pixel where (0,0) is the top-left corner.
|
4
|
-
# It has an angel in degree, where 0 points to the right.
|
5
|
-
# It also has a certain color in which it draws.
|
2
|
+
# A Pen is a drawing tool that provides basic drawing functionalities on a 2d {Sheet}.
|
6
3
|
class Pen
|
7
4
|
|
8
|
-
|
5
|
+
# @return [Fixnum] x-coordinate of position in pixel where left is 0
|
6
|
+
attr_reader :x
|
9
7
|
|
8
|
+
# @return [Fixnum] y-coordinate of position in pixel where top is 0
|
9
|
+
attr_reader :y
|
10
|
+
|
11
|
+
# @return [Fixnum] Direction of the pen in degree where 0 points to the right
|
12
|
+
attr_reader :angle
|
13
|
+
|
14
|
+
# @return [Gosu::Color] Color of the pen
|
15
|
+
attr_reader :color
|
16
|
+
|
17
|
+
# Creates a new Sheet
|
18
|
+
#
|
19
|
+
# @param x [Fixnum] initial value of {#x}
|
20
|
+
# @param y [Fixnum] initial value of {#y}
|
21
|
+
# @param angle [Fixnum] initial value of {#angle}
|
22
|
+
# @param color [Gosu::Color] initial value of {#color}
|
10
23
|
def initialize(sheet, x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN)
|
11
24
|
@x = x
|
12
25
|
@y = y
|
@@ -17,30 +30,53 @@ module EduDraw
|
|
17
30
|
down!
|
18
31
|
end
|
19
32
|
|
33
|
+
# Changes the direction of self to be turned to the left
|
34
|
+
# The {#angle} stays within 0..364
|
35
|
+
#
|
36
|
+
# @param degree [Fixnum] Amount of degree to be turned
|
20
37
|
def turn_left(degree)
|
21
38
|
@angle -= degree
|
39
|
+
@angle = (@angle + 360) % 360 # Make sure angle is always in 0..359
|
22
40
|
end
|
23
41
|
|
42
|
+
# Changes the direction of self to be turned to the right
|
43
|
+
# The {#angle} stays within 0..364
|
44
|
+
#
|
45
|
+
# @param degree [Fixnum] Amount of degree to be turned
|
24
46
|
def turn_right(degree)
|
25
47
|
turn_left -degree
|
26
48
|
end
|
27
49
|
|
50
|
+
# If the pen is up, it does not touch the {Sheet} and is not drawing. It can still be moved.
|
28
51
|
def up?
|
29
52
|
@up
|
30
53
|
end
|
31
54
|
|
55
|
+
# If the pen is up, it touches the {Sheet} and is drawing when moved.
|
32
56
|
def down?
|
33
57
|
!up?
|
34
58
|
end
|
35
59
|
|
60
|
+
# Lifts the pen from the {Sheet}
|
61
|
+
#
|
62
|
+
# @see {#up?}
|
63
|
+
# @see {#down?}
|
36
64
|
def up!
|
37
65
|
@up = true
|
38
66
|
end
|
39
67
|
|
68
|
+
# Sticks the pen to the {Sheet}
|
69
|
+
#
|
70
|
+
# @see {#up?}
|
71
|
+
# @see {#down?}
|
40
72
|
def down!
|
41
73
|
@up = false
|
42
74
|
end
|
43
75
|
|
76
|
+
# Moves the stick an amount of pixel into its direction.
|
77
|
+
# Draws when it is {#down?}
|
78
|
+
#
|
79
|
+
# @param length [Fixnum] Amount of pixels moved
|
44
80
|
def move(length)
|
45
81
|
target_x = x + Math.cos(angle_in_rad) * length
|
46
82
|
target_y = y + Math.sin(angle_in_rad) * length
|
@@ -51,6 +87,7 @@ module EduDraw
|
|
51
87
|
@y = target_y
|
52
88
|
end
|
53
89
|
|
90
|
+
# @return [Fixnum] angle in radians instead of degree
|
54
91
|
def angle_in_rad
|
55
92
|
angle * Math::PI / 180.0
|
56
93
|
end
|
data/lib/edu_draw/sheet.rb
CHANGED
@@ -1,17 +1,39 @@
|
|
1
1
|
module EduDraw
|
2
|
-
# A Sheet is a 2D-Canvas that can be drawn on using Pen
|
2
|
+
# A Sheet is a 2D-Canvas that can be drawn on using {Pen}
|
3
3
|
class Sheet < Gosu::Window
|
4
|
+
|
5
|
+
# @private
|
4
6
|
attr_reader :shapes
|
7
|
+
|
8
|
+
# Creates a new Sheet
|
9
|
+
#
|
10
|
+
# @param x [Fixnum] width in pixels
|
11
|
+
# @param y [Fixnum] height in pixels
|
12
|
+
# @param title [String] Caption of the window
|
5
13
|
def initialize(x: 100, y: 100, title: "A blank sheet")
|
6
14
|
super x, y, false
|
7
15
|
self.caption = title
|
8
16
|
@shapes = []
|
9
17
|
end
|
10
18
|
|
19
|
+
# @private
|
20
|
+
def needs_cursor?
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
# Creates a new {Pen} that draws on self
|
25
|
+
#
|
26
|
+
# @see {Pen}
|
27
|
+
#
|
28
|
+
# @param x [Fixnum] x-coordinate of starting position. Left is 0.
|
29
|
+
# @param y [Fixnum] y-coordinate of starting position. Top is 0.
|
30
|
+
# @param angle [Fixnum] Direction of pen in degree. 0 points to the right.
|
31
|
+
# @param color [Gosu::Color] Color of the pen
|
11
32
|
def new_pen(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN)
|
12
33
|
Pen.new(self, x: x, y: y, angle: angle, color: color)
|
13
34
|
end
|
14
35
|
|
36
|
+
# @private
|
15
37
|
def draw
|
16
38
|
shapes.each do |shape|
|
17
39
|
method,*args = shape
|
@@ -19,6 +41,7 @@ module EduDraw
|
|
19
41
|
end
|
20
42
|
end
|
21
43
|
|
44
|
+
# @private
|
22
45
|
def update
|
23
46
|
if button_down? Gosu::KbEscape
|
24
47
|
close
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
module EduDraw
|
3
|
+
class PenTest < Minitest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@sheet_stub = stub shapes: []
|
6
|
+
@pen = Pen.new(@sheet_stub, x: 10, y: 20, angle: 25, color: Gosu::Color::BLACK)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_initialize
|
10
|
+
assert_equal 10, @pen.x
|
11
|
+
assert_equal 20, @pen.y
|
12
|
+
assert_equal 25, @pen.angle
|
13
|
+
assert_equal Gosu::Color::BLACK, @pen.color
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_move
|
17
|
+
@pen.move 100
|
18
|
+
expected_x = 10 + Math.cos(25 * Math::PI / 180.0) * 100
|
19
|
+
expected_y = 20 + Math.sin(25 * Math::PI / 180.0) * 100
|
20
|
+
assert_equal expected_x, @pen.x # x changed
|
21
|
+
assert_equal expected_y, @pen.y # y changed
|
22
|
+
assert_equal 25, @pen.angle # angle unchanged
|
23
|
+
assert_equal Gosu::Color::BLACK, @pen.color # color unchanged
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_doesnt_draws_when_up
|
27
|
+
@sheet_stub.expects(:shapes).times(0)
|
28
|
+
@pen.up!
|
29
|
+
@pen.move 100
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_up_and_down
|
33
|
+
assert @pen.down?
|
34
|
+
refute @pen.up?
|
35
|
+
|
36
|
+
@pen.up!
|
37
|
+
assert @pen.up?
|
38
|
+
refute @pen.down?
|
39
|
+
|
40
|
+
@pen.down!
|
41
|
+
assert @pen.down?
|
42
|
+
refute @pen.up?
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_turning
|
46
|
+
assert_equal 25, @pen.angle
|
47
|
+
|
48
|
+
@pen.turn_left 25
|
49
|
+
assert_equal 0, @pen.angle
|
50
|
+
|
51
|
+
@pen.turn_left 10
|
52
|
+
assert_equal 350, @pen.angle
|
53
|
+
|
54
|
+
@pen.turn_right 20
|
55
|
+
assert_equal 10, @pen.angle
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_angle_in_rad
|
59
|
+
assert_equal 25 * Math::PI / 180.0, @pen.angle_in_rad
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_relative "../test_helper"
|
2
|
+
module EduDraw
|
3
|
+
class SheetTest < Minitest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@sheet = Sheet.new(x: 10, y: 20, title: "test")
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_initialize
|
9
|
+
assert_equal 10, @sheet.width
|
10
|
+
assert_equal 20, @sheet.height
|
11
|
+
assert_equal "test", @sheet.caption
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_cursor_visible
|
15
|
+
assert @sheet.needs_cursor?
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_window
|
19
|
+
assert_kind_of Gosu::Window, @sheet
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_new_pen_creates_pen
|
23
|
+
assert_respond_to @sheet, :new_pen
|
24
|
+
assert_kind_of Pen, @sheet.new_pen
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_new_pen_creates_new_instance
|
28
|
+
Pen.expects(:new).once
|
29
|
+
@sheet.new_pen
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: edu_draw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marvin Ede
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mocha
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.1'
|
27
41
|
description: Simple object oriented API for opening a window and drawing in it. Based
|
28
42
|
on gosu. It's meant to teach beginners programming as a visualization of code can
|
29
43
|
help.
|
@@ -32,11 +46,17 @@ executables: []
|
|
32
46
|
extensions: []
|
33
47
|
extra_rdoc_files: []
|
34
48
|
files:
|
35
|
-
-
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- examples/lines.rb
|
36
53
|
- lib/edu_draw.rb
|
37
54
|
- lib/edu_draw/pen.rb
|
38
55
|
- lib/edu_draw/sheet.rb
|
39
|
-
|
56
|
+
- test/edu_draw/pen_test.rb
|
57
|
+
- test/edu_draw/sheet_test.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
homepage: https://github.com/AKnopf/edu_draw
|
40
60
|
licenses:
|
41
61
|
- MIT
|
42
62
|
metadata: {}
|
@@ -61,3 +81,4 @@ signing_key:
|
|
61
81
|
specification_version: 4
|
62
82
|
summary: Offers a simple API to open a window and draw in it
|
63
83
|
test_files: []
|
84
|
+
has_rdoc:
|