Class: Geometry::Bezier
- Inherits:
-
Object
- Object
- Geometry::Bezier
- Defined in:
- lib/geometry/bezier.rb
Overview
Bézier curves are like lines, but curvier.
en.wikipedia.org/wiki/Bézier_curve
Constructors
Bezier.new [0,0], [1,1], [2,2] # From control points
Usage
To get a point on the curve for a particular value of t, you can use the subscript operator
bezier[0.5] # => [1,1]
Instance Attribute Summary collapse
- #degree ⇒ Object
-
#points ⇒ Object
Returns the value of attribute points.
Instance Method Summary collapse
- #[](t) ⇒ Object
- #binomial_coefficient(k) ⇒ Object
-
#initialize(*points) ⇒ Bezier
constructor
A new instance of Bezier.
Constructor Details
Instance Attribute Details
#degree ⇒ Object
20 21 22 |
# File 'lib/geometry/bezier.rb', line 20 def degree points.length - 1 end |
#points ⇒ Object
Returns the value of attribute points.
26 27 28 |
# File 'lib/geometry/bezier.rb', line 26 def points @points end |
Instance Method Details
#[](t) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/geometry/bezier.rb', line 39 def [](t) return nil unless (0..1).include?(t) result = Point.zero(points.first.size) points.each_with_index do |v, i| result += v * binomial_coefficient(i) * ((1 - t) ** (degree - i)) * (t ** i) end result end |
#binomial_coefficient(k) ⇒ Object
34 35 36 |
# File 'lib/geometry/bezier.rb', line 34 def binomial_coefficient(k) (0...k).inject(1) {|m,i| (m * (degree - i)) / (i + 1) } end |