-
Notifications
You must be signed in to change notification settings - Fork 1
/
point2d.rb
49 lines (40 loc) · 879 Bytes
/
point2d.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Point2D
attr_accessor :x, :y
def initialize(x, y)
@x = x
@y = y
end
def self.from_string(string)
coords = string.split.map(&:to_i)
Point2D.new(coords[0], coords[1])
end
def +(other)
if other.is_a?(Point2D)
return Point2D.new(@x+other.x, @y+other.y)
end
end
def -(other)
if other.is_a?(Point2D)
return Point2D.new(@x-other.x, @y-other.y)
end
end
def *(other)
if other.is_a?(Numeric)
return Point2D.new(@x*other, @y*other)
end
end
def /(other)
if other.is_a?(Numeric)
return Point2D.new(@x/other, @y/other)
end
end
def coerce(other)
return self, other
end
def to_s
"#{@x} #{@y}"
end
def pretty
"(%d,%d)" % [@x, @y]
end
end