bezier 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/Gemfile +2 -0
- data/LICENSE +21 -0
- data/README.md +16 -0
- data/Rakefile +15 -0
- data/bezier.gemspec +20 -0
- data/lib/bezier.rb +4 -0
- data/lib/bezier/bezier.rb +45 -0
- data/lib/bezier/math.rb +29 -0
- data/lib/bezier/painter.rb +65 -0
- data/lib/bezier/point.rb +124 -0
- data/test/helper.rb +2 -0
- data/test/test_bezier.rb +52 -0
- data/test/test_math.rb +24 -0
- data/test/test_painter.rb +32 -0
- data/test/test_point.rb +89 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d3aed71560a6e593488935c03515b4051250972f
|
4
|
+
data.tar.gz: 2f72e82dff6eb6e86f05fc077a2bc4d4af6b5cbd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fdce08938f49c4d32b9cb53cd8bc8952ba1e4c8434d87e6fa34a0a80288c79ec740e07a10f3a1fe9704255fe0c4a0d41db6651605f76f83856db4d2699d06045
|
7
|
+
data.tar.gz: 6ed226250dd075889198c9c17a442e4a68dfbd3cc768000c4a75682c45daa3255d8e80e50d559c8eb756f78779cbcf514009d92a36324db8a015e1451700cc6f
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Yihang Ho
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Bézier
|
2
|
+
A Ruby library to draw Bézier curves.
|
3
|
+
|
4
|
+
# Quick Start
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
require 'bezier'
|
8
|
+
|
9
|
+
p0 = Bezier::Point.new(20, 20)
|
10
|
+
p1 = Bezier::Point.new(20, 100)
|
11
|
+
p2 = Bezier::Point.new(200, 100)
|
12
|
+
p3 = Bezier::Point.new(200, 20)
|
13
|
+
|
14
|
+
bezier = Bezier::Bezier.new(p0, p1, p2, p3)
|
15
|
+
Bezier::Painter.new(bezier.run).save # Check Bézier.png
|
16
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'yard'
|
3
|
+
require 'yard-tomdoc'
|
4
|
+
|
5
|
+
task default: [:test]
|
6
|
+
|
7
|
+
Rake::TestTask.new(:test) do |test|
|
8
|
+
test.libs << 'test'
|
9
|
+
test.pattern = 'test/test_*.rb'
|
10
|
+
test.verbose = true
|
11
|
+
end
|
12
|
+
|
13
|
+
YARD::Rake::YardocTask.new do |t|
|
14
|
+
t.options = ['--plugin', 'tomdoc']
|
15
|
+
end
|
data/bezier.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'bezier'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.license = 'MIT'
|
5
|
+
s.summary = 'Draw a Bézier curve'
|
6
|
+
s.description = 'Library to draw Bézier curves'
|
7
|
+
s.author = 'Yihang Ho'
|
8
|
+
s.email = 'me@yihangho.com'
|
9
|
+
s.homepage = 'https://github.com/yihangho/bezier'
|
10
|
+
|
11
|
+
|
12
|
+
s.files = `git ls-files`.split($/)
|
13
|
+
|
14
|
+
s.add_runtime_dependency('chunky_png', '~> 1.3.1')
|
15
|
+
|
16
|
+
s.add_development_dependency('rake', '~> 10.3.1')
|
17
|
+
s.add_development_dependency('minitest', '~> 5.3.3')
|
18
|
+
s.add_development_dependency('yard', '~> 0.8.7.4')
|
19
|
+
s.add_development_dependency('yard-tomdoc', '~> 0.7.1')
|
20
|
+
end
|
data/lib/bezier.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Bezier
|
2
|
+
class Bezier
|
3
|
+
# Public: Gets/Sets the control points
|
4
|
+
attr_accessor :control_points
|
5
|
+
|
6
|
+
# Public: Construct a new instance of Bezier
|
7
|
+
#
|
8
|
+
# crit_points - The control points of this Bezier curve. They should be
|
9
|
+
# instances of Point.
|
10
|
+
def initialize(*control_points)
|
11
|
+
self.control_points = control_points
|
12
|
+
end
|
13
|
+
|
14
|
+
# Public: Get the degree of this Bezier curve
|
15
|
+
#
|
16
|
+
# Returns a number
|
17
|
+
def degrees
|
18
|
+
control_points.length - 1
|
19
|
+
end
|
20
|
+
|
21
|
+
# Public: Get a point on the Bezier curve
|
22
|
+
#
|
23
|
+
# t - An integer in the interval [0, 1]
|
24
|
+
#
|
25
|
+
# Returns a Point
|
26
|
+
def point_from_t(t)
|
27
|
+
return nil if t < 0 || t > 1
|
28
|
+
output = Point.new(*Array.new(control_points[0].dimensions) { 0 })
|
29
|
+
for i in 0..degrees
|
30
|
+
coefficient = Math.combination(degrees, i) * ((1 - t) ** (degrees - i)) * (t ** i)
|
31
|
+
output += control_points[i] * coefficient
|
32
|
+
end
|
33
|
+
output
|
34
|
+
end
|
35
|
+
|
36
|
+
# Public: "Plot" the Bezier curve.
|
37
|
+
#
|
38
|
+
# num_points - The number of points to produce. (Default: 1000)
|
39
|
+
#
|
40
|
+
# Returns an Array of Points
|
41
|
+
def run(num_points = 1000)
|
42
|
+
Array.new(num_points) { |i| point_from_t(i * 1.0 / (num_points - 1)) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/bezier/math.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module Bezier
|
2
|
+
# Public: Math-related stuff
|
3
|
+
class Math
|
4
|
+
# Public: Calculate the number of combinations
|
5
|
+
#
|
6
|
+
# n - A non-negative Integer, the number of items in a set
|
7
|
+
# r - A non-negative Integer, the number of items to be chosen from the set.
|
8
|
+
# r should be at most as large as n
|
9
|
+
#
|
10
|
+
# Examples
|
11
|
+
#
|
12
|
+
# Bezier::Math.combination(10, 8)
|
13
|
+
# => 45
|
14
|
+
#
|
15
|
+
# Returns an Integer if the combination can be calculated, nil otherwise.
|
16
|
+
def self.combination(n, r)
|
17
|
+
return nil unless n.is_a?(Integer) && r.is_a?(Integer)
|
18
|
+
return nil unless n >= 0 && r >= 0
|
19
|
+
return nil if r > n
|
20
|
+
|
21
|
+
output = 1
|
22
|
+
for i in 1..r
|
23
|
+
output *= (n - i + 1)
|
24
|
+
output /= i
|
25
|
+
end
|
26
|
+
output
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'chunky_png'
|
2
|
+
|
3
|
+
module Bezier
|
4
|
+
# Public: Paint an Array of Points into a PNG file
|
5
|
+
class Painter
|
6
|
+
# Public: The Array of Points to be drawn.
|
7
|
+
attr_accessor :points
|
8
|
+
|
9
|
+
# Public: The path to the output file.
|
10
|
+
attr_accessor :path
|
11
|
+
|
12
|
+
# Public: The padding to be added to the edges of the image.
|
13
|
+
attr_accessor :padding
|
14
|
+
|
15
|
+
# Public: Contruct a new instance of Bezier::Painter.
|
16
|
+
#
|
17
|
+
# Examples
|
18
|
+
#
|
19
|
+
# painter = Bezier::Painter.new do |p|
|
20
|
+
# p.points = [...]
|
21
|
+
# p.path = "Bézier.png"
|
22
|
+
# p.padding = 20
|
23
|
+
# end
|
24
|
+
def initialize
|
25
|
+
self.points = []
|
26
|
+
self.path = "Bézier.png"
|
27
|
+
self.padding = 10
|
28
|
+
yield(self) if block_given?
|
29
|
+
end
|
30
|
+
|
31
|
+
# Public: Save the PNG
|
32
|
+
#
|
33
|
+
# Returns nothing
|
34
|
+
def save
|
35
|
+
image = ChunkyPNG::Image.new(parameters[:width], parameters[:height], ChunkyPNG::Color::WHITE)
|
36
|
+
points.each do |pt|
|
37
|
+
x = pt.x - parameters[:x_offset]
|
38
|
+
y = pt.y - parameters[:y_offset]
|
39
|
+
image[x.round, y.round] = ChunkyPNG::Color::BLACK
|
40
|
+
end
|
41
|
+
p image.class
|
42
|
+
image.save(path)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Internal: Get the parameters associated with this PNG.
|
46
|
+
#
|
47
|
+
# Returns a Hash
|
48
|
+
def get_parameters
|
49
|
+
output = {}
|
50
|
+
|
51
|
+
output[:x_min] = self.points.min_by { |p| p.x }.x
|
52
|
+
output[:x_max] = self.points.max_by { |p| p.x }.x
|
53
|
+
output[:y_min] = self.points.min_by { |p| p.y }.y
|
54
|
+
output[:y_max] = self.points.max_by { |p| p.y }.y
|
55
|
+
|
56
|
+
output[:width] = (output[:x_max] - output[:x_min] + 2 * self.padding).ceil
|
57
|
+
output[:height] = (output[:y_max] - output[:y_min] + 2 * self.padding).ceil
|
58
|
+
|
59
|
+
output[:x_offset] = output[:x_min] - self.padding
|
60
|
+
output[:y_offset] = output[:y_min] - self.padding
|
61
|
+
|
62
|
+
output
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/bezier/point.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
module Bezier
|
2
|
+
# Public: Stuff related to N-dimensional Points
|
3
|
+
class Point
|
4
|
+
# Public: Gets/Sets coordinates
|
5
|
+
attr_accessor :coordinates
|
6
|
+
|
7
|
+
# Public: Construct an instance of Point.
|
8
|
+
#
|
9
|
+
# *coordinates - N numbers describing an N-dimensional Point.
|
10
|
+
#
|
11
|
+
# Examples
|
12
|
+
#
|
13
|
+
# Bezier::Point.new(1, 2, 3)
|
14
|
+
def initialize(*coordinates)
|
15
|
+
self.coordinates = coordinates
|
16
|
+
end
|
17
|
+
|
18
|
+
# Public: Get the dimension.
|
19
|
+
#
|
20
|
+
# Returns a number
|
21
|
+
def dimensions
|
22
|
+
coordinates.length
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: Get the first element of the coordinates
|
26
|
+
#
|
27
|
+
# Returns a number
|
28
|
+
def x
|
29
|
+
coordinates.first
|
30
|
+
end
|
31
|
+
|
32
|
+
# Public: Get the last element of the coordinates
|
33
|
+
#
|
34
|
+
# Returns a number
|
35
|
+
def y
|
36
|
+
coordinates.last
|
37
|
+
end
|
38
|
+
|
39
|
+
# Public: Vector addition.
|
40
|
+
#
|
41
|
+
# another - Another instance of Point having the same dimension as the
|
42
|
+
# current Point
|
43
|
+
#
|
44
|
+
# Examples
|
45
|
+
#
|
46
|
+
# Bezier::Point.new(1, 2, 3) + Bezier::Point.new(4, 5, 6)
|
47
|
+
# => (5, 7, 9)
|
48
|
+
#
|
49
|
+
# Bezier::Point.new(1, 2) + Bezier::Point.new(1, 2, 3)
|
50
|
+
# => nil
|
51
|
+
#
|
52
|
+
# Returns a new Point if addition can be carried out, nil otherwise
|
53
|
+
def +(another)
|
54
|
+
if another.respond_to?(:coordinates) && another.dimensions == dimensions
|
55
|
+
new_coordinates = []
|
56
|
+
coordinates.each_index do |i|
|
57
|
+
new_coordinates << coordinates[i] + another.coordinates[i]
|
58
|
+
end
|
59
|
+
self.class.new(*new_coordinates)
|
60
|
+
else
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Public: Equality test
|
66
|
+
#
|
67
|
+
# another - Another instance of Point to be tested for equality
|
68
|
+
#
|
69
|
+
# Examples
|
70
|
+
#
|
71
|
+
# Bezier::Point.new(1, 2) == Bezier::Point.new(1, 2)
|
72
|
+
# => true
|
73
|
+
#
|
74
|
+
# Bezier::Point.new(1, 2) == Bezier::Point.new(1, 2, 3)
|
75
|
+
# => false
|
76
|
+
#
|
77
|
+
# Returns a Boolean
|
78
|
+
def ==(another)
|
79
|
+
another.respond_to?(:coordinates) && coordinates == another.coordinates
|
80
|
+
end
|
81
|
+
|
82
|
+
# Public: Scalar multiplication
|
83
|
+
#
|
84
|
+
# scalar - A number (instance of Numeric)
|
85
|
+
#
|
86
|
+
# Examples
|
87
|
+
#
|
88
|
+
# Bezier::Point.new(1, 2) * 2
|
89
|
+
# => (2, 4)
|
90
|
+
#
|
91
|
+
# Bezier::Point.new(1, 2) * -1.2
|
92
|
+
# => (-1.2, -2.4)
|
93
|
+
#
|
94
|
+
# Returns a Point if multiplication can be carried out, nil otherwise
|
95
|
+
def *(scalar)
|
96
|
+
if scalar.is_a? Numeric
|
97
|
+
new_coordinates = coordinates.map { |x| scalar * x }
|
98
|
+
self.class.new(*new_coordinates)
|
99
|
+
else
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Public: Calculate the distance between two points
|
105
|
+
#
|
106
|
+
# another - A Point
|
107
|
+
#
|
108
|
+
# Examples
|
109
|
+
#
|
110
|
+
# Bezier::Point.new(3, 4).distance Bezier::Point.new(0, 0)
|
111
|
+
# => 5
|
112
|
+
#
|
113
|
+
# Returns a number if the distance is calculable, nil otherwise
|
114
|
+
def distance(another)
|
115
|
+
return nil unless another.respond_to?(:coordinates) && another.dimensions == dimensions
|
116
|
+
|
117
|
+
square_diff = 0
|
118
|
+
coordinates.each_index do |i|
|
119
|
+
square_diff += (coordinates[i] - another.coordinates[i]) ** 2
|
120
|
+
end
|
121
|
+
::Math.sqrt(square_diff)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
data/test/helper.rb
ADDED
data/test/test_bezier.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Bezier::Bezier do
|
4
|
+
before do
|
5
|
+
coordinates = [[20, 20], [20, 100], [200, 100], [200, 20]]
|
6
|
+
@control_points = Array.new(4) { |i| Bezier::Point.new *coordinates[i] }
|
7
|
+
@bezier = Bezier::Bezier.new *@control_points
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#control_points" do
|
11
|
+
it "should be an Array" do
|
12
|
+
assert_instance_of Array, @bezier.control_points
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should give the correct control points" do
|
16
|
+
assert_equal @bezier.control_points, @control_points
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#degrees" do
|
21
|
+
it "should give the correct degrees" do
|
22
|
+
assert_equal @bezier.degrees, @control_points.length - 1
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#point_from_t" do
|
27
|
+
it "should return P0 when t = 0" do
|
28
|
+
assert_equal @bezier.point_from_t(0), @control_points.first
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return PN when t = 1" do
|
32
|
+
assert_equal @bezier.point_from_t(1), @control_points.last
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return nil when t is not in [0, 1]" do
|
36
|
+
assert_nil @bezier.point_from_t(-1)
|
37
|
+
assert_nil @bezier.point_from_t(100)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should return the correct point elsewhere" do
|
41
|
+
assert_equal @bezier.point_from_t(0.5), Bezier::Point.new(110, 80)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#run" do
|
46
|
+
it "should return an Array of correct length" do
|
47
|
+
assert_instance_of Array, @bezier.run(2)
|
48
|
+
assert_equal @bezier.run(2).length, 2
|
49
|
+
assert_equal @bezier.run(50).length, 50
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/test/test_math.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Bezier::Math do
|
4
|
+
describe "#combination" do
|
5
|
+
it "should return nil either of the inputs is not a non-negative integer" do
|
6
|
+
assert_nil Bezier::Math.combination(-1, 10)
|
7
|
+
assert_nil Bezier::Math.combination(10, -1)
|
8
|
+
assert_nil Bezier::Math.combination(1.5, 2)
|
9
|
+
assert_nil Bezier::Math.combination(2, 1.5)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should be nil if r > n" do
|
13
|
+
assert_nil Bezier::Math.combination(0, 1)
|
14
|
+
assert_nil Bezier::Math.combination(10, 11)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return the correct value" do
|
18
|
+
assert_equal Bezier::Math.combination(5, 2), 10
|
19
|
+
assert_equal Bezier::Math.combination(10, 8), 45
|
20
|
+
assert_equal Bezier::Math.combination(5, 5), 1
|
21
|
+
assert_equal Bezier::Math.combination(0, 0), 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Bezier::Painter do
|
4
|
+
before do
|
5
|
+
coordinates = [[20, 20], [20, 100], [200, 100], [200, 20]]
|
6
|
+
@points = Array.new(4) { |i| Bezier::Point.new *coordinates[i] }
|
7
|
+
@painter = Bezier::Painter.new { |p| p.points = @points }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#get_parameters" do
|
11
|
+
it "should be a Hash" do
|
12
|
+
assert_instance_of Hash, @painter.get_parameters
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should contain correct width and height" do
|
16
|
+
assert_equal @painter.get_parameters[:width], 200
|
17
|
+
assert_equal @painter.get_parameters[:height], 100
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should contain correct min, max values" do
|
21
|
+
assert_equal @painter.get_parameters[:x_min], 20
|
22
|
+
assert_equal @painter.get_parameters[:x_max], 200
|
23
|
+
assert_equal @painter.get_parameters[:y_min], 20
|
24
|
+
assert_equal @painter.get_parameters[:y_max], 100
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should contain correct offsets" do
|
28
|
+
assert_equal @painter.get_parameters[:x_offset], 10
|
29
|
+
assert_equal @painter.get_parameters[:y_offset], 10
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/test/test_point.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe Bezier::Point do
|
4
|
+
before do
|
5
|
+
@point = Bezier::Point.new(1, 2)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "its coordinates" do
|
9
|
+
it "should be an Array" do
|
10
|
+
assert_kind_of Array, @point.coordinates, "Coordinates should be an Array"
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should match what is given" do
|
14
|
+
assert_equal @point.coordinates, [1, 2]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#dimensions" do
|
19
|
+
it "should return the dimension" do
|
20
|
+
assert_equal @point.dimensions, 2
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#distance" do
|
25
|
+
it "should return nil if the other party is not a Point" do
|
26
|
+
assert_nil @point.distance(1)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should return nil if the other party is not of the same dimension" do
|
30
|
+
assert_nil @point.distance(Bezier::Point.new(0))
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should calculate the correct distance" do
|
34
|
+
assert_equal @point.distance(Bezier::Point.new(4, 6)), 5
|
35
|
+
assert_equal @point.distance(Bezier::Point.new(1, 1)), 1
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "equality" do
|
40
|
+
it "should return true if the coordinates are exactly equal" do
|
41
|
+
assert_equal @point, Bezier::Point.new(1, 2)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return false if the other party does not respond to :coordinates" do
|
45
|
+
refute_equal @point, 1
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return false if the other party does not have the same coordinates" do
|
49
|
+
refute_equal @point, Bezier::Point.new(2, 1)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should return false if the other party does not have the same dimenstion" do
|
53
|
+
refute_equal @point, Bezier::Point.new(1, 2, 3)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "addition" do
|
58
|
+
it "should return nil if the other party does not respond to :coordinates" do
|
59
|
+
assert_nil (@point + 1)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return nil if the other party is not of the same dimension" do
|
63
|
+
assert_nil (@point + Bezier::Point.new(1))
|
64
|
+
assert_nil (@point + Bezier::Point.new(1, 2, 3))
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should perform correct addition" do
|
68
|
+
assert_equal (@point + Bezier::Point.new(1, 2)), Bezier::Point.new(2, 4)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "scalar multiplication" do
|
73
|
+
it "should return nil if parameter given is not a number" do
|
74
|
+
assert_nil (@point * "Hello")
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should perform correct multiplicaiton" do
|
78
|
+
assert_equal (@point * 2), Bezier::Point.new(2, 4)
|
79
|
+
assert_equal (@point * -1.2), Bezier::Point.new(-1.2, -2.4)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#x and #y" do
|
84
|
+
it "should return the correct values" do
|
85
|
+
assert_equal @point.x, 1
|
86
|
+
assert_equal @point.y, 2
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bezier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yihang Ho
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: chunky_png
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.3.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.3.1
|
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.3.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 10.3.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 5.3.3
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 5.3.3
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.8.7.4
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.8.7.4
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard-tomdoc
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.7.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.7.1
|
83
|
+
description: Library to draw Bézier curves
|
84
|
+
email: me@yihangho.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files: []
|
88
|
+
files:
|
89
|
+
- ".gitignore"
|
90
|
+
- Gemfile
|
91
|
+
- LICENSE
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- bezier.gemspec
|
95
|
+
- lib/bezier.rb
|
96
|
+
- lib/bezier/bezier.rb
|
97
|
+
- lib/bezier/math.rb
|
98
|
+
- lib/bezier/painter.rb
|
99
|
+
- lib/bezier/point.rb
|
100
|
+
- test/helper.rb
|
101
|
+
- test/test_bezier.rb
|
102
|
+
- test/test_math.rb
|
103
|
+
- test/test_painter.rb
|
104
|
+
- test/test_point.rb
|
105
|
+
homepage: https://github.com/yihangho/bezier
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.2.2
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Draw a Bézier curve
|
129
|
+
test_files: []
|
130
|
+
has_rdoc:
|