Class: CGPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/geomotion/cg_point.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make(options = {}) ⇒ Object

CGPoint.make(x: 10, y: 30)



3
4
5
# File 'lib/geomotion/cg_point.rb', line 3

def self.make(options = {})
  CGPoint.new(options[:x] || 0, options[:y] || 0)
end

Instance Method Details

#*(scale) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/geomotion/cg_point.rb', line 62

def *(scale)
  case scale
  when Numeric
    return CGPoint.new(self.x * scale, self.y * scale)
  else
    super
  end
end

#+(other) ⇒ Object

operator



53
54
55
56
57
58
59
60
# File 'lib/geomotion/cg_point.rb', line 53

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

#-(other) ⇒ Object



90
91
92
# File 'lib/geomotion/cg_point.rb', line 90

def -(other)
  self.+(-other)
end

#-@Object



86
87
88
# File 'lib/geomotion/cg_point.rb', line 86

def -@
  CGPoint.new(-self.x, -self.y)
end

#/(scale) ⇒ Object

it is tempting to define this as self * (1.0/scale) but floating point errors result in too many errors



73
74
75
76
77
78
79
80
# File 'lib/geomotion/cg_point.rb', line 73

def /(scale)
  case scale
  when Numeric
    return CGPoint.new(self.x / scale, self.y / scale)
  else
    super
  end
end

#==(point) ⇒ Object



82
83
84
# File 'lib/geomotion/cg_point.rb', line 82

def ==(point)
  point.is_a?(CGPoint) && CGPointEqualToPoint(self, point)
end

#angle_to(point) ⇒ Object



46
47
48
49
50
# File 'lib/geomotion/cg_point.rb', line 46

def angle_to(point)
  dx = point.x - self.x
  dy = point.y - self.y
  return Math.atan2(dy, dx)
end

#distance_to(point) ⇒ Object



40
41
42
43
44
# File 'lib/geomotion/cg_point.rb', line 40

def distance_to(point)
  dx = self.x - point.x
  dy = self.y - point.y
  return Math.sqrt(dx**2 + dy**2)
end

#down(dist = 0) ⇒ Object



28
29
30
# File 'lib/geomotion/cg_point.rb', line 28

def down(dist = 0)
  CGPoint.new(self.x, self.y + dist)
end

#inside?(rect) ⇒ Boolean



36
37
38
# File 'lib/geomotion/cg_point.rb', line 36

def inside?(rect)
  CGRectContainsPoint(rect, self)
end

#inspectObject



94
95
96
# File 'lib/geomotion/cg_point.rb', line 94

def inspect
  "#{self.class.name}(#{self.x}, #{self.y})"
end

#left(dist = 0) ⇒ Object

modified points



16
17
18
# File 'lib/geomotion/cg_point.rb', line 16

def left(dist = 0)
  CGPoint.new(self.x - dist, self.y)
end

#rect_of_size(size) ⇒ Object

size = CGSize.make width: 100, height: 100 point = CPPoint.make x:0, y:10 point.rect_of_size(size) # => CGRect([0, 10], [100, 100]) point.rect_of_size([10, 20]) # => CGRect([10, 20], [100, 100])



11
12
13
# File 'lib/geomotion/cg_point.rb', line 11

def rect_of_size(size)
  CGRect.new([self.x, self.y], size)
end

#right(dist = 0) ⇒ Object



20
21
22
# File 'lib/geomotion/cg_point.rb', line 20

def right(dist = 0)
  CGPoint.new(self.x + dist, self.y)
end

#roundObject



32
33
34
# File 'lib/geomotion/cg_point.rb', line 32

def round
  CGPoint.new(self.x.round, self.y.round)
end

#up(dist = 0) ⇒ Object



24
25
26
# File 'lib/geomotion/cg_point.rb', line 24

def up(dist = 0)
  CGPoint.new(self.x, self.y - dist)
end