Class: DxfIO::Entity::Support::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/dxf_io/entity/support/point.rb

Constant Summary collapse

START_POINT_GROUP_NUMS =
DxfIO::Constants::START_POINT_GROUP_NUMS
END_POINT_GROUP_NUMS =
DxfIO::Constants::END_POINT_GROUP_NUMS
TYPES =
%i(start end).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, options = {}) ⇒ Point

Returns a new instance of Point.



12
13
14
15
# File 'lib/dxf_io/entity/support/point.rb', line 12

def initialize(x, y, options = {})
  @x, @y = x, y
  @type = TYPES.include?(options[:type]) ? options[:type] : TYPES.first
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/dxf_io/entity/support/point.rb', line 10

def type
  @type
end

#xObject (readonly)

Returns the value of attribute x.



10
11
12
# File 'lib/dxf_io/entity/support/point.rb', line 10

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



10
11
12
# File 'lib/dxf_io/entity/support/point.rb', line 10

def y
  @y
end

Instance Method Details

#*(num) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/dxf_io/entity/support/point.rb', line 81

def *(num)
  if num.is_a? Numeric
    self.class.new(@x * num,
                   @y * num,
                   type: @type)
  else
    raise ArgumentError, 'argument must be Numeric'
  end
end

#+(point) ⇒ Object

binary



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dxf_io/entity/support/point.rb', line 57

def +(point)
  if point.is_a? self.class
    self.class.new(@x + point.x,
                   @y + point.y,
                   type: @type == point.type ? @type : :start)
  elsif point.is_a? Array
    self.class.new(@x + point[0],
                   @y + point[1],
                   type: @type)
  else
    raise ArgumentError, 'point must be an Array or a DxfIO::Entity::Support::Point'
  end
end

#-(point) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/dxf_io/entity/support/point.rb', line 71

def -(point)
  if point.is_a? self.class
    self - point
  elsif point.is_a? Array
    self + point.map(&:-@)
  else
    raise ArgumentError, 'point must be an Array or a DxfIO::Entity::Support::Point'
  end
end

#-@Object

unary



51
52
53
# File 'lib/dxf_io/entity/support/point.rb', line 51

def -@
  self.class.new(-@x, -@y, type: @type)
end

#/(num) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/dxf_io/entity/support/point.rb', line 91

def /(num)
  if num.is_a? Numeric
    self.class.new(@x / num,
                   @y / num,
                   type: @type)
  else
    raise ArgumentError, 'argument must be Numeric'
  end
end

#==(point) ⇒ Object

eq operations



43
44
45
# File 'lib/dxf_io/entity/support/point.rb', line 43

def ==(point)
  to_a == point.to_a
end

#end?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/dxf_io/entity/support/point.rb', line 21

def end?
  @type == :end
end

#rotate_180Object



107
108
109
# File 'lib/dxf_io/entity/support/point.rb', line 107

def rotate_180
  -self
end

#rotate_90Object

geometrical operation (supposed what point is a vector from zero)



103
104
105
# File 'lib/dxf_io/entity/support/point.rb', line 103

def rotate_90
  self.class.new(@y, -@x, type: @type)
end

#start?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/dxf_io/entity/support/point.rb', line 17

def start?
  @type == :start
end

#to_aObject



25
26
27
# File 'lib/dxf_io/entity/support/point.rb', line 25

def to_a
  [@x, @y]
end

#to_dxf_arrayObject



33
34
35
36
37
38
39
# File 'lib/dxf_io/entity/support/point.rb', line 33

def to_dxf_array
  if start?
    [{START_POINT_GROUP_NUMS.first => @x}, {START_POINT_GROUP_NUMS.last => @y}]
  elsif end?
    [{END_POINT_GROUP_NUMS.first => @x}, {END_POINT_GROUP_NUMS.last => @y}]
  end
end

#to_hObject



29
30
31
# File 'lib/dxf_io/entity/support/point.rb', line 29

def to_h
  {x: @x, y: @y}
end