beziercurve 0.6 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/beziercurve.rb +68 -15
  3. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27e5be3280b88a9b7f105e9684bb3e92fad3ed9c
4
- data.tar.gz: a2ec45dfe488b6deb17073ca90c140d5a2acdf10
3
+ metadata.gz: c7550491650757d1ef4313a11191cb788974cae7
4
+ data.tar.gz: 66f165775867bf78cec4db74658de8b7a9bce86c
5
5
  SHA512:
6
- metadata.gz: 3344ae1275b60c6fb196efcd50f0cc30e8aafc07de95a075b4e8b6509c12577dbadc805caf6aee60d82440f1dd4c8b3017c71e4b1d0e4a7f99040d8e55f80a22
7
- data.tar.gz: 65a9f4e29b4d02cb99facde883c9e5a1f5c98f650ef4cb2b610bfe3aee288e15d849ff2a78bfb879c6366ac62003d8e88ad091c0aa163e1c182e98715fed488b
6
+ metadata.gz: a03fad9c836c26f020f3f5b5e859f9f5268b411cc5583d7022e7cf89f975cfedd16611f3cf20736451fa3d23f2ccb1a308ccf1367cdf3d4d3a50631545bc1464
7
+ data.tar.gz: a84249c4189caac7f6f80d93dceebab5e29e1a0068e3d6e6ddd370e7e3d491361085703ea70fcafb059ffdd1c8120b3b7aa1520bb33fdd7afc2e75b3323181c4
data/lib/beziercurve.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # Curve == series of ControlPoints
1
2
  module Bezier
2
3
 
3
4
  class ControlPoint
@@ -15,31 +16,50 @@ module Bezier
15
16
  def inspect
16
17
  return @x, @y
17
18
  end
18
- def to_curve
19
+ # @return [CurvePoint] Returns a ControlPoint, converted to CurvePoint (only naming differences).
20
+ def to_curvepoint
19
21
  CurvePoint.new(self.x, self.y)
20
22
  end
23
+ def to_a
24
+ [self.x, self.y]
25
+ end
21
26
  end
27
+
22
28
  class CurvePoint < ControlPoint
23
- # @return [ControlPoint] the object converted into the expected format.
24
- def to_control
29
+ # @return [ControlPoint] point coordinates on the Bézier curve.
30
+ def to_controlpoint
25
31
  ControlPoint.new(self.x, self.y)
26
32
  end
27
33
  end
34
+
28
35
  class Curve
36
+ # returns hull control points
29
37
  attr_accessor :controlpoints
30
38
 
39
+ # @param controlpoints [Array<ControlPoints, Array(Fixnum, Fixnum)>] list of ControlPoints defining the hull for the Bézier curve. A point can be of class ControlPoint or an Array containig 2 Fixnums, which will be converted to ControlPoint.
40
+ # @return [Curve] a Bézier curve object
41
+ # @example
42
+ # initialize(p1, p2, p3)
43
+ # initialize(p1, [20, 30], p3)
31
44
  def initialize(*controlpoints)
32
45
  # need at least 3 control points
33
- if controlpoints.length < 3
34
- raise 'Cannot create Bézier curve with less than 3 control points'
46
+ # this constraint has to be lifted, to allow adding Curves together like a 1 point curve to a 3 point curve
47
+ if controlpoints.size < 3
48
+ raise ArgumentError, 'Cannot create Bézier curve with less than 3 control points'
35
49
  end
36
50
 
37
- # check for proper types
38
- if controlpoints.find {|p| p.class != ControlPoint} == nil
39
- @controlpoints = controlpoints
40
- end
51
+ @controlpoints = controlpoints.map { |e|
52
+ if e.class == Array
53
+ ControlPoint.new(*e[0..1]) # make sure ControlPoint.new gets no more than 2 arguments. 'e' should contain at least 2 elements here
54
+ elsif e.class == ControlPoint
55
+ e
56
+ else
57
+ raise 'Control points should be type of ControlPoint or Array'
58
+ end
59
+ }
41
60
  end
42
61
 
