blob: 146b0aabbf7c14e918cc420c2dec2e085bcebef1 [file] [log] [blame]
Max Fischc5774342023-06-17 17:43:44 -07001class Point2d
2 attr_accessor :x, :y
3 def initialize(x, y) @x, @y = x, y end
4 def inspect() "<#{@x}, #{@y}>" end
5 def to_s() "#<Point2d:#{@x} #{@y}>" end
6 def +(other) Point2d.new(@x + other.x, @y + other.y) end
7 def -(other) Point2d.new(@x - other.x, @y - other.y) end
8 def scale(d)
9 return Point2d.new(@x * d, @y * d)
10 end
11 def -@()
12 return Point2d.new(-@x, -@y)
13 end
14 def dot(o)
15 return o.x * @x + o.y * @y
16 end
17 def area(o)
18 return o.x * @y - o.y * @x
19 end
20 def normalize()
21 d = Math.sqrt(@x * @x + @y * @y)
22 return Point2d.new(@x / d, @y / d)
23 end
24 def mag() Math.sqrt(@x * @x + @y * @y) end
25 def mag_sqr() @x * @x + @y * @y end
26 def div(d) Point2d.new(@x / d, @y / d) end
27
28 def comp_h(o)
29 a = @x <=> o.x
30 return a if a != 0
31 return @y <=> o.y
32 end
33 def comp_v(o)
34 a = @y <=> o.y
35 return a if a != 0
36 return @x <=> o.x
37 end
38 def rot90()
39 return Point2d.new(-@y, @x)
40 end
41end