drawille 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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDIyMDkxN2Y3YmYxYWEzMDMwZDBlNGQ5MDE1MzAyZTM5MWNlZDZjMA==
4
+ Yjk3ZmI2NzE4YzNjYzM0ZWEyYjJjNDU2OWY0NDhmY2VjNzRlMGVlNQ==
5
5
  data.tar.gz: !binary |-
6
- Y2VmODE0YmRhMzZmNzI1NGZmOTAwNmUxODEzNDM0MTQwNDcwY2NiMQ==
6
+ NDVmZWMzYzY1MjA5YWE1ZjlkZWY1YzM1ODhiMzljMWI4NzhiNWQzOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGYxYTk4OTQyZWIzYzRlMzBmNjhhMjdjZDc1OTdlMzZmN2IzYTNkNzE5NTI5
10
- MGUwMjc1NjU1OGY2ZGEzNWFlNGEzNzliY2U2NjY3MmI0MDNjNWFkZGRlODg5
11
- YzZlOTlhMzRkODZmZjY1M2VjODY1MmMzMzQ3ZTNjZDRhMDE1NWM=
9
+ MjJkZTdjM2I1MTZlYTZlYWI1ZTM5Zjk0Zjc1YTkzZmExODEwZTUyZWU5N2Jm
10
+ MjYxYTAyNmFlOWY3YTVmMTIyN2NlMDFlNzhiMjEyODczZjEyOTRmY2JjYjI5
11
+ M2YxOWZkYmNjNmM3MmRlMjU3MDAwMTJkNWY4ZTdiZWJlY2I1NzM=
12
12
  data.tar.gz: !binary |-
13
- MjJhOGU4MjJkMGQ1NmMyMDkzNDViYjY1Mjc0MjlhZDc3ODk1YWVkZWIzYmNl
14
- YWViOTc3ODZmYWIzMWZmNDliY2EwM2UzODE2YjY4MzgxMWM0ZjE1Yzk0N2Rl
15
- MDdmYjhkY2RmYjg0ZGFkMjY1MTJkOGQxYzNjM2MzMzk1YjlhY2Q=
13
+ MDg3NjU4OTNmOTdkNzdlNmU4ODY5ODQwNWE1OTYyZWEzNzE4NTkzZmRmNTIy
14
+ NGFiODE4OTkxN2YyZTNmMjc0MjdkZDdiNDY3ZWYxMTc3MzhjMzFlN2YyYjc1
15
+ OWEwZWM5ZTg0MTM5ZTgwMDA3ODcyMDJjOGJkYWFmYzNkZWQ2Yjg=
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  RUBY-DRAWILLE
2
2
  ========
3
3
 
4
- Drawing in terminal with Unicode [Braille][] characters. Implementation based on [drawille][] by @asciimoo.
4
+ Draw in your terminal with Unicode [Braille][] characters. Implementation based on [drawille][] by @asciimoo.
5
5
 
6
6
  [Braille]: http://en.wikipedia.org/wiki/Braille
7
7
  [Drawille]: https://github.com/asciimoo/drawille
@@ -23,6 +23,8 @@ Or install it yourself as:
23
23
 
24
24
  ## Usage
25
25
 
26
+ Drawille can be used like the Python implementation due to the similar API. Here is on of its examples:
27
+
26
28
  ```ruby
27
29
  c = Drawille::Canvas.new
28
30
 
@@ -33,10 +35,116 @@ end
33
35
  puts c.frame
34
36
  ```
35
37
 
