Class: DYI::Shape::Path::ArcCommand

Inherits:
CommandBase show all
Defined in:
lib/dyi/shape/path.rb

Overview

Since:

  • 0.0.0

Instance Attribute Summary collapse

Attributes inherited from CommandBase

#point, #preceding_command

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBase

#absolute?, absolute_commands, #last_point, #preceding_point, #relative?, relative_commands, #start_point, #used_same_command?

Constructor Details

#initialize(relative, preceding_command, rx, ry, rotation, is_large_arc, is_clockwise, point) ⇒ ArcCommand

Returns a new instance of ArcCommand.

Raises:

  • (ArgumentError)

Since:

  • 0.0.0



946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
# File 'lib/dyi/shape/path.rb', line 946

def initialize(relative, preceding_command, rx, ry, rotation, is_large_arc, is_clockwise, point)
  raise ArgumentError, 'preceding_command is nil' if preceding_command.nil?
  @relative = relative
  @preceding_command = preceding_command
  @point = Coordinate.new(point)
  @rotation = rotation
  @is_large_arc = is_large_arc
  @is_clockwise = is_clockwise
  @rx = Length.new(rx).abs
  @ry = Length.new(ry).abs
  l = (modified_mid_point.x.to_f / @rx.to_f) ** 2 + (modified_mid_point.y.to_f / @ry.to_f) ** 2
  if 1 < l
    @rx *= Math.sqrt(l)
    @ry *= Math.sqrt(l)
  end
end

Instance Attribute Details

#rotationObject (readonly)

Since:

  • 0.0.0



944
945
946
# File 'lib/dyi/shape/path.rb', line 944

def rotation
  @rotation
end

#rxObject (readonly)

Since:

  • 0.0.0



944
945
946
# File 'lib/dyi/shape/path.rb', line 944

def rx
  @rx
end

#ryObject (readonly)

Since:

  • 0.0.0



944
945
946
# File 'lib/dyi/shape/path.rb', line 944

def ry
  @ry
end

Class Method Details

.commands(relative, preceding_command, *args) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 0.0.0



1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
# File 'lib/dyi/shape/path.rb', line 1061

def commands(relative, preceding_command, *args)
  raise ArgumentError, "number of arguments must be a multipule of 6" if args.size % 6 != 0
  cmd = preceding_command
  args.each_slice(6).inject([]) do |cmds, ars|
    if ars[0].zero? || ars[1].zero?
      cmds << (cmd = LineCommand.new(relative, cmd, ars.last))
    else
      cmds << (cmd = new(relative, cmd, *ars))
    end
  end
end

Instance Method Details

#center_pointObject

Since:

  • 0.0.0



1010
1011
1012
1013
# File 'lib/dyi/shape/path.rb', line 1010

def center_point
  st_pt = relative? ? Coordinate::ZERO : preceding_point
  Matrix.rotate(rotation).transform(modified_center_point) + (st_pt + point) * 0.5
end

#clockwise?Boolean

Returns:

  • (Boolean)

Since:

  • 0.0.0



967
968
969
# File 'lib/dyi/shape/path.rb', line 967

def clockwise?
  @is_clockwise
end

#instructions_charObject

Since:

  • 0.0.0



1006
1007
1008
# File 'lib/dyi/shape/path.rb', line 1006

def instructions_char
  relative? ? 'a' : 'A'
end

#large_arc?Boolean

Returns:

  • (Boolean)

Since:

  • 0.0.0



963
964
965
# File 'lib/dyi/shape/path.rb', line 963

def large_arc?
  @is_large_arc
end

#to_compatible_commands(preceding_command) ⇒ Object

Since:

  • 0.0.0



971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
# File 'lib/dyi/shape/path.rb', line 971

def to_compatible_commands(preceding_command)
  return LineCommand.new(relative?, preceding_command, point) if rx.zero? || ry.zero?
  division_count = (center_angle / 30.0).ceil
  division_angle = center_angle / division_count * (clockwise? ? 1 : -1)
  current_point = start_angle_point
  compat_commands = []
  division_count.times do |i|
    end_point = if i == division_count - 1
                  end_angle_point
                else
                  Matrix.rotate(division_angle).transform(current_point)
                end
    control_point1 = control_point_of_curve(current_point, division_angle, true)
    control_point2 = control_point_of_curve(end_point, division_angle, false)
    path_point = (i == division_count - 1) ? point : transform_orginal_shape(end_point)
    if relative?
      control_point1 += preceding_point
      control_point2 += preceding_point
      path_point += preceding_point
    end
    preceding_command = CurveCommand.absolute_commands(preceding_command,
                                                       control_point1,
                                                       control_point2,
                                                       path_point).first
    compat_commands << preceding_command
    current_point = end_point
  end
  compat_commands
end

#to_concise_syntax_fragmentsObject

Since:

  • 0.0.0



1001
1002
1003
1004
# File 'lib/dyi/shape/path.rb', line 1001

def to_concise_syntax_fragments
  [used_same_command? ? rx.to_s : instructions_char + rx.to_s,
   ry, rotation, large_arc? ? 1 : 0, clockwise? ? 1 : 0, point.to_s]
end