Class: Draught::ArcBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/draught/arc_builder.rb

Defined Under Namespace

Classes: SegmentBuilder

Constant Summary collapse

LARGEST_SEGMENT_RADIANS =

Largest arc segment representable by a single Cubic Bézier is 90º There’s enough jitter in floating point maths that we round the result to 12 d.p. which means we avoid requiring two segments to represent an arc of 90.000000000000000001º

(Math::PI / 2.0).round(12)
CIRCLE_RADIANS =
Math::PI * 2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ArcBuilder

Returns a new instance of ArcBuilder.



35
36
37
38
39
# File 'lib/draught/arc_builder.rb', line 35

def initialize(args = {})
  @radius = args.fetch(:radius)
  @starting_angle = args.fetch(:starting_angle, 0)
  @radians = args.fetch(:radians)
end

Instance Attribute Details

#radiansObject (readonly)

Returns the value of attribute radians.



17
18
19
# File 'lib/draught/arc_builder.rb', line 17

def radians
  @radians
end

#radiusObject (readonly)

Returns the value of attribute radius.



17
18
19
# File 'lib/draught/arc_builder.rb', line 17

def radius
  @radius
end

#starting_angleObject (readonly)

Returns the value of attribute starting_angle.



17
18
19
# File 'lib/draught/arc_builder.rb', line 17

def starting_angle
  @starting_angle
end

Class Method Details

.deg_to_rad(degrees) ⇒ Object



19
20
21
# File 'lib/draught/arc_builder.rb', line 19

def self.deg_to_rad(degrees)
  degrees * (Math::PI / 180)
end

.degrees(args = {}) ⇒ Object



28
29
30
31
32
33
# File 'lib/draught/arc_builder.rb', line 28

def self.degrees(args = {})
  new_args = {radius: args[:radius]}
  new_args[:radians] = deg_to_rad(args.fetch(:angle))
  new_args[:starting_angle] = deg_to_rad(args.fetch(:starting_angle, 0))
  new(new_args)
end

.radians(args = {}) ⇒ Object



23
24
25
26
# File 'lib/draught/arc_builder.rb', line 23

def self.radians(args = {})
  new_args = args.merge(radians: args[:angle])
  new(new_args)
end

Instance Method Details

#curveObject



49
50
51
52
53
54
# File 'lib/draught/arc_builder.rb', line 49

def curve
  @curve ||= Curve.new({
    point: segments.last.end_point,
    cubic_beziers: segments.map { |s| s.cubic_bezier }
  }).transform(transformer)
end

#pathObject



41
42
43
# File 'lib/draught/arc_builder.rb', line 41

def path
  @path ||= Path.new([start_point, curve])
end

#start_pointObject



45
46
47
# File 'lib/draught/arc_builder.rb', line 45

def start_point
  @start_point ||= untranslated_start_point.transform(transformer)
end