Class: DemCurves::ControlPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/core/control-point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loc) ⇒ ControlPoint

Returns a new instance of ControlPoint.



12
13
14
15
16
# File 'lib/core/control-point.rb', line 12

def initialize(loc)
  @loc = Vector.elements(loc)
  @path_elements = []
  @constraints = []
end

Instance Attribute Details

#locObject (readonly)

this class is necessary to let other curves and path elements modify points, it works much better than calling “notify movement” functions every time a control point moves, this comes in handy when you want a curve to use another curve’s end point as a starting point.



10
11
12
# File 'lib/core/control-point.rb', line 10

def loc
  @loc
end

Class Method Details

.[](*loc) ⇒ Object



103
104
105
# File 'lib/core/control-point.rb', line 103

def ControlPoint.[](*loc)
  return ControlPoint.new loc
end

Instance Method Details

#add_constraint(constraint) ⇒ Object



92
93
94
95
96
# File 'lib/core/control-point.rb', line 92

def add_constraint(constraint)
  unless @constraints.include? constraint
    @constraints << constraint
  end
end

#add_path_element(path_element) ⇒ Object



18
19
20
# File 'lib/core/control-point.rb', line 18

def add_path_element(path_element)
  @path_elements << path_element
end

#clear_constraintsObject



98
99
100
# File 'lib/core/control-point.rb', line 98

def clear_constraints
  @constraints = []
end

#move_to(destination) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/core/control-point.rb', line 50

def move_to(destination)
  old_pos = @loc
  replace destination
  
  new_pos = @loc
  rel = new_pos - old_pos
  
  @constraints.each do |constraint|
    constraint.notify self, self, {:new_pos => new_pos, :old_pos => old_pos, :rel => rel}
  end
end

#notify_to_move(orig_src, src_constraint, params) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/core/control-point.rb', line 68

def notify_to_move(orig_src, src_constraint, params)
  unless orig_src == self
    # Safety measure, it avoids infinite recursion, but produces weird results
    # with cyclic constraint structures.
    old_pos = @loc
    if params.include? :new_pos
      replace params[:new_pos]
    elsif params.include? :rel
      replace params[:rel] + @loc
    else
      return
    end
    
    new_pos = @loc
    rel = new_pos - old_pos
    
    @constraints.each do |constraint|
      unless constraint == src_constraint
        constraint.notify self, orig_src, {:new_pos => new_pos, :old_pos => old_pos, :rel => rel}
      end
    end
  end
end

#replace(other) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/core/control-point.rb', line 22

def replace(other)
  old_pos = @loc
  case other
  when ControlPoint
    @loc = Vector.elements(other.loc)
  when Vector, Array
    unless other.size == 2
      raise "Wrong number of dimensions, must be [x, y]"
    end
    @loc = Vector.elements(other)
  else
    raise "Argument is instance of #{other.class}! Replacement argument must be an instance of Vector, Array or ControlPoint."
  end
    
    @path_elements.each do |path_element|
      path_element.generate
  end
end

#rotate_around(pivot_ctl, angle) ⇒ Object



62
63
64
65
66
# File 'lib/core/control-point.rb', line 62

def rotate_around(pivot_ctl, angle)
  offset = @loc - pivot_ctl.loc
  new_offset = Matrix[[Math.cos(angle), -Math.sin(angle)], [Math.sin(angle), Math.cos(angle)]] * offset
  replace new_offset + pivot_ctl.loc
end

#shift(rel) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/core/control-point.rb', line 41

def shift(rel)
  unless rel.size == 2
    raise "Wrong number of dimensions, must be [x, y]"
  end
  
  old_pos = @loc
  move_to @loc + Vector.elements(rel)
end