Class: Point

Inherits:
Object
  • Object
show all
Defined in:
app/models/point.rb

Overview

This class describes a 2D coordinates in fortress.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Point) initialize(x, y)

Initialize this class.

Parameters:

  • x (Integer)

    x-coordinate

  • y (Integer)

    y-coordinate



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.

Examples:

(1, 0), (-1,0), (0, 1), (0, -1)

Returns:

  • (Array)

    identities Point.



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.

Parameters:

  • size (Integer)

    length of a side

Returns:

  • (Object)

    Point instance.



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)

Note:

This method creates new instance.

Add parameter point instance to myself.

Parameters:

  • point (Object)

    Point.

Returns:

  • (Object)

    point Point.



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)

Note:

This is a bang method.

Add parameter point instance to myself.

Parameters:

  • point (Object)

    Point.

Returns:

  • (Object)

    point Point.



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.

Parameters:

  • point (Object)

    Point.

Returns:

  • (Boolean)


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.

Examples:

(22, 33) -> (1, 1)
(-22, 33) -> (-1, 1)
(22, -33) -> (1, -1)
(0, 33) -> (0, 1)
(22, 0) -> (1, 0)

Returns:

  • (Object)

    point Point.



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.

Examples:

(0, 0), (1, 0), (0, 1), (1, 1)

Returns:

  • (Object)

    point Point.



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.

Examples:

(22, 33) -> (1, 0)
(-22, 33) -> (-1, 0)
(22, -33) -> (1, 0)
(0, 33) -> (0, 0)
(22, 0) -> (1, 0)

Returns:

  • (Object)

    point Point.



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.

Examples:

(22, 33) -> (0, 1)
(-22, 33) -> (0, 1)
(22, -33) -> (0, -1)
(0, 33) -> (0, 1)
(22, 0) -> (0, 0)

Returns:

  • (Object)

    point Point.



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)

Note:

This method creates new instance.

Subtract parameter point instance from myself.

Parameters:

  • point (Object)

    Point.

Returns:

  • (Object)

    point Point.



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)

Note:

This is a bang method.

Subtract parameter point instance from myself.

Parameters:

  • point (Object)

    Point.

Returns:

  • (Object)

    point Point.



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.

Examples:

(22, 33) -> [(1, 0), (0, 1)]
(0, 33) -> [(0, 1)]
(22, 0) -> [(1, 0)]
(0, 0) -> nil

Returns:

  • (Array)

    identities 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.

Returns:

  • (Boolean)


162
163
164
165
# File 'app/models/point.rb', line 162

def zero?
  return true if @x == 0 and @y ==0
  return false
end