53 lines
1.4 KiB
Ruby
53 lines
1.4 KiB
Ruby
# Nathan Hinton 2 Oct 2025
|
|
|
|
|
|
# A helper class for storing and converting universe point formats:
|
|
class Position
|
|
attr_reader :x_pos, :y_pos, :a, :b, :c
|
|
def initialize(*args)
|
|
if args.length == 2
|
|
# initialize with x, y position:
|
|
if args.sum.odd?
|
|
raise ArgumentError.new('A position created in (x, y) must sum to an even number!')
|
|
end
|
|
@x_pos = args[0]
|
|
@y_pos = args[1]
|
|
elsif args.length == 3
|
|
# initialize with a, b, c position
|
|
raise NotImplementedError.new('(a, b, c) positioning not implimented!')
|
|
else
|
|
raise ArgumentError.new('Creation of a position requires two or three integer args for a position.')
|
|
end
|
|
end
|
|
|
|
# Returns the corrdinates in the 2d plane in a 3 axis notation.
|
|
#
|
|
# @return [Array<Integer>] The (a, b, c) position
|
|
def two_d_hexagonal
|
|
return [a, b, c]
|
|
end
|
|
|
|
# Returns the coordinates in the 2d plane
|
|
#
|
|
# @return [Array<Integer>] The (x, y) position
|
|
def two_d_cartesian
|
|
return [x_pos, y_pos]
|
|
end
|
|
|
|
# Returns the string formatted version
|
|
#
|
|
# @return [String] Returns a string representing the point.
|
|
def to_s
|
|
return "(#{@x_pos}, #{y_pos})"
|
|
end
|
|
|
|
# Custom comparison. We need to check the the point is the same, not the
|
|
# identity.
|
|
#
|
|
# @param other [Positon] The position to compare with
|
|
# @return [Boolean] If they are in the same position as each other.
|
|
def ==(other)
|
|
return other.x_pos == @x_pos && other.y_pos == @y_pos
|
|
end
|
|
end
|