Class: Svgcode::SVG::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/svgcode/svg/point.rb

Constant Summary collapse

VALUE_SEP =
/\s?,\s?/
OBJECT_SEP =
/\s+/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str_or_x, y = nil) ⇒ Point

Returns a new instance of Point.



11
12
13
14
15
16
17
18
19
20
# File 'lib/svgcode/svg/point.rb', line 11

def initialize(str_or_x, y = nil)
  if y.nil?
    parts = str_or_x.split(VALUE_SEP)
    @x = Utility.x_to_f(parts.first)
    @y = Utility.x_to_f(parts.last)
  else
    @x = Utility.x_to_f(str_or_x)
    @y = Utility.x_to_f(y)
  end
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



6
7
8
# File 'lib/svgcode/svg/point.rb', line 6

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



6
7
8
# File 'lib/svgcode/svg/point.rb', line 6

def y
  @y
end

Class Method Details

.parse(str) ⇒ Object



85
86
87
# File 'lib/svgcode/svg/point.rb', line 85

def self.parse(str)
  str.split(OBJECT_SEP).collect { |p| Point.new(p.strip) }
end

Instance Method Details

#*(point_or_num) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/svgcode/svg/point.rb', line 46

def *(point_or_num)
  if point_or_num.is_a?(Point)
    Point.new(@x / point_or_num.x, @y / point_or_num.y)
  else
    Point.new(@x / point_or_num, @y / point_or_num)
  end
end

#+(point_or_num) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/svgcode/svg/point.rb', line 30

def +(point_or_num)
  if point_or_num.is_a?(Point)
    Point.new(@x + point_or_num.x, @y + point_or_num.y)
  else
    Point.new(@x + point_or_num, @y + point_or_num)
  end
end

#-(point_or_num) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/svgcode/svg/point.rb', line 38

def -(point_or_num)
  if point_or_num.is_a?(Point)
    Point.new(@x - point_or_num.x, @y - point_or_num.y)
  else
    Point.new(@x - point_or_num, @y - point_or_num)
  end
end

#/(point_or_num) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/svgcode/svg/point.rb', line 54

def /(point_or_num)
  if point_or_num.is_a?(Point)
    Point.new(@x / point_or_num.x, @y / point_or_num.y)
  else
    Point.new(@x / point_or_num, @y / point_or_num)
  end
end

#==(other) ⇒ Object



77
78
79
# File 'lib/svgcode/svg/point.rb', line 77

def ==(other)
  other.is_a?(self.class) && other.x.eql?(@x) && other.y.eql?(@y)
end

#divide_by!(amount) ⇒ Object



62
63
64
65
# File 'lib/svgcode/svg/point.rb', line 62

def divide_by!(amount)
  @x /= amount
  @y /= amount
end

#flip_y(max_y) ⇒ Object



67
68
69
70
71
# File 'lib/svgcode/svg/point.rb', line 67

def flip_y(max_y)
  p = Point.new(@x, @y)
  p.flip_y!(max_y)
  p
end

#flip_y!(max_y) ⇒ Object



73
74
75
# File 'lib/svgcode/svg/point.rb', line 73

def flip_y!(max_y)
  @y = max_y - @y
end

#negate_yObject



22
23
24
# File 'lib/svgcode/svg/point.rb', line 22

def negate_y
  Point.new(@x, -@y)
end

#negate_y!Object



26
27
28
# File 'lib/svgcode/svg/point.rb', line 26

def negate_y!
  @y = -@y
end

#to_sObject



81
82
83
# File 'lib/svgcode/svg/point.rb', line 81

def to_s
  "#{@x},#{@y}"
end