edu_draw 1.0.0
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 +7 -0
- data/example.rb +46 -0
- data/lib/edu_draw.rb +3 -0
- data/lib/edu_draw/pen.rb +58 -0
- data/lib/edu_draw/sheet.rb +28 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e69b855005aad0caf28a5744f23ec795726ee46
|
4
|
+
data.tar.gz: 2fdf15c6ccc60044879e4da397b1958c6d9150bf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aea2b517f916a19d104c936e492abb5c34cad4dc6a693ef6ed855b980df4c9351374df6b3a4212c4aa2770d81e2c4497284cda0b7c83a657d2071eb06a7fbb08
|
7
|
+
data.tar.gz: 9b1357680876556026150810b66eee8724cbd24b8109e6de090a415c9de3b95901c14a22ee575dd2fbc3b28d408e50a598240d28d471d532f77c0c6ba41fea88
|
data/example.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'edu_draw'
|
2
|
+
|
3
|
+
sheet = EduDraw::Sheet.new x: 500, y: 600, title: "It works"
|
4
|
+
pen = sheet.new_pen(x: 100, y: 20)
|
5
|
+
10.times do
|
6
|
+
pen.move 40
|
7
|
+
pen.turn_right 36
|
8
|
+
end
|
9
|
+
|
10
|
+
pen.up!
|
11
|
+
pen.move 150
|
12
|
+
pen.down!
|
13
|
+
|
14
|
+
10.times do
|
15
|
+
pen.move 40
|
16
|
+
pen.turn_right 36
|
17
|
+
end
|
18
|
+
|
19
|
+
pen.up!
|
20
|
+
pen.move -55
|
21
|
+
pen.turn_right 90
|
22
|
+
pen.move 250
|
23
|
+
pen.turn_left 180
|
24
|
+
pen.down!
|
25
|
+
pen.move 10
|
26
|
+
|
27
|
+
def draw_tree(pen, length)
|
28
|
+
angle = 20
|
29
|
+
min_length = 4
|
30
|
+
shrink_rate = 0.7
|
31
|
+
return if length < min_length
|
32
|
+
pen.move length
|
33
|
+
pen.turn_left angle
|
34
|
+
draw_tree pen, length * shrink_rate
|
35
|
+
pen.turn_right angle * 2
|
36
|
+
draw_tree pen, length * shrink_rate
|
37
|
+
pen.turn_left angle
|
38
|
+
pen.move -length
|
39
|
+
end
|
40
|
+
|
41
|
+
8.times do
|
42
|
+
draw_tree pen, 40
|
43
|
+
pen.turn_left 45
|
44
|
+
end
|
45
|
+
|
46
|
+
sheet.show
|
data/lib/edu_draw.rb
ADDED
data/lib/edu_draw/pen.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module EduDraw
|
2
|
+
# A Pen is a drawing tool that provides basic drawing functionalities on a 2d sheet.
|
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.
|
6
|
+
class Pen
|
7
|
+
|
8
|
+
attr_reader :x, :y, :angle, :color
|
9
|
+
|
10
|
+
def initialize(sheet, x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN)
|
11
|
+
@x = x
|
12
|
+
@y = y
|
13
|
+
@angle = angle
|
14
|
+
@color = color
|
15
|
+
@sheet = sheet
|
16
|
+
|
17
|
+
down!
|
18
|
+
end
|
19
|
+
|
20
|
+
def turn_left(degree)
|
21
|
+
@angle -= degree
|
22
|
+
end
|
23
|
+
|
24
|
+
def turn_right(degree)
|
25
|
+
turn_left -degree
|
26
|
+
end
|
27
|
+
|
28
|
+
def up?
|
29
|
+
@up
|
30
|
+
end
|
31
|
+
|
32
|
+
def down?
|
33
|
+
!up?
|
34
|
+
end
|
35
|
+
|
36
|
+
def up!
|
37
|
+
@up = true
|
38
|
+
end
|
39
|
+
|
40
|
+
def down!
|
41
|
+
@up = false
|
42
|
+
end
|
43
|
+
|
44
|
+
def move(length)
|
45
|
+
target_x = x + Math.cos(angle_in_rad) * length
|
46
|
+
target_y = y + Math.sin(angle_in_rad) * length
|
47
|
+
if down?
|
48
|
+
@sheet.shapes << [:draw_line, x, y, color, target_x, target_y, color]
|
49
|
+
end
|
50
|
+
@x = target_x
|
51
|
+
@y = target_y
|
52
|
+
end
|
53
|
+
|
54
|
+
def angle_in_rad
|
55
|
+
angle * Math::PI / 180.0
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module EduDraw
|
2
|
+
# A Sheet is a 2D-Canvas that can be drawn on using Pen
|
3
|
+
class Sheet < Gosu::Window
|
4
|
+
attr_reader :shapes
|
5
|
+
def initialize(x: 100, y: 100, title: "A blank sheet")
|
6
|
+
super x, y, false
|
7
|
+
self.caption = title
|
8
|
+
@shapes = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def new_pen(x: 0, y: 0, angle: 0, color: Gosu::Color::GREEN)
|
12
|
+
Pen.new(self, x: x, y: y, angle: angle, color: color)
|
13
|
+
end
|
14
|
+
|
15
|
+
def draw
|
16
|
+
shapes.each do |shape|
|
17
|
+
method,*args = shape
|
18
|
+
send method, *args
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def update
|
23
|
+
if button_down? Gosu::KbEscape
|
24
|
+
close
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: edu_draw
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marvin Ede
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: gosu
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.8'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.8'
|
27
|
+
description: Simple object oriented API for opening a window and drawing in it. Based
|
28
|
+
on gosu. It's meant to teach beginners programming as a visualization of code can
|
29
|
+
help.
|
30
|
+
email: marvinede@gmx.net
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- example.rb
|
36
|
+
- lib/edu_draw.rb
|
37
|
+
- lib/edu_draw/pen.rb
|
38
|
+
- lib/edu_draw/sheet.rb
|
39
|
+
homepage: http://rubygems.org/gems/edu_draw
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata: {}
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 2.2.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: Offers a simple API to open a window and draw in it
|
63
|
+
test_files: []
|