Class: Coordinates

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/software_challenge_client/coordinates.rb

Overview

Einfache kartesische Koordinaten

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Coordinates

Erstellt neue leere Koordinaten.



9
10
11
12
# File 'lib/software_challenge_client/coordinates.rb', line 9

def initialize(x, y)
  @x = x
  @y = y
end

Instance Attribute Details

#xObject (readonly)

Returns the value of attribute x.



6
7
8
# File 'lib/software_challenge_client/coordinates.rb', line 6

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



6
7
8
# File 'lib/software_challenge_client/coordinates.rb', line 6

def y
  @y
end

Class Method Details

.doubled_to_oddr(c) ⇒ Object

Konvertiert c in das odd-r Koordinatensystem.

Parameters:

  • c (Coordinates)

    Koordinaten aus dem doubled System



38
39
40
# File 'lib/software_challenge_client/coordinates.rb', line 38

def self.doubled_to_oddr(c)
  self.doubled_to_oddr_int(c.x, c.y)
end

.doubled_to_oddr_int(x, y) ⇒ Object

Konvertiert (x, y) in das doubled Koordinatensystem.

Parameters:

  • x (Integer)

    X-Koordinate aus dem doubled System

  • y (Integer)

    Y-Koordinate aus dem doubled System



45
46
47
# File 'lib/software_challenge_client/coordinates.rb', line 45

def self.doubled_to_oddr_int(x, y)
  Coordinates.new((x / 2.0).ceil() - y % 2, y)
end

.oddr_to_doubled(c) ⇒ Object

Konvertiert (x, y) in das doubled Koordinatensystem.

Parameters:

  • x (Integer)

    X-Koordinate aus dem odd-r System

  • y (Integer)

    Y-Koordinate aus dem odd-r System



26
27
28
# File 'lib/software_challenge_client/coordinates.rb', line 26

def self.oddr_to_doubled(c)
  self.oddr_to_doubled_int(c.x, c.y)
end

.oddr_to_doubled_int(x, y) ⇒ Object

Konvertiert c in das doubled Koordinatensystem.

Parameters:



32
33
34
# File 'lib/software_challenge_client/coordinates.rb', line 32

def self.oddr_to_doubled_int(x, y)
  Coordinates.new(x * 2 + y % 2, y)
end

.originObject

Gibt die Ursprungs-Koordinaten (0, 0) zurück.



19
20
21
# File 'lib/software_challenge_client/coordinates.rb', line 19

def self.origin
  Coordinates.new(0, 0)
end

Instance Method Details

#+(other) ⇒ Object



59
60
61
# File 'lib/software_challenge_client/coordinates.rb', line 59

def +(other)
  Coordinates.new(x + other.x, y + other.y)
end

#<=>(other) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/software_challenge_client/coordinates.rb', line 49

def <=>(other)
  xComp = x <=> other.x
  yComp = y <=> other.y
  if xComp == 0
    yComp
  else
    xComp
  end
end

#==(other) ⇒ Object



14
15
16
# File 'lib/software_challenge_client/coordinates.rb', line 14

def ==(other)
  x == other.x && y == other.y
end

#inspectObject



68
69
70
# File 'lib/software_challenge_client/coordinates.rb', line 68

def inspect
  to_s
end

#to_sObject

Gibt eine textuelle Repräsentation der Koordinaten aus.



64
65
66
# File 'lib/software_challenge_client/coordinates.rb', line 64

def to_s
  "(#{x}, #{y})"
end