dudegl 0.1.0 → 0.2.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 +4 -4
- data/lib/arm.rb +80 -0
- data/lib/config.rb +14 -0
- data/lib/dude.rb +40 -0
- data/lib/dudegl.rb +10 -138
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6509c0bdb695721e690b1e1b491f623e667bbb1032674d646624e95ad22be31
|
4
|
+
data.tar.gz: d22a0720fd2579d8130bd07da4da2c01d6d3238a7827055e02d7549516a8ba10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d53a0ac7a567382244034ebcb22f877db3bd4e50db94800c8f609114c4fde4a7b232a9c132f7cc84128c2e545033bed32ca2c06a6f383282e3626829f1da370
|
7
|
+
data.tar.gz: 719724062406b845f675916e99f1de1f2a907b6062f5bd55f721e74077d8efb5a2a70759648dc570d21e1843f58d4c347816b6d4f5f8605e7b9bc5076a713cde
|
data/lib/arm.rb
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
require_relative 'config'
|
2
|
+
require_relative 'utils'
|
3
|
+
|
4
|
+
class Arm
|
5
|
+
include Utils
|
6
|
+
|
7
|
+
attr_reader :end_x, :end_y, :body_side
|
8
|
+
|
9
|
+
def initialize(canvas, params, x0, y0, body_side)
|
10
|
+
@canvas = canvas
|
11
|
+
@params = params
|
12
|
+
@x0 = x0
|
13
|
+
@y0 = y0
|
14
|
+
@end_y = y0
|
15
|
+
@body_side = body_side
|
16
|
+
|
17
|
+
draw_arm
|
18
|
+
end
|
19
|
+
|
20
|
+
def draw_arm
|
21
|
+
caption = @params[:name]
|
22
|
+
length = @params[:length]
|
23
|
+
args_num = @params[:args]
|
24
|
+
conditions = @params[:conditions]
|
25
|
+
|
26
|
+
length <= Config::METHOD_LENGTH_OK_MAX ? arm_length = Config::ARM_LENGTH : arm_length = Config::ARM_LENGTH_LONG
|
27
|
+
arm_length = - arm_length if @body_side == :left
|
28
|
+
@end_x = @x0 + arm_length
|
29
|
+
|
30
|
+
if conditions.positive?
|
31
|
+
draw_conditions(conditions, arm_length, @x0, @y0, @body_side)
|
32
|
+
else
|
33
|
+
draw_line(@canvas, @x0, @y0, @x0 + arm_length, @y0)
|
34
|
+
end
|
35
|
+
|
36
|
+
draw_caption(@canvas, caption, 0.8 * (@x0 + arm_length).round, (0.95 * @y0).round)
|
37
|
+
draw_fingers(args_num)
|
38
|
+
end
|
39
|
+
|
40
|
+
def draw_conditions(conditions, arm_length, x0, y0, body_side)
|
41
|
+
lines_num = conditions + 1
|
42
|
+
line_length = ((arm_length.abs - conditions * Config::ELLIPSE_LENGTH) / lines_num).round
|
43
|
+
body_side == :left ? orientation = -1 : orientation = 1
|
44
|
+
|
45
|
+
x0 = x0
|
46
|
+
x1 = x0 + line_length * orientation
|
47
|
+
|
48
|
+
lines_num.times do |i|
|
49
|
+
draw_line(@canvas, x0, y0, x1, y0)
|
50
|
+
|
51
|
+
@canvas.ellipse cx: (x1 + Config::ELLIPSE_LENGTH * orientation / 2).round,
|
52
|
+
cy: y0, rx: (Config::ELLIPSE_LENGTH / 2).round, ry: (Config::ELLIPSE_LENGTH / 4).round,
|
53
|
+
style: Config::STYLE, fill: 'white' if i < lines_num - 1
|
54
|
+
|
55
|
+
x0 = x0 + (line_length + Config::ELLIPSE_LENGTH) * orientation
|
56
|
+
x1 = x0 + line_length * orientation
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def draw_fingers(args_num)
|
61
|
+
fingers_range = Math::PI / 2.0
|
62
|
+
|
63
|
+
if body_side == :left
|
64
|
+
finger_angle_start = Config::FINGER_ANGLE_START
|
65
|
+
finger_angle_end = finger_angle_start + fingers_range
|
66
|
+
finger_angle_step = calc_range(fingers_range, args_num)
|
67
|
+
else
|
68
|
+
finger_angle_start = - Config::FINGER_ANGLE_START + Math::PI
|
69
|
+
finger_angle_end = finger_angle_start - fingers_range
|
70
|
+
finger_angle_step = - calc_range(fingers_range, args_num)
|
71
|
+
end
|
72
|
+
|
73
|
+
args_num.times do |i|
|
74
|
+
finger_end_x, finger_end_y = circle_rotate(end_x, end_y, Config::FINGER_LENGTH,
|
75
|
+
finger_angle_start + finger_angle_step * i)
|
76
|
+
|
77
|
+
draw_line(@canvas, end_x, end_y, finger_end_x, finger_end_y)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/lib/config.rb
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
module Config
|
2
2
|
STYLE = { stroke: 'black', stroke_width: 1 }
|
3
3
|
IMAGE_DIR = 'images'
|
4
|
+
|
5
|
+
BODY_LENGTH = 140
|
6
|
+
BODY_CENTER = Math::PI * (3 / 2.0)
|
7
|
+
# defines how slim is a body
|
8
|
+
SLIM_FACTOR = Math::PI * (2 / 8.0)
|
9
|
+
HEAD_RADIUS = 40
|
10
|
+
|
11
|
+
METHOD_LENGTH_OK_MAX = 5
|
12
|
+
ARM_LENGTH = 40
|
13
|
+
ARM_LENGTH_LONG = 70
|
14
|
+
ELLIPSE_LENGTH = 10
|
15
|
+
|
16
|
+
FINGER_LENGTH = 10
|
17
|
+
FINGER_ANGLE_START = Math::PI * (3 / 4.0)
|
4
18
|
end
|
data/lib/dude.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require_relative 'utils'
|
2
|
+
require_relative 'config'
|
3
|
+
|
4
|
+
class Dude
|
5
|
+
include Utils
|
6
|
+
|
7
|
+
attr_reader :body_right_x, :body_right_top_y, :body_left_x, :body_left_top_y
|
8
|
+
|
9
|
+
def initialize(canvas, width = 400, height = 400)
|
10
|
+
@width = width
|
11
|
+
@height = height
|
12
|
+
@canvas = canvas
|
13
|
+
draw_dude
|
14
|
+
end
|
15
|
+
|
16
|
+
def draw_dude
|
17
|
+
# head center
|
18
|
+
head_center_x = (0.5 * @width).round
|
19
|
+
head_center_y = (0.3 * @height).round
|
20
|
+
|
21
|
+
@body_right_x, @body_right_top_y = circle_rotate(head_center_x, head_center_y,
|
22
|
+
Config::HEAD_RADIUS,
|
23
|
+
Config::BODY_CENTER + Config::SLIM_FACTOR)
|
24
|
+
@body_left_x, @body_left_top_y = circle_rotate(head_center_x, head_center_y,
|
25
|
+
Config::HEAD_RADIUS,
|
26
|
+
Config::BODY_CENTER - Config::SLIM_FACTOR)
|
27
|
+
# head
|
28
|
+
@canvas.circle cx: head_center_x, cy: head_center_y, r: Config::HEAD_RADIUS,
|
29
|
+
fill: 'white', style: Config::STYLE
|
30
|
+
# right part of a body
|
31
|
+
draw_line(@canvas, @body_right_x, @body_right_top_y,
|
32
|
+
@body_right_x, @body_right_top_y + Config::BODY_LENGTH)
|
33
|
+
# left part of a body
|
34
|
+
draw_line(@canvas, @body_left_x, @body_left_top_y,
|
35
|
+
@body_left_x, @body_left_top_y + Config::BODY_LENGTH)
|
36
|
+
# bottom
|
37
|
+
draw_line(@canvas, @body_right_x, @body_right_top_y + Config::BODY_LENGTH,
|
38
|
+
@body_left_x, @body_left_top_y + Config::BODY_LENGTH)
|
39
|
+
end
|
40
|
+
end
|
data/lib/dudegl.rb
CHANGED
@@ -2,153 +2,25 @@ require 'victor'
|
|
2
2
|
require 'byebug'
|
3
3
|
require_relative 'config'
|
4
4
|
require_relative 'utils'
|
5
|
+
require_relative 'dude'
|
6
|
+
require_relative 'arm'
|
5
7
|
|
6
8
|
class DudeGl
|
7
9
|
include Utils
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
# defines how slim is a body
|
12
|
-
SLIM_FACTOR = Math::PI * (2 / 8.0)
|
13
|
-
HEAD_RADIUS = 40
|
14
|
-
|
15
|
-
METHOD_LENGTH_OK_MAX = 5
|
16
|
-
ARM_LENGTH = 40
|
17
|
-
ARM_LENGTH_LONG = 70
|
18
|
-
ELLIPSE_LENGTH = 10
|
19
|
-
|
20
|
-
FINGER_LENGTH = 10
|
21
|
-
FINGER_ANGLE_START = Math::PI * (3 / 4.0)
|
22
|
-
|
23
|
-
def initialize(object, width = 400, height = 400)
|
24
|
-
@object = object
|
25
|
-
@width = width
|
26
|
-
@height = height
|
27
|
-
create_canvas
|
28
|
-
draw_dude
|
29
|
-
draw_arms
|
30
|
-
end
|
31
|
-
|
32
|
-
def save_to_svg(file_name)
|
33
|
-
@canvas.save "#{Config::IMAGE_DIR}/#{file_name}"
|
34
|
-
end
|
35
|
-
|
36
|
-
private
|
37
|
-
|
38
|
-
def create_canvas
|
39
|
-
@canvas = Victor::SVG.new width: @width, height: @height, style: { background: 'white' }
|
40
|
-
end
|
41
|
-
|
42
|
-
def draw_dude
|
43
|
-
# head center
|
44
|
-
head_center_x = (0.5 * @width).round
|
45
|
-
head_center_y = (0.3 * @height).round
|
46
|
-
|
47
|
-
@body_right_x, @body_right_top_y = circle_rotate(head_center_x, head_center_y,
|
48
|
-
HEAD_RADIUS, BODY_CENTER + SLIM_FACTOR)
|
49
|
-
@body_left_x, @body_left_top_y = circle_rotate(head_center_x, head_center_y,
|
50
|
-
HEAD_RADIUS, BODY_CENTER - SLIM_FACTOR)
|
51
|
-
# head
|
52
|
-
@canvas.circle cx: head_center_x, cy: head_center_y, r: HEAD_RADIUS,
|
53
|
-
fill: 'white', style: Config::STYLE
|
54
|
-
# right part of a body
|
55
|
-
draw_line(@canvas, @body_right_x, @body_right_top_y,
|
56
|
-
@body_right_x, @body_right_top_y + BODY_LENGTH)
|
57
|
-
# left part of a body
|
58
|
-
draw_line(@canvas, @body_left_x, @body_left_top_y,
|
59
|
-
@body_left_x, @body_left_top_y + BODY_LENGTH)
|
60
|
-
# bottom
|
61
|
-
draw_line(@canvas, @body_right_x, @body_right_top_y + BODY_LENGTH,
|
62
|
-
@body_left_x, @body_left_top_y + BODY_LENGTH)
|
63
|
-
end
|
64
|
-
|
65
|
-
def draw_arm(side_arms, x0, y0, body_side)
|
66
|
-
method_name = side_arms[:name]
|
67
|
-
length = side_arms[:length]
|
68
|
-
args_num = side_arms[:args]
|
69
|
-
conditions = side_arms[:conditions]
|
70
|
-
|
71
|
-
length <= METHOD_LENGTH_OK_MAX ? arm_length = ARM_LENGTH : arm_length = ARM_LENGTH_LONG
|
72
|
-
arm_length = - arm_length if body_side == :left
|
73
|
-
|
74
|
-
if conditions.positive?
|
75
|
-
draw_conditions(conditions, arm_length, x0, y0, body_side)
|
76
|
-
else
|
77
|
-
draw_line(@canvas, x0, y0, x0 + arm_length, y0)
|
78
|
-
end
|
79
|
-
|
80
|
-
draw_caption(@canvas, method_name, 0.8 * (x0 + arm_length).round, (0.95 * y0).round)
|
81
|
-
draw_fingers(args_num, x0 + arm_length, y0, body_side)
|
11
|
+
def create_canvas(width = 400, height = 400)
|
12
|
+
@canvas = Victor::SVG.new width: width, height: height, style: { background: 'white' }
|
82
13
|
end
|
83
14
|
|
84
|
-
def
|
85
|
-
|
86
|
-
divide_arms
|
87
|
-
|
88
|
-
@left_arms_num.times do |i|
|
89
|
-
hy = (1.05 * @body_left_top_y + i * @arms_step).round
|
90
|
-
draw_arm(@left_arms[i], @body_left_x, hy, :left)
|
91
|
-
end
|
92
|
-
|
93
|
-
@right_arms_num.times do |i|
|
94
|
-
hy = (1.05 * @body_right_top_y + i * @arms_step).round
|
95
|
-
draw_arm(@right_arms[i], @body_right_x, hy, :right)
|
96
|
-
end
|
15
|
+
def create_dude
|
16
|
+
@dude = Dude.new(@canvas)
|
97
17
|
end
|
98
18
|
|
99
|
-
def
|
100
|
-
|
101
|
-
line_length = ((arm_length.abs - conditions * ELLIPSE_LENGTH) / lines_num).round
|
102
|
-
body_side == :left ? orientation = -1 : orientation = 1
|
103
|
-
|
104
|
-
x0 = x0
|
105
|
-
x1 = x0 + line_length * orientation
|
106
|
-
|
107
|
-
lines_num.times do |i|
|
108
|
-
draw_line(@canvas, x0, y0, x1, y0)
|
109
|
-
|
110
|
-
@canvas.ellipse cx: (x1 + ELLIPSE_LENGTH * orientation / 2).round,
|
111
|
-
cy: y0, rx: (ELLIPSE_LENGTH / 2).round, ry: (ELLIPSE_LENGTH / 4).round,
|
112
|
-
style: Config::STYLE, fill: 'white' if i < lines_num - 1
|
113
|
-
|
114
|
-
x0 = x0 + (line_length + ELLIPSE_LENGTH) * orientation
|
115
|
-
x1 = x0 + line_length * orientation
|
116
|
-
end
|
19
|
+
def add_arm(canvas, params, x0, y0, body_side)
|
20
|
+
arm = Arm.new(canvas, params, x0, y0, body_side)
|
117
21
|
end
|
118
22
|
|
119
|
-
def
|
120
|
-
@
|
121
|
-
remainder = @methods_num % 2
|
122
|
-
|
123
|
-
@left_arms_num = (@methods_num - remainder) / 2 + remainder
|
124
|
-
@right_arms_num = @methods_num - @left_arms_num
|
125
|
-
|
126
|
-
# BODY_LENGTH * 0.9 - to avoid drawing hands on a body edges
|
127
|
-
@arms_step = ((BODY_LENGTH * 0.9) / @left_arms_num).round
|
128
|
-
end
|
129
|
-
|
130
|
-
def divide_arms
|
131
|
-
@left_arms = @object[:methods][0, @left_arms_num]
|
132
|
-
@right_arms = @object[:methods][@left_arms_num, @methods_num]
|
133
|
-
end
|
134
|
-
|
135
|
-
def draw_fingers(args_num, arm_end_x, arm_end_y, body_side)
|
136
|
-
fingers_range = Math::PI / 2.0
|
137
|
-
|
138
|
-
if body_side == :left
|
139
|
-
finger_angle_start = FINGER_ANGLE_START
|
140
|
-
finger_angle_end = finger_angle_start + fingers_range
|
141
|
-
finger_angle_step = calc_range(fingers_range, args_num)
|
142
|
-
else
|
143
|
-
finger_angle_start = - FINGER_ANGLE_START + Math::PI
|
144
|
-
finger_angle_end = finger_angle_start - fingers_range
|
145
|
-
finger_angle_step = - calc_range(fingers_range, args_num)
|
146
|
-
end
|
147
|
-
|
148
|
-
args_num.times do |i|
|
149
|
-
finger_end_x, finger_end_y = circle_rotate(arm_end_x, arm_end_y, FINGER_LENGTH,
|
150
|
-
finger_angle_start + finger_angle_step * i)
|
151
|
-
draw_line(@canvas, arm_end_x, arm_end_y, finger_end_x, finger_end_y)
|
152
|
-
end
|
23
|
+
def save_to_svg(file_name)
|
24
|
+
@canvas.save "#{Config::IMAGE_DIR}/#{file_name}"
|
153
25
|
end
|
154
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dudegl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Khramtsov
|
@@ -19,7 +19,9 @@ executables: []
|
|
19
19
|
extensions: []
|
20
20
|
extra_rdoc_files: []
|
21
21
|
files:
|
22
|
+
- lib/arm.rb
|
22
23
|
- lib/config.rb
|
24
|
+
- lib/dude.rb
|
23
25
|
- lib/dudegl.rb
|
24
26
|
- lib/utils.rb
|
25
27
|
homepage: https://github.com/dmikhr/DudeGL/wiki
|