36
- ## Contributing
38
+ ![Sinus](docs/images/sinus.gif)
39
+
40
+ This implementation also includes a Turtle graphics API for all your beloved fractals. See this simple example:
41
+
42
+ ```ruby
43
+ require 'drawille'
44
+
45
+ canvas = Drawille::Canvas.new
46
+
47
+ frame = canvas.paint do
48
+ move 300, 300
49
+ down
50
+
51
+ 36.times do
52
+ right 10
53
+ 36.times do
54
+ right 10
55
+ forward 8
56
+ end
57
+ end
58
+
59
+ end.frame
60
+
61
+ puts frame
62
+ ```
63
+
64
+ ![MN-gon](docs/images/mn-gon.gif)
65
+
66
+ ### Turtle-API
67
+
68
+ ```ruby
69
+ canvas = Drawille::Canvas.new
70
+ canvas.paint do
71
+ # Move your brush with the following commands
72
+ end
73
+ ```
74
+
75
+ ``forward 10`` or ``fw 10``
76
+
77
+ Moves your brush in the current direction (default is 0 which points to the right). Please note that your brush has to be set down to actually draw.
78
+
79
+ ``back 10`` or ``bk 10``
80
+
81
+ Moves your brush in the opposite direction. Please note that your brush has to be set down to actually draw.
82
+
83
+ ``right 90`` or ``rt 90``
84
+
85
+ Turn the direction of the brush by 90 degrees to the right.
86
+
87
+ ``left 90`` or ``lt 90``
88
+
89
+ Turn the direction of the brush by 90 degrees to the left.
90
+
91
+ ``up`` or ``pu``
92
+
93
+ Sets the brush up which means all following operations will have no effect on the canvas itself.
94
+
95
+ ``down`` or ``pd``
96
+
97
+ Sets the drush down which means that moving the brush will draw a stroke.
98
+
99
+ ``move 100, 100`` or ``mv 100, 100``
100
+
101
+ Moves the brush to the position ``x=100`` and ``y=100``.
102
+
103
+ ``line from: [30, 20], to: [100, 100]``
104
+
105
+ A line between the two given points will be drawn. The movement to the starting point will be with an ``up`` brush, but the former state of the brush will be restored after this line.
106
+
107
+ ### ``Drawille::Canvas`` API
108
+
109
+ ``#set(x, y)`` / ``#[x, y] = true``
110
+
111
+ Sets the state of the given position to ``true``, i.e. the ``#frame`` method will render a point at ``[x,y]``.
112
+
113
+ ``#unset(x, y)`` / ``#[x, y] = false``
114
+
115
+ Sets the state of the given position to ``false``, i.e. the ``#frame`` method will _not_ render a point at ``[x,y]``.
116
+
117
+ ``#toggle(x, y)``
118
+
119
+ Toggles the state of the given position.
120
+
121
+ ``#clear``
122
+
123
+ No point will be rendered by the ``#frame`` method.
124
+
125
+ ``#frame``
126
+
127
+ Returns newline-delimited string of the given canvas state. Braille characters are used to represent points. Please note that a single character contains 2 x 4 pixels.
128
+
129
+ ## License
130
+
131
+ MIT License
132
+
133
+ Permission is hereby granted, free of charge, to any person obtaining
134
+ a copy of this software and associated documentation files (the
135
+ "Software"), to deal in the Software without restriction, including
136
+ without limitation the rights to use, copy, modify, merge, publish,
137
+ distribute, sublicense, and/or sell copies of the Software, and to
138
+ permit persons to whom the Software is furnished to do so, subject to
139
+ the following conditions:
140
+
141
+ The above copyright notice and this permission notice shall be
142
+ included in all copies or substantial portions of the Software.
37
143
 
38
- 1. Fork it
39
- 2. Create your feature branch (`git checkout -b my-new-feature`)
40
- 3. Commit your changes (`git commit -am 'Add some feature'`)
41
- 4. Push to the branch (`git push origin my-new-feature`)
42
- 5. Create new Pull Request
144
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
145
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
146
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
147
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
148
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
149
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
150
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Binary file
Binary file
@@ -0,0 +1,19 @@
1
+ require 'drawille'
2
+
3
+ c = Drawille::Canvas.new
4
+
5
+ frame = c.paint do
6
+ move 300, 300
7
+ down
8
+
9
+ 36.times do
10
+ right 10
11
+ 36.times do
12
+ right 10
13
+ forward 8
14
+ end
15
+ end
16
+
17
+ end.frame
18
+
19
+ puts frame
data/lib/drawille.rb CHANGED
@@ -1,89 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require "drawille/version"
3
3
 
