Class: CGPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-ui-geometry/cgpoint.rb

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/motion-ui-geometry/cgpoint.rb', line 56

def *(other)
  case other
  when Fixnum, Float
    CGPointMake(x * other, y * other)
  when CGPoint
    CGPointMake(x * other.x, y * other.y)
  when CGSize
    CGPointMake(x * other.width, y * other.height)
  when CGAffineTransform
    apply other
  else
    raise TypeError, "Right operand for * must be "\
      "Fixnum, Float, CGPoint, CGSize or CGAffineTransform "\
      "(got #{other})."
  end
end

#+(other) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/motion-ui-geometry/cgpoint.rb', line 20

def +(other)
  case other
  when CGPoint
    CGPointMake x + other.x, y + other.y
  when CGSize
    CGPointMake x + other.width, y + other.height
  else
    raise TypeError, "Right operand for + must be "\
      "CGPoint or CGSize (got #{other})."
  end
end

#-(other) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/motion-ui-geometry/cgpoint.rb', line 32

def -(other)
  case other
  when CGPoint
    CGPointMake x - other.x, y - other.y
  when CGSize
    CGPointMake x - other.width, y - other.height
  else
    raise TypeError, "Right operand for - must be "\
      "CGPoint or CGSize (got #{other})."
  end
end

#-@Object



44
45
46
# File 'lib/motion-ui-geometry/cgpoint.rb', line 44

def -@
  CGPointMake -x, -y
end

#/(other) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/motion-ui-geometry/cgpoint.rb', line 73

def /(other)
  case other
  when Fixnum, Float
    CGPointMake(x / other, y / other)
  when CGPoint
    CGPointMake(x / other.x, y / other.y)
  when CGSize
    CGPointMake(x / other.width, y / other.height)
  when CGAffineTransform
    apply self.invert
  else
    raise TypeError, "Right operand for / must be "\
      "Fixnum, Float, CGPoint, CGSize or CGAffineTransform "\
      "(got #{other})"
  end
end

#==(other) ⇒ Object



3
4
5
# File 'lib/motion-ui-geometry/cgpoint.rb', line 3

def ==(other)
  other.is_a?(CGPoint) && x == other.x && y == other.y
end

#=~(other) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/motion-ui-geometry/cgpoint.rb', line 7

def =~(other)
  case other
  when CGPoint
    x =~ other.x && y =~ other.y
  when CGSize
    x =~ other.width && y =~ other.height
  else
    raise TypeError, "Right operand for =~ must be "\
      "CGPoint or CGSize (got #{other})."
  end
end

#angleObject



121
122
123
# File 'lib/motion-ui-geometry/cgpoint.rb', line 121

def angle
  Math.atan2 y, x
end

#apply(transform) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/motion-ui-geometry/cgpoint.rb', line 48

def apply(transform)
  unless transform.is_a? CGAffineTransform
    raise TypeError, "Parameter must be a CGAffineTransform, got #{transform}."
  end

  CGPointApplyAffineTransform(self, transform)
end

#clamp_to_rect(rect) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/motion-ui-geometry/cgpoint.rb', line 110

def clamp_to_rect(rect)
  unless rect.is_a? CGRect
    raise TypeError, "Parameter must be a CGRect, got #{rect}."
  end

  p = CGPoint.new
  p.x = x.clamp(rect.origin.x, rect.origin.x + rect.size.width)
  p.y = y.clamp(rect.origin.y, rect.origin.y + rect.size.height)
  p
end

#distance_to(other) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/motion-ui-geometry/cgpoint.rb', line 99

def distance_to(other)
  unless other.is_a? CGPoint
    raise TypeError, "Parameter must be a CGPoint, got #{other}."
  end

  dist_x = x - other.x
  dist_y = y - other.y

  Math.sqrt(dist_x ** 2 + dist_y ** 2).abs
end

#floorObject



95
96
97
# File 'lib/motion-ui-geometry/cgpoint.rb', line 95

def floor
  CGPointMake x.floor, y.floor
end

#roundObject



91
92
93
# File 'lib/motion-ui-geometry/cgpoint.rb', line 91

def round
  CGPointMake x.round, y.round
end

#to_dictionaryObject



129
130
131
# File 'lib/motion-ui-geometry/cgpoint.rb', line 129

def to_dictionary
  CGPointCreateDictionaryRepresentation self
end

#to_sObject



137
138
139
# File 'lib/motion-ui-geometry/cgpoint.rb', line 137

def to_s
  NSStringFromCGPoint self
end

#to_sizeObject



125
126
127
# File 'lib/motion-ui-geometry/cgpoint.rb', line 125

def to_size
  CGSizeMake x, y
end

#to_valueObject



133
134
135
# File 'lib/motion-ui-geometry/cgpoint.rb', line 133

def to_value
  NSValue.valueWithCGPoint self
end