Class: DXF::Bezier
Constant Summary
Constants inherited from Entity
Instance Attribute Summary collapse
- #degree ⇒ Object
-
#points ⇒ Object
Returns the value of attribute points.
Attributes inherited from Spline
Attributes inherited from Entity
Instance Method Summary collapse
- #[](t) ⇒ Object
- #binomial_coefficient(k) ⇒ Object
-
#initialize(*points) ⇒ Bezier
constructor
A new instance of Bezier.
-
#lines(count = 20) ⇒ Object
Convert the Bezier into the given number of line segments.
Methods inherited from Entity
Methods included from ClusterFactory
Constructor Details
#initialize(*points) ⇒ Bezier
Returns a new instance of Bezier.
144 145 146 |
# File 'lib/dxf/entity.rb', line 144 def initialize(*points) @points = points.map {|v| Geometry::Point[v]} end |
Instance Attribute Details
#degree ⇒ Object
136 137 138 |
# File 'lib/dxf/entity.rb', line 136 def degree points.length - 1 end |
#points ⇒ Object
Returns the value of attribute points.
142 143 144 |
# File 'lib/dxf/entity.rb', line 142 def points @points end |
Instance Method Details
#[](t) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/dxf/entity.rb', line 155 def [](t) return nil unless (0..1).include?(t) result = Geometry::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
150 151 152 |
# File 'lib/dxf/entity.rb', line 150 def binomial_coefficient(k) (0...k).inject(1) {|m,i| (m * (degree - i)) / (i + 1) } end |
#lines(count = 20) ⇒ Object
Convert the DXF::Bezier into the given number of line segments
165 166 167 |
# File 'lib/dxf/entity.rb', line 165 def lines(count=20) (0..1).step(1.0/count).map {|t| self[t]}.each_cons(2).map {|a,b| Line.new a, b} end |