Class: Point
- Inherits:
-
Object
- Object
- Point
- Defined in:
- app/models/point.rb
Overview
This class describes a 2D coordinates in fortress.
Instance Attribute Summary (collapse)
-
- (Object) x
Returns the value of attribute x.
-
- (Object) y
Returns the value of attribute y.
Class Method Summary (collapse)
-
+ (Array) identities
Return all the identities.
-
+ (Object) random(size)
Create random point.
Instance Method Summary (collapse)
-
- (Object) add(point)
Add parameter point instance to myself.
-
- (Object) add!(point)
Add parameter point instance to myself.
-
- (Boolean) equal?(point)
Compare parameter point instance and myself.
-
- (Object) identity
Set all 1 to x and y.
-
- (Object) identity_random
Set 0 or 1 randomly.
-
- (Object) identity_x
Set 1 to x and 0 to y.
-
- (Object) identity_y
Set 0 to x and 1 to y.
-
- (Point) initialize(x, y)
constructor
Initialize this class.
-
- (Object) sub(point)
Subtract parameter point instance from myself.
-
- (Object) sub!(point)
Subtract parameter point instance from myself.
-
- (Array) sub_identities(point)
Return Array of identity of subtracted point.
-
- (Boolean) zero?
Check whether x and y are 0.
Constructor Details
- (Point) initialize(x, y)
Initialize this class.
9 10 11 12 |
# File 'app/models/point.rb', line 9 def initialize(x, y) @x = x @y = y end |
Instance Attribute Details
- (Object) x
Returns the value of attribute x
3 4 5 |
# File 'app/models/point.rb', line 3 def x @x end |
- (Object) y
Returns the value of attribute y
4 5 6 |
# File 'app/models/point.rb', line 4 def y @y end |
Class Method Details
+ (Array) identities
Return all the identities.
120 121 122 123 124 125 126 127 |
# File 'app/models/point.rb', line 120 def self.identities identities = Array.new identities.push(Point.new(1, 0)) identities.push(Point.new(-1, 0)) identities.push(Point.new(0, 1)) identities.push(Point.new(0, -1)) return identities end |
+ (Object) random(size)
Create random point.
17 18 19 20 21 |
# File 'app/models/point.rb', line 17 def self.random(size) x = rand(size * 2) - (size - 1) y = rand(size * 2) - (size - 1) return Point.new(x, y) end |
Instance Method Details
- (Object) add(point)
This method creates new instance.
Add parameter point instance to myself.
44 45 46 47 48 49 |
# File 'app/models/point.rb', line 44 def add(point) new_point = Marshal.load(Marshal.dump(self)) new_point.x = @x + point.x new_point.y = @y + point.y return new_point end |
- (Object) add!(point)
This is a bang method.
Add parameter point instance to myself.
35 36 37 38 |
# File 'app/models/point.rb', line 35 def add!(point) @x += point.x @y += point.y end |
- (Boolean) equal?(point)
Compare parameter point instance and myself.
26 27 28 29 |
# File 'app/models/point.rb', line 26 def equal?(point) return true if @x == point.x and @y == point.y return false end |
- (Object) identity
Set all 1 to x and y.
79 80 81 82 83 84 85 86 |
# File 'app/models/point.rb', line 79 def identity new_point = Marshal.load(Marshal.dump(self)) new_point.x = 0 new_point.y = 0 new_point.x = @x / @x.abs if @x != 0 new_point.y = @y / @y.abs if @y != 0 return new_point end |
- (Object) identity_random
Set 0 or 1 randomly.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/models/point.rb', line 148 def identity_random new_point = identity if new_point.x != 0 and new_point.y != 0 if rand(2) > 0 new_point.x = 0 else new_point.y = 0 end end return new_point end |
- (Object) identity_x
Set 1 to x and 0 to y.
96 97 98 99 100 |
# File 'app/models/point.rb', line 96 def identity_x new_point = identity new_point.y = 0 return new_point end |
- (Object) identity_y
Set 0 to x and 1 to y.
110 111 112 113 114 |
# File 'app/models/point.rb', line 110 def identity_y new_point = identity new_point.x = 0 return new_point end |
- (Object) sub(point)
This method creates new instance.
Subtract parameter point instance from myself.
64 65 66 67 68 69 |
# File 'app/models/point.rb', line 64 def sub(point) new_point = Marshal.load(Marshal.dump(self)) new_point.x = @x - point.x new_point.y = @y - point.y return new_point end |
- (Object) sub!(point)
This is a bang method.
Subtract parameter point instance from myself.
55 56 57 58 |
# File 'app/models/point.rb', line 55 def sub!(point) @x -= point.x @y -= point.y end |
- (Array) sub_identities(point)
Return Array of identity of subtracted point.
136 137 138 139 140 141 142 |
# File 'app/models/point.rb', line 136 def sub_identities(point) identities = Array.new subtracted_point = sub(point) identities << subtracted_point.identity_x unless subtracted_point.x == 0 identities << subtracted_point.identity_y unless subtracted_point.y == 0 return identities end |
- (Boolean) zero?
Check whether x and y are 0.
162 163 164 165 |
# File 'app/models/point.rb', line 162 def zero? return true if @x == 0 and @y ==0 return false end |