62
+ # @param point [ControlPoint] addition
43
63
  def add(point)
44
64
  if point.class == ControlPoint
45
65
  @controlpoints << point
@@ -48,9 +68,10 @@ module Bezier
48
68
  end
49
69
  end
50
70
 
71
+ # @param t [CurvePoint]
51
72
  def point_on_curve(t)
52
73
 
53
- def point_on_hull(point1, point2, t) # making this local
74
+ def point_on_hull(point1, point2, t) # making this method local
54
75
  if (point1.class != ControlPoint) or (point2.class != ControlPoint)
55
76
  raise TypeError, 'Both points should be type of ControlPoint'
56
77
  end
@@ -59,7 +80,7 @@ module Bezier
59
80
  return ControlPoint.new(new_x, new_y)
60
81
  end
61
82
 
62
- # imperatively ugly but works, refactor later. point_on_curve and point_on_hull should be one method
83
+ # imperatively ugly, but works, refactor later. point_on_curve and point_on_hull should be one method
63
84
  ary = @controlpoints
64
85
  return ary if ary.length <= 1 # zero or one element as argument, return unmodified
65
86
 
@@ -71,10 +92,41 @@ module Bezier
71
92
  end
72
93
  ary = temp
73
94
  end
74
- return temp[0].to_curve
95
+ return temp[0].to_curvepoint
96
+ end
97
+
98
+ def pascaltriangle(nth_line)
99
+ # @param n [Fixnum] Ye' olde factorial function
100
+ # @todo this is slow, should be rewritten
101
+ # @return [Array] an array of the specified line from the Pascal triangle
102
+ # @example
103
+ # > fact(5)
104
+ # >
105
+ def fact(n)
106
+ (1..n).reduce(:*)
107
+ end
108
+
109
+ # @param n [Fixnum]
110
+ # @param k [Fixnum]
111
+ def binomial(n,k)
112
+ return 1 if n-k <= 0
113
+ return 1 if k <= 0
114
+ fact(n) / ( fact(k) * fact( n - k ) )
115
+ end
116
+ (0..nth_line).map { |e| binomial(nth_line, e) }
75
117
  end
76
118
 
77
- def display_points # just a helper, for quickly put CotrolPOints to STDOUT in a gnuplottable format
119
+ def point_on_curve_binom(t)
120
+
121
+ # locally scoped, we don't need it outside of point_on_curve_binom
122
+
123
+ coeffs = pascaltriangle(self.order)
124
+ coeffs.reduce { |memo, obj|
125
+ memo += t**obj * (1-t)** (n - obj)
126
+ }
127
+ end
128
+
129
+ def display_points # just a helper, for quickly put ControlPoints to STDOUT in a gnuplottable format
78
130
  @controlpoints.map{|point| puts "#{point.x} #{point.y}"}
79
131
  end
80
132
 
@@ -85,14 +137,15 @@ module Bezier
85
137
  loop do
86
138
  yielder.yield(number)
87
139
  point_position += delta_t.to_f
88
- raise StopIteration if point_position > 1
140
+ raise StopIteration if point_position > 1.0
89
141
  number = point_on_curve(point_position)
90
142
  end
91
143
  end
92
144
  end
93
145
 
94
146
  def order
95
- @controlpoints.length
147
+ @controlpoints.size
96
148
  end
97
149
  end
150
+
98
151
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beziercurve
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: '0.8'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Földes László
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-19 00:00:00.000000000 Z
11
+ date: 2014-01-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Creates a Bézier curve by its control points. Implemented with de Casteljau
14
14
  method.
@@ -28,17 +28,17 @@ require_paths:
28
28
  - lib
29
29
  required_ruby_version: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 2.0.0
34
34
  required_rubygems_version: !ruby/object:Gem::Requirement
35
35
  requirements:
36
- - - '>='
36
+ - - ">="
37
37
  - !ruby/object:Gem::Version
38
38
  version: '0'
39
39
  requirements: []
40
40
  rubyforge_project:
41
- rubygems_version: 2.1.10
41
+ rubygems_version: 2.2.2
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Create and analyze Bézier curves