bezier_curver 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/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +8 -0
- data/exe/bezier_curver +7 -0
- data/lib/bezier_curver/calculator.rb +47 -0
- data/lib/bezier_curver/manager.rb +80 -0
- data/lib/bezier_curver/version.rb +5 -0
- data/lib/bezier_curver/window.rb +44 -0
- data/lib/bezier_curver.rb +15 -0
- data/spec/bezier_curver/calclator_spec.rb +77 -0
- data/spec/bezier_curver/manager_spec.rb +109 -0
- data/spec/bezier_curver/window_spec.rb +95 -0
- data/spec/bezier_curver_spec.rb +9 -0
- data/spec/integration/bezier_curver_spec.rb +48 -0
- data/spec/spec_helper.rb +61 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 779641ccd0bcd08b1275af6b54d5ebdc3df1ba5fff66002646014a7f64f8f097
|
4
|
+
data.tar.gz: 9e8212602c4875eccb2df0539dabf5032d27df01ccd45260358a59e4613c995d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 708ed2c5515834f1c915e9db8df40086abfbbd41d16732c6c511942a7947702b53ff18138c0a46feb8cf431b7862b0317864cd799b865c2a29632ace9c4a7cbe
|
7
|
+
data.tar.gz: a9ca67487e8ae8f8f101543261524f82f1a2616bb019d58842fb6e9f2eb21a4ad16709bdbd96af7c1a61bdd9ab984e9095be2096709e2744e5b8fdfe3375684e
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Yudai Takada
|
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,55 @@
|
|
1
|
+
# BezierCurver
|
2
|
+
|
3
|
+
Interactive Bezier curve drawing tool using Ruby2D.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'bezier_curver'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
$ gem install bezier_curver
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Run the tool from command line:
|
28
|
+
|
29
|
+
```bash
|
30
|
+
bezier_curver
|
31
|
+
```
|
32
|
+
|
33
|
+
Or use it in your Ruby code:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
BezierCurver.run
|
37
|
+
```
|
38
|
+
|
39
|
+
## Controls
|
40
|
+
|
41
|
+
- Left Click: Add control point
|
42
|
+
- ESC: Clear all points
|
43
|
+
- Curves are automatically drawn every 5 control points
|
44
|
+
|
45
|
+
## Development
|
46
|
+
|
47
|
+
After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake` to run tests.
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ydah/bezier_curver.
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
The gem is available as open source under the terms of the MIT License.
|
data/Rakefile
ADDED
data/exe/bezier_curver
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BezierCurver
|
4
|
+
class Calculator
|
5
|
+
# Calculate 4th degree Bezier curve (5 control points)
|
6
|
+
def self.calculate_point(p0, p1, p2, p3, p4, t)
|
7
|
+
x = (((1 - t)**4) * p0[0]) +
|
8
|
+
(4 * ((1 - t)**3) * t * p1[0]) +
|
9
|
+
(6 * ((1 - t)**2) * (t**2) * p2[0]) +
|
10
|
+
(4 * (1 - t) * (t**3) * p3[0]) +
|
11
|
+
((t**4) * p4[0])
|
12
|
+
|
13
|
+
y = (((1 - t)**4) * p0[1]) +
|
14
|
+
(4 * ((1 - t)**3) * t * p1[1]) +
|
15
|
+
(6 * ((1 - t)**2) * (t**2) * p2[1]) +
|
16
|
+
(4 * (1 - t) * (t**3) * p3[1]) +
|
17
|
+
((t**4) * p4[1])
|
18
|
+
|
19
|
+
[x, y]
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.generate_curve_points(control_points, segments = 100)
|
23
|
+
points = []
|
24
|
+
|
25
|
+
(0...control_points.length).step(5) do |i|
|
26
|
+
next if i + 4 >= control_points.length
|
27
|
+
|
28
|
+
curve_segment = []
|
29
|
+
(0..segments).each do |j|
|
30
|
+
t = j.to_f / segments
|
31
|
+
point = calculate_point(
|
32
|
+
control_points[i],
|
33
|
+
control_points[i + 1],
|
34
|
+
control_points[i + 2],
|
35
|
+
control_points[i + 3],
|
36
|
+
control_points[i + 4],
|
37
|
+
t
|
38
|
+
)
|
39
|
+
curve_segment << point
|
40
|
+
end
|
41
|
+
points << curve_segment
|
42
|
+
end
|
43
|
+
|
44
|
+
points
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BezierCurver
|
4
|
+
class Manager
|
5
|
+
attr_reader :control_points
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@control_points = []
|
9
|
+
@point_circles = []
|
10
|
+
@curve_lines = []
|
11
|
+
@control_lines = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_control_point(x, y)
|
15
|
+
@control_points << [x, y]
|
16
|
+
|
17
|
+
# Display control point
|
18
|
+
circle = Circle.new(
|
19
|
+
x: x, y: y,
|
20
|
+
radius: 5,
|
21
|
+
color: 'red',
|
22
|
+
z: 10
|
23
|
+
)
|
24
|
+
@point_circles << circle
|
25
|
+
|
26
|
+
draw_control_lines
|
27
|
+
draw_bezier_curves if @control_points.length >= 5
|
28
|
+
end
|
29
|
+
|
30
|
+
def clear_all
|
31
|
+
@control_points.clear
|
32
|
+
|
33
|
+
@point_circles.each(&:remove)
|
34
|
+
@point_circles.clear
|
35
|
+
|
36
|
+
@curve_lines.each(&:remove)
|
37
|
+
@curve_lines.clear
|
38
|
+
|
39
|
+
@control_lines.each(&:remove)
|
40
|
+
@control_lines.clear
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def draw_bezier_curves
|
46
|
+
@curve_lines.each(&:remove)
|
47
|
+
@curve_lines.clear
|
48
|
+
|
49
|
+
curve_segments = Calculator.generate_curve_points(@control_points)
|
50
|
+
|
51
|
+
curve_segments.each do |segment|
|
52
|
+
segment.each_cons(2) do |p1, p2|
|
53
|
+
line = Line.new(
|
54
|
+
x1: p1[0], y1: p1[1],
|
55
|
+
x2: p2[0], y2: p2[1],
|
56
|
+
width: 2,
|
57
|
+
color: 'blue'
|
58
|
+
)
|
59
|
+
@curve_lines << line
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def draw_control_lines
|
65
|
+
@control_lines.each(&:remove)
|
66
|
+
@control_lines.clear
|
67
|
+
|
68
|
+
@control_points.each_cons(2) do |p1, p2|
|
69
|
+
line = Line.new(
|
70
|
+
x1: p1[0], y1: p1[1],
|
71
|
+
x2: p2[0], y2: p2[1],
|
72
|
+
width: 1,
|
73
|
+
color: 'gray',
|
74
|
+
opacity: 0.5
|
75
|
+
)
|
76
|
+
@control_lines << line
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module BezierCurver
|
4
|
+
class Window
|
5
|
+
def initialize
|
6
|
+
setup_window
|
7
|
+
@manager = Manager.new
|
8
|
+
setup_events
|
9
|
+
add_instructions
|
10
|
+
end
|
11
|
+
|
12
|
+
def show
|
13
|
+
::Window.show
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def setup_window
|
19
|
+
::Window.set title: 'Bezier Curve Drawing Tool'
|
20
|
+
::Window.set width: 800
|
21
|
+
::Window.set height: 600
|
22
|
+
::Window.set background: 'black'
|
23
|
+
end
|
24
|
+
|
25
|
+
def setup_events
|
26
|
+
::Window.on :mouse_down do |event|
|
27
|
+
@manager.add_control_point(event.x, event.y) if event.button == :left
|
28
|
+
end
|
29
|
+
|
30
|
+
::Window.on :key_down do |event|
|
31
|
+
@manager.clear_all if event.key == 'escape'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def add_instructions
|
36
|
+
Text.new(
|
37
|
+
'Click: Add control point | ESC: Clear | 4th degree Bezier curve drawn every 5 points',
|
38
|
+
x: 10, y: 10,
|
39
|
+
size: 14,
|
40
|
+
color: 'white'
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby2d'
|
4
|
+
require 'bezier_curver/version'
|
5
|
+
require 'bezier_curver/calculator'
|
6
|
+
require 'bezier_curver/manager'
|
7
|
+
require 'bezier_curver/window'
|
8
|
+
|
9
|
+
module BezierCurver
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
def self.run
|
13
|
+
Window.new.show
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe BezierCurver::Calculator do
|
6
|
+
describe '.calculate_point' do
|
7
|
+
it 'returns start point when t=0' do
|
8
|
+
p0 = [0, 0]
|
9
|
+
p1 = [25, 100]
|
10
|
+
p2 = [50, 100]
|
11
|
+
p3 = [75, 100]
|
12
|
+
p4 = [100, 0]
|
13
|
+
|
14
|
+
result = described_class.calculate_point(p0, p1, p2, p3, p4, 0)
|
15
|
+
expect(result).to eq([0, 0])
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'returns end point when t=1' do
|
19
|
+
p0 = [0, 0]
|
20
|
+
p1 = [25, 100]
|
21
|
+
p2 = [50, 100]
|
22
|
+
p3 = [75, 100]
|
23
|
+
p4 = [100, 0]
|
24
|
+
|
25
|
+
result = described_class.calculate_point(p0, p1, p2, p3, p4, 1)
|
26
|
+
expect(result).to eq([100, 0])
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'returns intermediate point when t=0.5' do
|
30
|
+
p0 = [0, 0]
|
31
|
+
p1 = [0, 100]
|
32
|
+
p2 = [50, 100]
|
33
|
+
p3 = [100, 100]
|
34
|
+
p4 = [100, 0]
|
35
|
+
|
36
|
+
result = described_class.calculate_point(p0, p1, p2, p3, p4, 0.5)
|
37
|
+
expect(result[0]).to be_within(0.1).of(50)
|
38
|
+
expect(result[1]).to be > 50 # Should be elevated due to control points
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '.generate_curve_points' do
|
43
|
+
context 'with less than 5 control points' do
|
44
|
+
it 'returns empty array' do
|
45
|
+
control_points = [[0, 0], [50, 50], [100, 0]]
|
46
|
+
result = described_class.generate_curve_points(control_points)
|
47
|
+
expect(result).to be_empty
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with exactly 5 control points' do
|
52
|
+
it 'returns one curve segment' do
|
53
|
+
control_points = [
|
54
|
+
[0, 0], [25, 100], [50, 100], [75, 100], [100, 0]
|
55
|
+
]
|
56
|
+
result = described_class.generate_curve_points(control_points, 10)
|
57
|
+
|
58
|
+
expect(result.length).to eq(1)
|
59
|
+
expect(result[0].length).to eq(11) # 0 to 10 inclusive
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'with 10 control points' do
|
64
|
+
it 'returns two curve segments' do
|
65
|
+
control_points = [
|
66
|
+
[0, 0], [10, 50], [20, 50], [30, 50], [40, 0],
|
67
|
+
[40, 0], [50, 50], [60, 50], [70, 50], [80, 0]
|
68
|
+
]
|
69
|
+
result = described_class.generate_curve_points(control_points, 10)
|
70
|
+
|
71
|
+
expect(result.length).to eq(2)
|
72
|
+
expect(result[0].length).to eq(11)
|
73
|
+
expect(result[1].length).to eq(11)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe BezierCurver::Manager do
|
6
|
+
# Mock Ruby2D classes for testing
|
7
|
+
before do
|
8
|
+
stub_const('Circle', MockCircle)
|
9
|
+
stub_const('Line', MockLine)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:manager) { described_class.new }
|
13
|
+
|
14
|
+
describe '#initialize' do
|
15
|
+
it 'starts with empty control points' do
|
16
|
+
expect(manager.control_points).to be_empty
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#add_control_point' do
|
21
|
+
it 'adds a control point to the array' do
|
22
|
+
manager.add_control_point(100, 200)
|
23
|
+
|
24
|
+
expect(manager.control_points).to eq([[100, 200]])
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'creates a circle for the control point' do
|
28
|
+
expect(Circle).to receive(:new).with(
|
29
|
+
x: 100, y: 200, radius: 5, color: 'red', z: 10
|
30
|
+
).and_call_original
|
31
|
+
|
32
|
+
manager.add_control_point(100, 200)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'creates control lines between points' do
|
36
|
+
manager.add_control_point(0, 0)
|
37
|
+
|
38
|
+
expect(Line).to receive(:new).with(
|
39
|
+
hash_including(x1: 0, y1: 0, x2: 100, y2: 100)
|
40
|
+
).and_call_original
|
41
|
+
|
42
|
+
manager.add_control_point(100, 100)
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'with 5 control points' do
|
46
|
+
before do
|
47
|
+
allow(BezierCurver::Calculator).to receive(:generate_curve_points)
|
48
|
+
.and_return([[[0, 0], [50, 50], [100, 0]]])
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'draws bezier curves' do
|
52
|
+
# Track line creation
|
53
|
+
blue_line_created = false
|
54
|
+
|
55
|
+
allow(Line).to receive(:new).and_wrap_original do |original, *args|
|
56
|
+
opts = args.first
|
57
|
+
blue_line_created = true if opts[:color] == 'blue' && opts[:width] == 2
|
58
|
+
original.call(*args)
|
59
|
+
end
|
60
|
+
|
61
|
+
5.times { |i| manager.add_control_point(i * 25, 0) }
|
62
|
+
|
63
|
+
expect(blue_line_created).to be true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#clear_all' do
|
69
|
+
before do
|
70
|
+
3.times { |i| manager.add_control_point(i * 50, i * 50) }
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'clears all control points' do
|
74
|
+
manager.clear_all
|
75
|
+
expect(manager.control_points).to be_empty
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'removes all visual elements' do
|
79
|
+
# Collect created objects
|
80
|
+
created_circles = []
|
81
|
+
created_lines = []
|
82
|
+
|
83
|
+
# Mock Circle creation
|
84
|
+
allow(Circle).to receive(:new).and_wrap_original do |original, *args|
|
85
|
+
circle = original.call(*args)
|
86
|
+
created_circles << circle
|
87
|
+
circle
|
88
|
+
end
|
89
|
+
|
90
|
+
# Mock Line creation
|
91
|
+
allow(Line).to receive(:new).and_wrap_original do |original, *args|
|
92
|
+
line = original.call(*args)
|
93
|
+
created_lines << line
|
94
|
+
line
|
95
|
+
end
|
96
|
+
|
97
|
+
# Create a new drawing manager and add points
|
98
|
+
test_manager = described_class.new
|
99
|
+
3.times { |i| test_manager.add_control_point(i * 50, i * 50) }
|
100
|
+
|
101
|
+
# Clear all
|
102
|
+
test_manager.clear_all
|
103
|
+
|
104
|
+
# Verify all elements were removed
|
105
|
+
created_circles.each { |circle| expect(circle).to be_removed }
|
106
|
+
created_lines.each { |line| expect(line).to be_removed }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe BezierCurver::Window do
|
6
|
+
# Mock the global Window module
|
7
|
+
before do
|
8
|
+
window_mock = double('Window')
|
9
|
+
stub_const('Window', window_mock)
|
10
|
+
stub_const('Text', double('Text'))
|
11
|
+
stub_const('BezierCurver::Manager', double('Manager'))
|
12
|
+
|
13
|
+
allow(Window).to receive(:set)
|
14
|
+
allow(Window).to receive(:on)
|
15
|
+
allow(Window).to receive(:show)
|
16
|
+
allow(Text).to receive(:new)
|
17
|
+
allow(BezierCurver::Manager).to receive(:new)
|
18
|
+
.and_return(double('drawing_manager'))
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#initialize' do
|
22
|
+
it 'sets up the window properties' do
|
23
|
+
expect(Window).to receive(:set).with(title: 'Bezier Curve Drawing Tool')
|
24
|
+
expect(Window).to receive(:set).with(width: 800)
|
25
|
+
expect(Window).to receive(:set).with(height: 600)
|
26
|
+
expect(Window).to receive(:set).with(background: 'black')
|
27
|
+
|
28
|
+
described_class.new
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'creates a drawing manager' do
|
32
|
+
expect(BezierCurver::Manager).to receive(:new)
|
33
|
+
described_class.new
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'sets up event handlers' do
|
37
|
+
expect(Window).to receive(:on).with(:mouse_down)
|
38
|
+
expect(Window).to receive(:on).with(:key_down)
|
39
|
+
|
40
|
+
described_class.new
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'adds instruction text' do
|
44
|
+
expect(Text).to receive(:new).with(
|
45
|
+
'Click: Add control point | ESC: Clear | 4th degree Bezier curve drawn every 5 points',
|
46
|
+
hash_including(x: 10, y: 10, size: 14, color: 'white')
|
47
|
+
)
|
48
|
+
|
49
|
+
described_class.new
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#show' do
|
54
|
+
it 'calls Window.show' do
|
55
|
+
window = described_class.new
|
56
|
+
|
57
|
+
expect(Window).to receive(:show)
|
58
|
+
window.show
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'event handling' do
|
63
|
+
let(:drawing_manager) { double('drawing_manager') }
|
64
|
+
let(:window) { described_class.new }
|
65
|
+
|
66
|
+
before do
|
67
|
+
allow(BezierCurver::Manager).to receive(:new)
|
68
|
+
.and_return(drawing_manager)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'handles mouse click events' do
|
72
|
+
mouse_handler = nil
|
73
|
+
allow(Window).to receive(:on).with(:mouse_down) { |&block| mouse_handler = block }
|
74
|
+
|
75
|
+
described_class.new
|
76
|
+
|
77
|
+
expect(drawing_manager).to receive(:add_control_point).with(150, 250)
|
78
|
+
|
79
|
+
event = double('event', button: :left, x: 150, y: 250)
|
80
|
+
mouse_handler.call(event)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'handles escape key events' do
|
84
|
+
key_handler = nil
|
85
|
+
allow(Window).to receive(:on).with(:key_down) { |&block| key_handler = block }
|
86
|
+
|
87
|
+
described_class.new
|
88
|
+
|
89
|
+
expect(drawing_manager).to receive(:clear_all)
|
90
|
+
|
91
|
+
event = double('event', key: 'escape')
|
92
|
+
key_handler.call(event)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe 'Bezier Curve Tool Integration' do
|
6
|
+
before do
|
7
|
+
stub_const('Circle', MockCircle)
|
8
|
+
stub_const('Line', MockLine)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'creates a complete bezier curve from 5 control points' do
|
12
|
+
drawing_manager = BezierCurver::Manager.new
|
13
|
+
|
14
|
+
# Add 5 control points
|
15
|
+
control_points = [
|
16
|
+
[0, 100], [50, 0], [100, 0], [150, 0], [200, 100]
|
17
|
+
]
|
18
|
+
|
19
|
+
control_points.each do |x, y|
|
20
|
+
drawing_manager.add_control_point(x, y)
|
21
|
+
end
|
22
|
+
|
23
|
+
# Verify control points were added
|
24
|
+
expect(drawing_manager.control_points).to eq(control_points)
|
25
|
+
|
26
|
+
# Verify curve calculation
|
27
|
+
curve_points = BezierCurver::Calculator.generate_curve_points(control_points, 10)
|
28
|
+
expect(curve_points).not_to be_empty
|
29
|
+
expect(curve_points[0].first).to eq([0, 100]) # Start point
|
30
|
+
expect(curve_points[0].last).to eq([200, 100]) # End point
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'handles multiple curve segments' do
|
34
|
+
drawing_manager = BezierCurver::Manager.new
|
35
|
+
|
36
|
+
# Add 10 control points (2 curves)
|
37
|
+
10.times do |i|
|
38
|
+
drawing_manager.add_control_point(i * 20, (i % 2) * 100)
|
39
|
+
end
|
40
|
+
|
41
|
+
curve_points = BezierCurver::Calculator.generate_curve_points(
|
42
|
+
drawing_manager.control_points,
|
43
|
+
10
|
44
|
+
)
|
45
|
+
|
46
|
+
expect(curve_points.length).to eq(2) # Two curve segments
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'bezier_curver'
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
# Enable flags like --only-failures and --next-failure
|
8
|
+
config.example_status_persistence_file_path = '.rspec_status'
|
9
|
+
|
10
|
+
# Disable RSpec exposing methods globally on `Module` and `main`
|
11
|
+
config.disable_monkey_patching!
|
12
|
+
|
13
|
+
config.expect_with :rspec do |c|
|
14
|
+
c.syntax = :expect
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Mock Ruby2D classes since we can't create actual windows in tests
|
19
|
+
class MockCircle
|
20
|
+
attr_reader :x, :y, :radius, :color, :z
|
21
|
+
|
22
|
+
def initialize(opts = {})
|
23
|
+
@x = opts[:x]
|
24
|
+
@y = opts[:y]
|
25
|
+
@radius = opts[:radius]
|
26
|
+
@color = opts[:color]
|
27
|
+
@z = opts[:z]
|
28
|
+
@removed = false
|
29
|
+
end
|
30
|
+
|
31
|
+
def remove
|
32
|
+
@removed = true
|
33
|
+
end
|
34
|
+
|
35
|
+
def removed?
|
36
|
+
@removed
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class MockLine
|
41
|
+
attr_reader :x1, :y1, :x2, :y2, :width, :color, :opacity
|
42
|
+
|
43
|
+
def initialize(opts = {})
|
44
|
+
@x1 = opts[:x1]
|
45
|
+
@y1 = opts[:y1]
|
46
|
+
@x2 = opts[:x2]
|
47
|
+
@y2 = opts[:y2]
|
48
|
+
@width = opts[:width]
|
49
|
+
@color = opts[:color]
|
50
|
+
@opacity = opts[:opacity]
|
51
|
+
@removed = false
|
52
|
+
end
|
53
|
+
|
54
|
+
def remove
|
55
|
+
@removed = true
|
56
|
+
end
|
57
|
+
|
58
|
+
def removed?
|
59
|
+
@removed
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bezier_curver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yudai Takada
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: ruby2d
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - "~>"
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0.12'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - "~>"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0.12'
|
26
|
+
description: Interactive Bezier curve drawing tool using Ruby2D. Click to add control
|
27
|
+
points and draw 4th degree Bezier curves.
|
28
|
+
email:
|
29
|
+
- t.yudai92@gmail.com
|
30
|
+
executables:
|
31
|
+
- bezier_curver
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".rspec"
|
36
|
+
- CHANGELOG.md
|
37
|
+
- CODE_OF_CONDUCT.md
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- exe/bezier_curver
|
42
|
+
- lib/bezier_curver.rb
|
43
|
+
- lib/bezier_curver/calculator.rb
|
44
|
+
- lib/bezier_curver/manager.rb
|
45
|
+
- lib/bezier_curver/version.rb
|
46
|
+
- lib/bezier_curver/window.rb
|
47
|
+
- spec/bezier_curver/calclator_spec.rb
|
48
|
+
- spec/bezier_curver/manager_spec.rb
|
49
|
+
- spec/bezier_curver/window_spec.rb
|
50
|
+
- spec/bezier_curver_spec.rb
|
51
|
+
- spec/integration/bezier_curver_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
homepage: https://github.com/ydah/bezier_curver
|
54
|
+
licenses:
|
55
|
+
- MIT
|
56
|
+
metadata:
|
57
|
+
homepage_uri: https://github.com/ydah/bezier_curver
|
58
|
+
source_code_uri: https://github.com/ydah/bezier_curver
|
59
|
+
changelog_uri: https://github.com/ydah/bezier_curver/blob/master/CHANGELOG.md
|
60
|
+
rubygems_mfa_required: 'true'
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.2'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubygems_version: 3.7.0.dev
|
76
|
+
specification_version: 4
|
77
|
+
summary: A tool for drawing Bezier curves with Ruby2D
|
78
|
+
test_files: []
|