Class: Svgcode::SVG::Point
- Inherits:
-
Object
- Object
- Svgcode::SVG::Point
- Defined in:
- lib/svgcode/svg/point.rb
Constant Summary collapse
- VALUE_SEP =
/\s?,\s?/- OBJECT_SEP =
/\s+/
Instance Attribute Summary collapse
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Class Method Summary collapse
Instance Method Summary collapse
- #*(point_or_num) ⇒ Object
- #+(point_or_num) ⇒ Object
- #-(point_or_num) ⇒ Object
- #/(point_or_num) ⇒ Object
- #==(other) ⇒ Object
- #divide_by!(amount) ⇒ Object
- #flip_y!(max_y) ⇒ Object
-
#initialize(str_or_x, y = nil) ⇒ Point
constructor
A new instance of Point.
- #negate_y ⇒ Object
- #negate_y! ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(str_or_x, y = nil) ⇒ Point
Returns a new instance of Point.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/svgcode/svg/point.rb', line 9 def initialize(str_or_x, y = nil) if y.nil? parts = str_or_x.split(VALUE_SEP) @x = parts.first.to_f @y = parts.last.to_f else @x = str_or_x.to_f @y = y.to_f end end |
Instance Attribute Details
#x ⇒ Object (readonly)
Returns the value of attribute x.
4 5 6 |
# File 'lib/svgcode/svg/point.rb', line 4 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
4 5 6 |
# File 'lib/svgcode/svg/point.rb', line 4 def y @y end |
Class Method Details
.parse(str) ⇒ Object
77 78 79 |
# File 'lib/svgcode/svg/point.rb', line 77 def self.parse(str) str.split(OBJECT_SEP).collect { |p| Point.new(p.strip) } end |
Instance Method Details
#*(point_or_num) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/svgcode/svg/point.rb', line 44 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
28 29 30 31 32 33 34 |
# File 'lib/svgcode/svg/point.rb', line 28 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
36 37 38 39 40 41 42 |
# File 'lib/svgcode/svg/point.rb', line 36 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
52 53 54 55 56 57 58 |
# File 'lib/svgcode/svg/point.rb', line 52 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
69 70 71 |
# File 'lib/svgcode/svg/point.rb', line 69 def ==(other) other.is_a?(self.class) && other.x.eql?(@x) && other.y.eql?(@y) end |
#divide_by!(amount) ⇒ Object
60 61 62 63 |
# File 'lib/svgcode/svg/point.rb', line 60 def divide_by!(amount) @x /= amount @y /= amount end |
#flip_y!(max_y) ⇒ Object
65 66 67 |
# File 'lib/svgcode/svg/point.rb', line 65 def flip_y!(max_y) @y = max_y - @y end |
#negate_y ⇒ Object
20 21 22 |
# File 'lib/svgcode/svg/point.rb', line 20 def negate_y Point.new(@x, -@y) end |
#negate_y! ⇒ Object
24 25 26 |
# File 'lib/svgcode/svg/point.rb', line 24 def negate_y! @y = -@y end |
#to_s ⇒ Object
73 74 75 |
# File 'lib/svgcode/svg/point.rb', line 73 def to_s "#{@x},#{@y}" end |