Class: DXF::Bezier

Inherits:
Spline show all
Defined in:
lib/dxf/entity.rb

Constant Summary

Constants inherited from Entity

Entity::TypeError

Instance Attribute Summary collapse

Attributes inherited from Spline

#knots

Attributes inherited from Entity

#handle, #layer

Instance Method Summary collapse

Methods inherited from Entity

new, #parse_pair

Methods included from ClusterFactory

included

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

#degreeObject



136
137
138
# File 'lib/dxf/entity.rb', line 136

def degree
    points.length - 1
end

#pointsObject

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

Parameters:

  • t (Float)

    the input parameter



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