4
- module Drawille
5
-
6
- class Canvas
7
-
8
- attr_reader :chars
9
-
10
- PIXEL_MAP = [[0x01, 0x08],
11
- [0x02, 0x10],
12
- [0x04, 0x20],
13
- [0x40, 0x80]]
14
-
15
- BRAILLE_CHAR_OFFSET = 0x2800
16
-
17
- def initialize
18
- clear
19
- end
20
-
21
- def clear
22
- @chars = Hash.new { |h,v| h[v] = Hash.new(0) }
23
- end
24
-
25
- def set x, y
26
- x, y, px, py = convert x, y
27
- @chars[py][px] |= PIXEL_MAP[y % 4][x % 2]
28
- end
29
-
30
- def unset x, y
31
- x, y, px, py = convert x, y
32
- @chars[py][px] &= ~PIXEL_MAP[y % 4][x % 2]
33
- end
34
-
35
- def get x, y
36
- x, y, px, py = convert x, y
37
- @chars[py][px] & PIXEL_MAP[y % 4][x % 2] != 0
38
- end
39
-
40
- def []= x, y, bool
41
- bool ? set(x, y) : unset(x, y)
42
- end
43
-
44
- alias_method :[], :get
45
-
46
- def toggle x, y
47
- x, y, px, py = convert x, y
48
- @chars[py][px] & PIXEL_MAP[y % 4][x % 2] == 0 ? set(x, y) : unset(x, y)
49
- end
50
-
51
- def row y, options={}
52
- row = @chars[y]
53
- min = options[:min_x] || row.keys.min
54
- max = options[:max_x] || row.keys.max
55
-
56
- (min..max).reduce("") { |memo, i| memo << to_braille(row[i] || 0) }
57
- end
58
-
59
- def rows options={}
60
- min = options[:min_y] || @chars.keys.min
61
- max = options[:max_y] || @chars.keys.max
62
- options[:min_x] ||= @chars.reduce([]) { |m,x| m << x.last.keys }.flatten.min
63
- options[:max_x] ||= @chars.reduce([]) { |m,x| m << x.last.keys }.flatten.max
64
- (min..max).map { |i| row i, options }
65
- end
66
-
67
- def frame options={}
68
- rows(options).join("\n")
69
- end
70
-
71
- def char x, y
72
- to_braille @chars[y][x]
73
- end
74
-
75
- private
76
-
77
- def convert x, y
78
- x = x.round
79
- y = y.round
80
- [x, y, (x / 2).floor, (y / 4).floor]
81
- end
82
-
83
- def to_braille x
84
- [BRAILLE_CHAR_OFFSET + x].pack("U*")
85
- end
86
-
87
- end
88
-
89
- end
4
+ require "drawille/canvas"
5
+ require "drawille/brush"
@@ -0,0 +1,91 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Drawille
4
+
5
+ class Brush
6
+
7
+ attr_accessor :canvas
8
+
9
+ def initialize canvas
10
+ @canvas = canvas
11
+ @state = {
12
+ x: 0,
13
+ y: 0,
14
+ up: true,
15
+ rotation: 0
16
+ }
17
+ end
18
+
19
+ def down
20
+ @state[:up] = false
21
+ end
22
+
23
+ def up
24
+ @state[:up] = true
25
+ end
26
+
27
+ def forward length
28
+ theta = ((@state[:rotation]) / 180.0 * Math::PI)
29
+ x = @state[:x] + length * Math::cos(theta)
30
+ y = @state[:y] + length * Math::sin(theta)
31
+
32
+ move x, y
33
+ end
34
+
35
+ def back length
36
+ forward -length
37
+ end
38
+
39
+ def right angle
40
+ @state[:rotation] += angle
41
+ end
42
+
43
+ def left angle
44
+ @state[:rotation] -= angle
45
+ end
46
+
47
+ def move x, y
48
+ unless @state[:up]
49
+ x1 = @state[:x].round
50
+ y1 = @state[:y].round
51
+ x2 = x
52
+ y2 = y
53
+
54
+ xdiff = [x1, x2].max - [x1, x2].min
55
+ ydiff = [y1, y2].max - [y1, y2].min
56
+
57
+ xdir = x1 <= x2 ? 1 : -1
58
+ ydir = y1 <= y2 ? 1 : -1
59
+
60
+ r = [xdiff, ydiff].max
61
+
62
+ (0..r).each do |i|
63
+ x, y = x1, y1
64
+ y += (i.to_f*ydiff)/r*ydir if ydiff > 0
65
+ x += (i.to_f*xdiff)/r*xdir if xdiff > 0
66
+ @canvas.set(x, y)
67
+ end
68
+ end
69
+ @state[:x], @state[:y] = x, y
70
+ end
71
+
72
+ def line coordinates={}
73
+ last_state = @state[:up]
74
+
75
+ up
76
+ move *coordinates[:from]
77
+ down
78
+ move *coordinates[:to]
79
+
80
+ @state[:up] = last_state
81
+ end
82
+
83
+ alias_method :fd, :forward
84
+ alias_method :bk, :back
85
+ alias_method :rt, :right
86
+ alias_method :lt, :left
87
+ alias_method :pu, :up
88
+ alias_method :pd, :down
89
+ alias_method :mv, :move
90
+ end
91
+ end
@@ -0,0 +1,91 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module Drawille
4
+
5
+ class Canvas
6
+
7
+ attr_reader :chars
8
+
9
+ PIXEL_MAP = [[0x01, 0x08],
10
+ [0x02, 0x10],
11
+ [0x04, 0x20],
12
+ [0x40, 0x80]]
13
+
14
+ BRAILLE_CHAR_OFFSET = 0x2800
15
+
16
+ def initialize
17
+ clear
18
+ end
19
+
20
+ def paint &block
21
+ Brush.new(self).instance_eval(&block)
22
+ self
23
+ end
24
+
25
+ def clear
26
+ @chars = Hash.new { |h,v| h[v] = Hash.new(0) }
27
+ end
28
+
29
+ def set x, y
30
+ x, y, px, py = convert x, y
31
+ @chars[py][px] |= PIXEL_MAP[y % 4][x % 2]
32
+ end
33
+
34
+ def unset x, y
35
+ x, y, px, py = convert x, y
36
+ @chars[py][px] &= ~PIXEL_MAP[y % 4][x % 2]
37
+ end
38
+
39
+ def get x, y
40
+ x, y, px, py = convert x, y
41
+ @chars[py][px] & PIXEL_MAP[y % 4][x % 2] != 0
42
+ end
43
+
44
+ def []= x, y, bool
45
+ bool ? set(x, y) : unset(x, y)
46
+ end
47
+
48
+ alias_method :[], :get
49
+
50
+ def toggle x, y
51
+ x, y, px, py = convert x, y
52
+ @chars[py][px] & PIXEL_MAP[y % 4][x % 2] == 0 ? set(x, y) : unset(x, y)
53
+ end
54
+
55
+ def row y, options={}
56
+ row = @chars[y]
57
+ min = options[:min_x] || row.keys.min
58
+ max = options[:max_x] || row.keys.max
59
+
60
+ (min..max).reduce("") { |memo, i| memo << to_braille(row[i] || 0) }
61
+ end
62
+
63
+ def rows options={}
64
+ min = options[:min_y] || @chars.keys.min
65
+ max = options[:max_y] || @chars.keys.max
66
+ options[:min_x] ||= @chars.reduce([]) { |m,x| m << x.last.keys }.flatten.min
67
+ options[:max_x] ||= @chars.reduce([]) { |m,x| m << x.last.keys }.flatten.max
68
+ (min..max).map { |i| row i, options }
69
+ end
70
+
71
+ def frame options={}
72
+ rows(options).join("\n")
73
+ end
74
+
75
+ def char x, y
76
+ to_braille @chars[y][x]
77
+ end
78
+
79
+ private
80
+
81
+ def convert x, y
82
+ x = x.round
83
+ y = y.round
84
+ [x, y, (x / 2).floor, (y / 4).floor]
85
+ end
86
+
87
+ def to_braille x
88
+ [BRAILLE_CHAR_OFFSET + x].pack("U*")
89
+ end
90
+ end
91
+ end
@@ -1,3 +1,3 @@
1
1
  module Drawille
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drawille
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Skirzynski
@@ -35,8 +35,13 @@ files:
35
35
  - LICENSE.txt
36
36
  - README.md
37
37
  - Rakefile
38
+ - docs/images/mn-gon.gif
39
+ - docs/images/sinus.gif
38
40
  - drawille.gemspec
41
+ - examples/mn-gon.rb
39
42
  - lib/drawille.rb
43
+ - lib/drawille/brush.rb
44
+ - lib/drawille/canvas.rb
40
45
  - lib/drawille/version.rb
41
46
  - spec/drawille_spec.rb
42
47
  - spec/sinus.dat