curses-geometry 0.1.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/.gitignore +10 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/curses-geometry.gemspec +40 -0
- data/lib/curses/geometry.rb +18 -0
- data/lib/curses/geometry/glyph.rb +6 -0
- data/lib/curses/geometry/graphic_context.rb +35 -0
- data/lib/curses/geometry/line.rb +190 -0
- data/lib/curses/geometry/polygon.rb +133 -0
- data/lib/curses/geometry/rectangle.rb +43 -0
- data/lib/curses/geometry/version.rb +5 -0
- data/samples/antialiased_line.rb +55 -0
- data/samples/basic.rb +37 -0
- data/samples/line.rb +55 -0
- data/samples/polygon.rb +58 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 965516aafcb4e42f99f9a7f6bb54509944c5016d
|
4
|
+
data.tar.gz: 9f45d7ff2ab5b1aa86ef94c59169ffcd567751cb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: adcf6de236c379c185bf5a721add40433a8b14237f117c19ea37d02749a3b05fa2a192a24209cfd434001cdcfbb5d312a0398b49d6b2e584abb53c16358e0cd1
|
7
|
+
data.tar.gz: a9f113b418a5982e7103a46a3163bc284b236f59f51583dcb3be547202567717afd872e71b2b646517955c4ac1f4c2adc4b092adeb39a7a0118105b49af7186a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at sojastar07@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Julien Jassaud
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Curses::Geometry
|
2
|
+
|
3
|
+
A gem that adds simple graphics primitives to the Ruby curses module.
|
4
|
+
|
5
|
+
Available primitives :
|
6
|
+
- single glyphs
|
7
|
+
- lines and antialiased lines
|
8
|
+
- frames and filled rectangles
|
9
|
+
- arbitrary convex polygons
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'curses-geometry'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install curses-geometry
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sojastar/curses-geometry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
38
|
+
|
39
|
+
## Code of Conduct
|
40
|
+
|
41
|
+
Everyone interacting in the Curses::Geometry project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/curses-geometry/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "curses/geometry"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "curses/geometry/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "curses-geometry"
|
7
|
+
spec.version = Curses::Geometry::VERSION
|
8
|
+
spec.authors = ["Julien Jassaud"]
|
9
|
+
spec.email = ["sojastar07@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Adds some basic graphic primitives to the Curses module.}
|
12
|
+
spec.description = %q{Adds some basic graphic primitives to the Curses module:\n- single glyphs\n- lines and antialiased lines\n- rectangles\n- convex polygons}
|
13
|
+
spec.homepage = "https://rubygems.org/gems/curses-geometry"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
20
|
+
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/sojastar/curses-geometry.git"
|
23
|
+
else
|
24
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
25
|
+
"public gem pushes."
|
26
|
+
end
|
27
|
+
|
28
|
+
# Specify which files should be added to the gem when it is released.
|
29
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
30
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
31
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
32
|
+
end
|
33
|
+
spec.bindir = "exe"
|
34
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ["lib"]
|
36
|
+
|
37
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
38
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
39
|
+
spec.add_runtime_dependency "curses"
|
40
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'curses'
|
2
|
+
require_relative "geometry/version"
|
3
|
+
require_relative "geometry/graphic_context"
|
4
|
+
require_relative "geometry/glyph"
|
5
|
+
require_relative "geometry/line"
|
6
|
+
require_relative "geometry/rectangle"
|
7
|
+
require_relative "geometry/polygon"
|
8
|
+
|
9
|
+
module Curses
|
10
|
+
module Geometry
|
11
|
+
include GraphicContext
|
12
|
+
include Glyph
|
13
|
+
include Line
|
14
|
+
include Rectangle
|
15
|
+
include Polygon
|
16
|
+
end
|
17
|
+
include Geometry
|
18
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module GraphicContext
|
2
|
+
@@graphic_context = { :color_pairs => {},
|
3
|
+
:stroke_glyph => 's',
|
4
|
+
:fill_glyph => 'f' }
|
5
|
+
|
6
|
+
def get_graphic_context
|
7
|
+
@@graphic_context
|
8
|
+
end
|
9
|
+
|
10
|
+
def register_color_pair(name, front_color, back_color)
|
11
|
+
new_pair_index = @@graphic_context[:color_pairs].length + 1
|
12
|
+
init_pair(new_pair_index, front_color, back_color)
|
13
|
+
@@graphic_context[:color_pairs][name] = new_pair_index
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_color_pair(name)
|
17
|
+
attron color_pair(@@graphic_context[:color_pairs][name])
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_stroke_glyph
|
21
|
+
@@graphic_context[:stroke_glyph]
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_stroke_glyph(glyph)
|
25
|
+
@@graphic_context[:stroke_glyph] = glyph
|
26
|
+
end
|
27
|
+
|
28
|
+
def get_fill_glyph
|
29
|
+
@@graphic_context[:fill_glyph]
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_fill_glyph(glyph)
|
33
|
+
@@graphic_context[:fill_glyph] = glyph
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
module Line
|
2
|
+
|
3
|
+
### CONSTANTS :
|
4
|
+
BOTTOM_LEFT_TO_TOP_RIGHT_GLYPH = '/'
|
5
|
+
TOP_LEFT_TO_BOTTOM_RIGHT_GLYPH = '\\'
|
6
|
+
HORIZONTAL_GLYPH = '-'
|
7
|
+
VERTICAL_GLYPH = '|'
|
8
|
+
|
9
|
+
|
10
|
+
### METHODS :
|
11
|
+
def draw_horizontal_line(x1, x2, y)
|
12
|
+
if x1 < x2 then min_x, max_x = x1, x2
|
13
|
+
else min_x, max_x = x2, x1
|
14
|
+
end
|
15
|
+
|
16
|
+
glyph = get_graphic_context[:stroke_glyph]
|
17
|
+
setpos(y, min_x)
|
18
|
+
min_x.upto(max_x) do |x|
|
19
|
+
addch(glyph)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def draw_vertical_line (x, y1, y2)
|
24
|
+
if y1 < y2 then min_y, max_y = y1, y2
|
25
|
+
else min_y, max_y = y2, y1
|
26
|
+
end
|
27
|
+
|
28
|
+
glyph = get_graphic_context[:stroke_glyph]
|
29
|
+
min_y.upto(max_y) do |y|
|
30
|
+
setpos(y,x)
|
31
|
+
addch(glyph)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def draw_line(x1, y1, x2, y2)
|
36
|
+
|
37
|
+
# Set-up of the bresenham algorythm :
|
38
|
+
x, y = x1, y1
|
39
|
+
dx, dy = x2 - x1, y2 - y1
|
40
|
+
|
41
|
+
x_increment = dx > 0 ? 1 : -1
|
42
|
+
y_increment = dy > 0 ? 1 : -1
|
43
|
+
|
44
|
+
|
45
|
+
# Edge cases :
|
46
|
+
if dy == 0 then # horizontal line
|
47
|
+
draw_horizontal_line(x1, x2, y1)
|
48
|
+
return
|
49
|
+
end
|
50
|
+
|
51
|
+
if dx == 0 then # vertical line
|
52
|
+
draw_vertical_line(x1, y1, y2)
|
53
|
+
return
|
54
|
+
end
|
55
|
+
|
56
|
+
# Scanning the line :
|
57
|
+
dx, dy = dx.abs, dy.abs
|
58
|
+
glyph = get_graphic_context[:stroke_glyph]
|
59
|
+
|
60
|
+
# First Point :
|
61
|
+
setpos(y1, x1)
|
62
|
+
addch(glyph)
|
63
|
+
|
64
|
+
# Rest of the Line :
|
65
|
+
if dx > dy then
|
66
|
+
d = dx >> 1
|
67
|
+
dx.times do |i|
|
68
|
+
x += x_increment
|
69
|
+
d += dy
|
70
|
+
|
71
|
+
if d >= dx then
|
72
|
+
d -= dx;
|
73
|
+
y += y_increment;
|
74
|
+
|
75
|
+
setpos(y, x)
|
76
|
+
else
|
77
|
+
ny = y_increment < 0 ? y : y + 1
|
78
|
+
setpos ny, x
|
79
|
+
end
|
80
|
+
|
81
|
+
addch(glyph)
|
82
|
+
end
|
83
|
+
else
|
84
|
+
d = dy >> 1
|
85
|
+
dy.times do |i|
|
86
|
+
y += y_increment
|
87
|
+
d += dx
|
88
|
+
|
89
|
+
if d >= dy then
|
90
|
+
d -= dy
|
91
|
+
x += x_increment
|
92
|
+
end
|
93
|
+
|
94
|
+
setpos(y, x)
|
95
|
+
addch(glyph)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def draw_antialiased_line(x1, y1, x2, y2)
|
101
|
+
|
102
|
+
# Set-up of the bresenham algorythm :
|
103
|
+
x, y = x1, y1
|
104
|
+
dx, dy = x2 - x1, y2 - y1
|
105
|
+
|
106
|
+
x_increment = dx > 0 ? 1 : -1
|
107
|
+
y_increment = dy > 0 ? 1 : -1
|
108
|
+
|
109
|
+
|
110
|
+
# Edge cases :
|
111
|
+
if dy == 0 then # horizontal line
|
112
|
+
previous_stroke_glyph = get_stroke_glyph
|
113
|
+
set_stroke_glyph HORIZONTAL_GLYPH
|
114
|
+
|
115
|
+
draw_horizontal_line(x1, x2, y1)
|
116
|
+
|
117
|
+
set_stroke_glyph previous_stroke_glyph
|
118
|
+
return
|
119
|
+
end
|
120
|
+
|
121
|
+
if dx == 0 then # vertical line
|
122
|
+
previous_stroke_glyph = get_stroke_glyph
|
123
|
+
set_stroke_glyph VERTICAL_GLYPH
|
124
|
+
|
125
|
+
draw_vertical_line(x1, y1, y2)
|
126
|
+
|
127
|
+
set_stroke_glyph previous_stroke_glyph
|
128
|
+
return
|
129
|
+
end
|
130
|
+
|
131
|
+
# Chosing the "stepping" glyph :
|
132
|
+
if dx > 0 then
|
133
|
+
step_glyph = dy > 0 ? TOP_LEFT_TO_BOTTOM_RIGHT_GLYPH : BOTTOM_LEFT_TO_TOP_RIGHT_GLYPH
|
134
|
+
else
|
135
|
+
step_glyph = dy > 0 ? BOTTOM_LEFT_TO_TOP_RIGHT_GLYPH : TOP_LEFT_TO_BOTTOM_RIGHT_GLYPH
|
136
|
+
end
|
137
|
+
|
138
|
+
# Scanning the line :
|
139
|
+
dx, dy = dx.abs, dy.abs
|
140
|
+
|
141
|
+
# First Point :
|
142
|
+
tan = (dy.to_f / dx).abs
|
143
|
+
if ( ( tan > 0.08 ) && ( tan < 5.67 ) ) then
|
144
|
+
glyph = step_glyph
|
145
|
+
else
|
146
|
+
glyph = dx > dy ? HORIZONTAL_GLYPH : VERTICAL_GLYPH
|
147
|
+
end
|
148
|
+
|
149
|
+
setpos(y1, x1)
|
150
|
+
addch(glyph)
|
151
|
+
|
152
|
+
# Rest of the Line :
|
153
|
+
if dx > dy then
|
154
|
+
d = dx >> 1
|
155
|
+
dx.times do |i|
|
156
|
+
x += x_increment
|
157
|
+
d += dy
|
158
|
+
|
159
|
+
if d >= dx then
|
160
|
+
d -= dx;
|
161
|
+
y += y_increment;
|
162
|
+
|
163
|
+
setpos(y, x)
|
164
|
+
addch(step_glyph)
|
165
|
+
else
|
166
|
+
ny = y_increment < 0 ? y : y + 1
|
167
|
+
setpos ny, x
|
168
|
+
addch(HORIZONTAL_GLYPH)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
else
|
172
|
+
d = dy >> 1
|
173
|
+
(dy - 1).times do |i|
|
174
|
+
y += y_increment
|
175
|
+
d += dx
|
176
|
+
|
177
|
+
if d >= dy then
|
178
|
+
d -= dy
|
179
|
+
x += x_increment
|
180
|
+
|
181
|
+
setpos(y, x)
|
182
|
+
addch(glyph)
|
183
|
+
else
|
184
|
+
setpos(y, x)
|
185
|
+
addch(VERTICAL_GLYPH)
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
module Polygon
|
2
|
+
### Module variable :
|
3
|
+
@left_scan = nil
|
4
|
+
@right_scan = nil
|
5
|
+
|
6
|
+
|
7
|
+
### Methods :
|
8
|
+
def scan_polygon_left_edge(x1, y1, x2, y2)
|
9
|
+
|
10
|
+
# Escape horizontal lines :
|
11
|
+
return if (y1 - y2).abs < 0.75
|
12
|
+
|
13
|
+
|
14
|
+
# Calculating useful values for the scanning loop :
|
15
|
+
islope = ( x2 - x1 ).to_f / ( y2 - y1 ).to_f
|
16
|
+
#islope = ( x2 - x1 ) / ( y2 - y1 )
|
17
|
+
|
18
|
+
y_top = y1 > 0.5 ? ( y1 - 0.5 ).floor.to_i : 0.0
|
19
|
+
y_bottom = ( y2 + 0.5 ).floor.to_i
|
20
|
+
|
21
|
+
x = ( y_top + 0.5 - y1 ) * islope + x1 # + 0.5 : the scan line goes through the middle of the pixel
|
22
|
+
|
23
|
+
|
24
|
+
# Scan converting :
|
25
|
+
y_top.downto(y_bottom) do |y|
|
26
|
+
@left_scan[y] = ( x + 0.5 ).floor.to_i
|
27
|
+
x -= islope
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def scan_polygon_right_edge(x1, y1, x2, y2)
|
33
|
+
|
34
|
+
# Escape horizontal lines :
|
35
|
+
return if ( y1 - y2 ).abs < 0.75
|
36
|
+
|
37
|
+
# Calculating useful values for the scanning loop :
|
38
|
+
islope = ( x2 - x1 ).to_f / ( y2 - y1 ).to_f
|
39
|
+
#islope = ( x2 - x1 ) / ( y2 - y1 )
|
40
|
+
|
41
|
+
y_top = y1 > 0.5 ? ( y1 - 0.5 ).floor.to_i : 0.0
|
42
|
+
y_bottom = ( y2 + 0.5 ).floor.to_i
|
43
|
+
|
44
|
+
x = ( y_top + 0.5 - y1 ) * islope + x1 # + 0.5 : the scan line goes through the middle of the pixel
|
45
|
+
|
46
|
+
|
47
|
+
# Scan converting :
|
48
|
+
y_top.downto(y_bottom) do |y|
|
49
|
+
@right_scan[y] = x > 0.5 ? ( x - 0.5 ).floor.to_i : 0.0
|
50
|
+
x -= islope
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def fill_polygon (vertices)
|
56
|
+
|
57
|
+
# Clearing the rasterizer's buffers :
|
58
|
+
@left_scan = Array.new(lines) { 1000000 }
|
59
|
+
@right_scan = Array.new(lines) { 0 }
|
60
|
+
|
61
|
+
# Finding the top and bottom of the polygon :
|
62
|
+
|
63
|
+
# Initializing the loop with the first point y value :
|
64
|
+
max_y_index = 0
|
65
|
+
min_y_index = 0
|
66
|
+
|
67
|
+
max_y = vertices[max_y_index][1].to_i
|
68
|
+
min_y = vertices[min_y_index][1].to_i
|
69
|
+
|
70
|
+
vertices.each.with_index do |vertex,i|
|
71
|
+
# Find the bottom index :
|
72
|
+
if (vertex[1] < min_y) then
|
73
|
+
min_y = vertex[1].to_i
|
74
|
+
min_y_index = i
|
75
|
+
end
|
76
|
+
|
77
|
+
# Find the top index :
|
78
|
+
if vertex[1] > max_y then
|
79
|
+
max_y = vertex[1].to_i
|
80
|
+
max_y_index = i
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
# Discard null height polygons :
|
86
|
+
return if min_y == max_y
|
87
|
+
|
88
|
+
|
89
|
+
# Devise the winding order :
|
90
|
+
previous_point_x = vertices[(max_y_index - 1 + vertices.length) % vertices.length][0].to_i
|
91
|
+
next_point_x = vertices[(max_y_index + 1) % vertices.length][0].to_i
|
92
|
+
winding_order = previous_point_x < next_point_x ? -1 : 1
|
93
|
+
|
94
|
+
|
95
|
+
# Scan the left and right edges of the polygon :
|
96
|
+
|
97
|
+
# Left :
|
98
|
+
i = 0
|
99
|
+
while( vertices[( ( max_y_index + i * winding_order ) + vertices.length) % vertices.length][1] != min_y ) do
|
100
|
+
index = ( ( max_y_index + i * winding_order ) + vertices.length ) % vertices.length
|
101
|
+
next_index = ( ( max_y_index + ( i + 1 ) * winding_order ) + vertices.length ) % vertices.length
|
102
|
+
scan_polygon_left_edge( vertices[index][0],
|
103
|
+
vertices[index][1],
|
104
|
+
vertices[next_index][0],
|
105
|
+
vertices[next_index][1] )
|
106
|
+
|
107
|
+
i += 1
|
108
|
+
end
|
109
|
+
|
110
|
+
# Right :
|
111
|
+
i = 0;
|
112
|
+
while ( vertices[( ( max_y_index - i * winding_order ) + vertices.length ) % vertices.length][1] != min_y ) do
|
113
|
+
index = ( ( max_y_index - i * winding_order ) + vertices.length ) % vertices.length
|
114
|
+
next_index = ( ( max_y_index - ( i + 1 ) * winding_order ) + vertices.length ) % vertices.length
|
115
|
+
scan_polygon_right_edge( vertices[index][0],
|
116
|
+
vertices[index][1],
|
117
|
+
vertices[next_index][0],
|
118
|
+
vertices[next_index][1] )
|
119
|
+
|
120
|
+
i += 1
|
121
|
+
end
|
122
|
+
|
123
|
+
|
124
|
+
# Draw the polygon :
|
125
|
+
min_y.upto(max_y) do |y|
|
126
|
+
@left_scan[y].upto(@right_scan[y]) do |x|
|
127
|
+
setpos(y, x)
|
128
|
+
addch(get_fill_glyph)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Rectangle
|
2
|
+
def stroke_rectangle(x1, y1, x2, y2)
|
3
|
+
draw_horizontal_line(x1, x2, y1)
|
4
|
+
draw_horizontal_line(x1, x2, y2)
|
5
|
+
draw_vertical_line(x1, y1, y2)
|
6
|
+
draw_vertical_line(x2, y1, y2)
|
7
|
+
end
|
8
|
+
|
9
|
+
def fill_rectangle(x1, y1, x2, y2)
|
10
|
+
min_x, max_x = x1 < x2 ? [x1,x2] : [x2,x1]
|
11
|
+
min_y, max_y = y1 < y2 ? [y1,y2] : [y2,y1]
|
12
|
+
|
13
|
+
setpos(min_x, min_y)
|
14
|
+
min_y.upto(max_y) do |y|
|
15
|
+
setpos(y, min_x)
|
16
|
+
min_x.upto(max_x) do |x|
|
17
|
+
addch(get_fill_glyph)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def draw_framed_rectangle(x1, y1, x2, y2)
|
23
|
+
|
24
|
+
# Stroke the frame :
|
25
|
+
draw_horizontal_line(x1, x2, y1)
|
26
|
+
draw_horizontal_line(x1, x2, y2)
|
27
|
+
draw_vertical_line(x1, y1, y2)
|
28
|
+
draw_vertical_line(x2, y1, y2)
|
29
|
+
|
30
|
+
# Fill the frame :
|
31
|
+
min_x, max_x = x1 < x2 ? [x1,x2] : [x2,x1]
|
32
|
+
min_y, max_y = y1 < y2 ? [y1,y2] : [y2,y1]
|
33
|
+
|
34
|
+
setpos(min_x + 1, min_y + 1)
|
35
|
+
(min_y + 1).upto(max_y - 1) do |y|
|
36
|
+
setpos(y, min_x + 1)
|
37
|
+
(min_x + 1).upto(max_x + 1) do |x|
|
38
|
+
addch(get_fill_glyph)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'curses/geometry'
|
2
|
+
include Curses
|
3
|
+
|
4
|
+
Signal.trap("INT") { close_screen; exit 0 }
|
5
|
+
|
6
|
+
init_screen
|
7
|
+
timeout=0
|
8
|
+
begin
|
9
|
+
|
10
|
+
# Screen setup :
|
11
|
+
start_color
|
12
|
+
curs_set 0
|
13
|
+
|
14
|
+
max_x = cols - 1
|
15
|
+
max_y = lines - 1
|
16
|
+
|
17
|
+
# Graphic context ( color ) setup :
|
18
|
+
register_color_pair(:black_on_white, COLOR_WHITE, COLOR_BLACK )
|
19
|
+
register_color_pair(:red_on_blue, COLOR_RED, COLOR_BLUE )
|
20
|
+
register_color_pair(:green_on_yellow, COLOR_GREEN, COLOR_YELLOW )
|
21
|
+
register_color_pair(:magenta_on_cyan, COLOR_MAGENTA, COLOR_CYAN )
|
22
|
+
register_color_pair(:red_on_green, COLOR_RED, COLOR_GREEN )
|
23
|
+
|
24
|
+
colors = [:red_on_blue, :green_on_yellow, :magenta_on_cyan, :red_on_green]
|
25
|
+
glyphs = ['o', '.', '+', '*']
|
26
|
+
|
27
|
+
# Draws a frame :
|
28
|
+
set_color_pair :red_on_blue
|
29
|
+
set_stroke_glyph '='
|
30
|
+
stroke_rectangle 0, 0, max_x, max_y
|
31
|
+
|
32
|
+
# Randomly draws lines :
|
33
|
+
while(true) do
|
34
|
+
# Clear the frame :
|
35
|
+
set_color_pair :black_on_white
|
36
|
+
set_fill_glyph ' '
|
37
|
+
fill_rectangle 1, 1, max_x - 1, max_y - 1
|
38
|
+
|
39
|
+
# Draw stuff inside the frame :
|
40
|
+
set_color_pair(colors.sample)
|
41
|
+
set_stroke_glyph glyphs.sample
|
42
|
+
|
43
|
+
x1 = rand 2..(max_x - 2)
|
44
|
+
y1 = rand 2..(max_y - 2)
|
45
|
+
x2 = rand 2..(max_x - 2)
|
46
|
+
y2 = rand 2..(max_y - 2)
|
47
|
+
|
48
|
+
draw_antialiased_line(x1, y1, x2, y2)
|
49
|
+
|
50
|
+
refresh
|
51
|
+
sleep 1
|
52
|
+
end
|
53
|
+
ensure
|
54
|
+
close_screen
|
55
|
+
end
|
data/samples/basic.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'curses/geometry'
|
2
|
+
include Curses
|
3
|
+
|
4
|
+
Signal.trap("INT") { close_screen; exit 0 }
|
5
|
+
|
6
|
+
init_screen
|
7
|
+
timeout=0
|
8
|
+
begin
|
9
|
+
|
10
|
+
# Screen setup :
|
11
|
+
start_color
|
12
|
+
curs_set 0
|
13
|
+
|
14
|
+
max_x = cols - 1
|
15
|
+
max_y = lines - 1
|
16
|
+
|
17
|
+
# Graphic context ( color ) setup :
|
18
|
+
register_color_pair(:black_on_white, COLOR_WHITE, COLOR_BLACK )
|
19
|
+
register_color_pair(:red_on_blue, COLOR_RED, COLOR_BLUE )
|
20
|
+
register_color_pair(:green_on_yellow, COLOR_GREEN, COLOR_YELLOW )
|
21
|
+
register_color_pair(:magenta_on_cyan, COLOR_MAGENTA, COLOR_CYAN )
|
22
|
+
register_color_pair(:red_on_green, COLOR_RED, COLOR_GREEN )
|
23
|
+
|
24
|
+
colors = [:red_on_blue, :green_on_yellow, :magenta_on_cyan, :red_on_green]
|
25
|
+
glyphs = ['o', '.', '+', '*']
|
26
|
+
|
27
|
+
# Randomly draws a single char :
|
28
|
+
while(true) do
|
29
|
+
set_color_pair colors.sample
|
30
|
+
draw_glyph glyphs.sample, rand(0..max_x), rand(0..max_y)
|
31
|
+
refresh
|
32
|
+
sleep 0.1
|
33
|
+
end
|
34
|
+
|
35
|
+
ensure
|
36
|
+
close_screen
|
37
|
+
end
|
data/samples/line.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'curses/geometry'
|
2
|
+
include Curses
|
3
|
+
|
4
|
+
Signal.trap("INT") { close_screen; exit 0 }
|
5
|
+
|
6
|
+
init_screen
|
7
|
+
timeout=0
|
8
|
+
begin
|
9
|
+
|
10
|
+
# Screen setup :
|
11
|
+
start_color
|
12
|
+
curs_set 0
|
13
|
+
|
14
|
+
max_x = cols - 1
|
15
|
+
max_y = lines - 1
|
16
|
+
|
17
|
+
# Graphic context ( color ) setup :
|
18
|
+
register_color_pair(:black_on_white, COLOR_WHITE, COLOR_BLACK )
|
19
|
+
register_color_pair(:red_on_blue, COLOR_RED, COLOR_BLUE )
|
20
|
+
register_color_pair(:green_on_yellow, COLOR_GREEN, COLOR_YELLOW )
|
21
|
+
register_color_pair(:magenta_on_cyan, COLOR_MAGENTA, COLOR_CYAN )
|
22
|
+
register_color_pair(:red_on_green, COLOR_RED, COLOR_GREEN )
|
23
|
+
|
24
|
+
colors = [:red_on_blue, :green_on_yellow, :magenta_on_cyan, :red_on_green]
|
25
|
+
glyphs = ['o', '.', '+', '*']
|
26
|
+
|
27
|
+
# Draws a frame :
|
28
|
+
set_color_pair :red_on_blue
|
29
|
+
set_stroke_glyph '='
|
30
|
+
stroke_rectangle 0, 0, max_x, max_y
|
31
|
+
|
32
|
+
# Randomly draws lines :
|
33
|
+
while(true) do
|
34
|
+
# Clear the frame :
|
35
|
+
set_color_pair :black_on_white
|
36
|
+
set_fill_glyph ' '
|
37
|
+
fill_rectangle 1, 1, max_x - 1, max_y - 1
|
38
|
+
|
39
|
+
# Draw stuff inside the frame :
|
40
|
+
set_color_pair(colors.sample)
|
41
|
+
set_stroke_glyph glyphs.sample
|
42
|
+
|
43
|
+
x1 = rand 2..(max_x - 2)
|
44
|
+
y1 = rand 2..(max_y - 2)
|
45
|
+
x2 = rand 2..(max_x - 2)
|
46
|
+
y2 = rand 2..(max_y - 2)
|
47
|
+
|
48
|
+
draw_line(x1, y1, x2, y2)
|
49
|
+
|
50
|
+
refresh
|
51
|
+
sleep 0.1
|
52
|
+
end
|
53
|
+
ensure
|
54
|
+
close_screen
|
55
|
+
end
|
data/samples/polygon.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'curses/geometry'
|
2
|
+
include Curses
|
3
|
+
|
4
|
+
Signal.trap("INT") { close_screen; exit 0 }
|
5
|
+
|
6
|
+
init_screen
|
7
|
+
timeout=0
|
8
|
+
begin
|
9
|
+
# Screen setup :
|
10
|
+
start_color
|
11
|
+
curs_set 0
|
12
|
+
|
13
|
+
max_x = cols - 1
|
14
|
+
max_y = lines - 1
|
15
|
+
|
16
|
+
# Graphic context ( color ) setup :
|
17
|
+
register_color_pair(:black_on_white, COLOR_WHITE, COLOR_BLACK )
|
18
|
+
register_color_pair(:red_on_blue, COLOR_RED, COLOR_BLUE )
|
19
|
+
register_color_pair(:green_on_yellow, COLOR_GREEN, COLOR_YELLOW )
|
20
|
+
register_color_pair(:magenta_on_cyan, COLOR_MAGENTA, COLOR_CYAN )
|
21
|
+
register_color_pair(:red_on_green, COLOR_RED, COLOR_GREEN )
|
22
|
+
|
23
|
+
colors = [:red_on_blue, :green_on_yellow, :magenta_on_cyan, :red_on_green]
|
24
|
+
glyphs = ['o', '.', '+', '*']
|
25
|
+
|
26
|
+
# Draws a frame :
|
27
|
+
set_color_pair :red_on_blue
|
28
|
+
set_stroke_glyph '='
|
29
|
+
stroke_rectangle 0, 0, max_x, max_y
|
30
|
+
|
31
|
+
# Randomly draws polygons :
|
32
|
+
while(true) do
|
33
|
+
# Clear the frame :
|
34
|
+
set_color_pair :black_on_white
|
35
|
+
set_fill_glyph ' '
|
36
|
+
fill_rectangle 1, 1, max_x - 1, max_y - 1
|
37
|
+
|
38
|
+
# Draw stuff inside the frame :
|
39
|
+
set_color_pair(colors.sample)
|
40
|
+
set_fill_glyph glyphs.sample
|
41
|
+
|
42
|
+
px1 = rand 1..( max_x / 2 )
|
43
|
+
py1 = rand 1..( max_y / 2 )
|
44
|
+
px2 = rand ( max_x / 2 + 1)..( max_x - 1 )
|
45
|
+
py2 = rand 1..( max_y / 2 )
|
46
|
+
px3 = rand ( max_x / 2 + 1)..( max_x - 1 )
|
47
|
+
py3 = rand ( max_y / 2 + 1)..( max_y - 1 )
|
48
|
+
px4 = rand 1..( max_x / 2 )
|
49
|
+
py4 = rand ( max_y / 2 + 1)..( max_y - 1 )
|
50
|
+
|
51
|
+
fill_polygon([[px1, py1], [px2, py2], [px3, py3], [px4, py4]])
|
52
|
+
|
53
|
+
refresh
|
54
|
+
sleep 0.1
|
55
|
+
end
|
56
|
+
ensure
|
57
|
+
close_screen
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: curses-geometry
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julien Jassaud
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: curses
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Adds some basic graphic primitives to the Curses module:\n- single glyphs\n-
|
56
|
+
lines and antialiased lines\n- rectangles\n- convex polygons
|
57
|
+
email:
|
58
|
+
- sojastar07@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- curses-geometry.gemspec
|
73
|
+
- lib/curses/geometry.rb
|
74
|
+
- lib/curses/geometry/glyph.rb
|
75
|
+
- lib/curses/geometry/graphic_context.rb
|
76
|
+
- lib/curses/geometry/line.rb
|
77
|
+
- lib/curses/geometry/polygon.rb
|
78
|
+
- lib/curses/geometry/rectangle.rb
|
79
|
+
- lib/curses/geometry/version.rb
|
80
|
+
- samples/antialiased_line.rb
|
81
|
+
- samples/basic.rb
|
82
|
+
- samples/line.rb
|
83
|
+
- samples/polygon.rb
|
84
|
+
homepage: https://rubygems.org/gems/curses-geometry
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata:
|
88
|
+
allowed_push_host: https://rubygems.org
|
89
|
+
homepage_uri: https://rubygems.org/gems/curses-geometry
|
90
|
+
source_code_uri: https://github.com/sojastar/curses-geometry.git
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.6.13
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Adds some basic graphic primitives to the Curses module.
|
111
|
+
test_files: []
|