lib/geomotion/cg_point.rb in geomotion-0.5 vs lib/geomotion/cg_point.rb in geomotion-0.7.0

- old
+ new

@@ -10,24 +10,62 @@ # point.rect_of_size([10, 20]) # => CGRect([10, 20], [100, 100]) def rect_of_size(size) CGRect.new([self.x, self.y], size) end + # modified points + def left(dist = 0) + CGPoint.new(self.x - dist, self.y) + end + + def right(dist = 0) + CGPoint.new(self.x + dist, self.y) + end + + def up(dist = 0) + CGPoint.new(self.x, self.y - dist) + end + + def down(dist = 0) + CGPoint.new(self.x, self.y + dist) + end + + def round + CGPoint.new(self.x.round, self.y.round) + end + + def inside?(rect) + CGRectContainsPoint(rect, self) + end + + # operator def +(other) case other when CGSize return self.rect_of_size(other) when CGPoint return CGPoint.new(self.x + other.x, self.y + other.y) end end - def round - CGPoint.new(self.x.round, self.y.round) + def *(scale) + case scale + when Numeric + return CGPoint.new(self.x * scale, self.y * scale) + else + super + end end - def inside?(rect) - CGRectContainsPoint(rect, self) + # it is tempting to define this as self * (1.0/scale) but floating point + # errors result in too many errors + def /(scale) + case scale + when Numeric + return CGPoint.new(self.x / scale, self.y / scale) + else + super + end end def ==(point) point.is_a?(CGPoint) && CGPointEqualToPoint(self, point) end