class DYI::Coordinate

Class representing a coordinate. This class works with two length that mean orthogonal coordinates. The initial coordinate system has the origin at the top/left with the x-axis pointing to the right and the y-axis pointing down.

The equality operator ‘{#== ==}’ does not test equality instance but test equality value of x-coordinate and y-coordinate.

Ways of Calculating

This class suports following arithmetic operators and methods: {#+ +}, {#- -}, {#* *}, {#/ /}, {#** **}, {#quo}. The operators ‘{#+ +}’, ‘{#- -}’ coerces a right hand operand into Coordinate, and then calculates.

@since 0.0.0

Constants

ZERO

The origin point.

Attributes

x[R]

Returns an x-coordinate @return [Length] an x-coordinate

y[R]

Returns a y-coordinate @return [Length] a y-coordinate

Public Class Methods

default_format() click to toggle source

Returns a format that is used when called {#to_s} without an argument. @return [String] a format string @see .default_format=

# File lib/dyi/coordinate.rb, line 314
def default_format
  @@default_format
end
default_format=(fromat) click to toggle source

Sets a format string that is used when called {#to_s} without an argument. The format string that is set at this method is used permanently. Use {.set_default_format} with a block when you want to use a format string temporarily.

Uses the following characters as coordinate format strings.

"x" (x-coordinate placeholder)

Placeholder ‘x’ is replaced as x-coordinate.

"y" (y-coordinate placeholder)

Placeholder ‘y’ is replaced as y-coordinate.

"\" (Escape Character)

Causes the next character to be interpreted as a literal.

@see to_s @see .set_default_format @see Length.default_format= @see Numeric#strfnum

# File lib/dyi/coordinate.rb, line 334
def default_format=(fromat)
  @@default_format = fromat.clone
end
new(*args) click to toggle source

@overload initialize(coordinate)

Returns the argument itself.
@param [Coordinate] coordinate the source coordinate

@overload initialize(array)

Return a new instance of Coordinate. First element of _array_ is used
for x-coordinate, second element of _array_ is used y-coordinate.
@param [Array<Length, Number, String>] array an array converted into
  Coordinate
@raise [ArgumentError] size of _array_ does not equal to 2

@overload initialize(x, y)

@param [Length, Number, String] x an x-cooridnate
@param [Length, Number, String] y a y-cooridnate

@raise [TypeError] the argument can not be coerced into Coordinate @see .new

# File lib/dyi/coordinate.rb, line 65
def initialize(*args)
  case args.size
  when 1
    case arg = args.first
    when Coordinate
      @x = arg.x
      @y = arg.y
    when Array
      raise ArgumentError, "wrong number of arguments' size (#{arg.size} for 2)" if arg.size != 2
      @x = Length.new(arg[0])
      @y = Length.new(arg[1])
    else
      raise TypeError, "#{arg.class} can't be coerced into #{self.class}"
    end
  when 2
    @x = Length.new(args[0])
    @y = Length.new(args[1])
  else
    raise ArgumentError, "wrong number of arguments (#{args.size} for #{args.size == 0 ? 1 : 2})"
  end
end
new(*args) click to toggle source

Creates and returns a new instance of Coordinate provided the argument is not an instace of Coordinate. If the argument is an instace of Coordinate, returns the argument itself. @overload new(coordinate)

Returns the argument itself.
@param [Coordinate] coordinate the source coordinate

@overload new(array)

Return a new instance of Coordinate. First element of _array_ is used
for x-coordinate, second element of _array_ is used y-coordinate.
@param [Array<Length, Number, String>] array an array converted into
  Coordinate
@raise [ArgumentError] size of _array_ does not equal to 2

@overload new(x, y)

@param [Length, Number, String] x an x-cooridnate
@param [Length, Number, String] y a y-cooridnate

@raise (see initialize) @example

x = DYI::Length(10)
y = DYI::Length(20)
point1 = DYI::Coordinate.new(x, y)              # this point is (10, 20)
point2 = DYI::Coordinate.new(10, 20)            # it is (10, 20) too
point3 = DYI::Coordinate.new([x, y])            # it is (10, 20) too
point4 = DYI::Coordinate.new([10, 20])          # it is (10, 20) too
point5 = DYI::Coordinate.new(['10px', '20px'])  # it is (10, 20) too
Calls superclass method
# File lib/dyi/coordinate.rb, line 245
def new(*args)
  return args.first if args.size == 1 && args.first.instance_of?(self)
  super
end
new_or_nil(*args) click to toggle source

Returns a new instace of Coordinate if the argments is not nil (calls Coordinate.new method), but returns nil if the argument is nil. @return [Coordinate, nil] a new instace of Length if the argments is not

nil, nil otherwise

@see .new

# File lib/dyi/coordinate.rb, line 255
def new_or_nil(*args)
  (args.size == 1 && args.first.nil?) ? nil : new(*args)
end
orthogonal_coordinates(x, y) click to toggle source

Creates a new instance of Coordinate using the cartesian coordinates, and returns it. @param [Length, Number, String] x an x-cooridnate @param [Length, Number, String] y a y-cooridnate

# File lib/dyi/coordinate.rb, line 263
def orthogonal_coordinates(x, y)
  new(x, y)
end
polar_coordinates(radius, theta) click to toggle source

Creates a new instance of Coordinate using the polar coordinates, and returns it. @param [Length, Number, String] radius distance from the origin point @param [Numeric] theta the angle from x-direction in degrees

# File lib/dyi/coordinate.rb, line 271
def polar_coordinates(radius, theta)
  new(radius * DYI::Util.cos(theta), radius * DYI::Util.sin(theta))
end
set_default_format(format) { || ... } click to toggle source

Invokes block with given format string as default format. @overload set_default_format(format)

Invokes block with given _format_ as default format. After invokes the
block, the original format is used. 
@param [String] format a format string
@yield a block which the format string is used in
@return [Length] the receiver itself

@overload set_default_format(format)

Sets default format setring as {.default_format=} method.
@param [String] format a format string
@return [String] the given argument

@example

# an initial format string is "(x,y)"
point = DYI::Coordinate.new(10, 20)
point.to_s                            # => "(10,20)"
DYI::Coordinate.set_default_format('<x, y>') {
  point.to_s                          # => "<10, 20>"
  DYI::Length.set_default_format('0.0u') {
    point.to_s                        # => "<10.0pt, 20.0pt>"
  }
}
point.to_s                            # => "(10,20)"

@see Length.set_default_format @see .default_format=

# File lib/dyi/coordinate.rb, line 299
def set_default_format(format)
  if block_given?
    org_format = default_format
    self.default_format = format
    yield
    self.default_format = org_format
    self
  else
    self.default_format = format
  end
end

Public Instance Methods

*(number) click to toggle source

Returns a new muliplicative coordinate of the receiver by number. @param [Numeric] number the operand value @return [Length] a new muliplicative length

# File lib/dyi/coordinate.rb, line 127
def *(number)
  self.class.new(@x * number, @y * number)
end
**(number) click to toggle source

Raises a coordinate the number power. @param [Numeric] number the operand value @return [Length] a coordinate the number power

# File lib/dyi/coordinate.rb, line 134
def **(number)
  self.class.new(@x ** number, @y ** number)
end
+(other) click to toggle source

Returns a new coordinate which is the sum of the receiver and other. First, other is converted into Coordinate. @param [Coordinate, Array<Length, Number, String>] other the value that

can be converted into +Coordinate+

@return [Length] a new length which is the sum of the receiver and other

# File lib/dyi/coordinate.rb, line 108
def +(other)
  other = self.class.new(other)
  self.class.new(@x + other.x, @y + other.y)
end
+@() click to toggle source

Unary Plus – Returns the receiver’s value. @return [Length] receiver itself

# File lib/dyi/coordinate.rb, line 92
def +@
  self
end
-(other) click to toggle source

Returns a new length which is the difference of the receiver and other. First other is converted into Coordinate. @param [Length, Numeric, String] other the value that can be converted

into +Coordinate+

@return [Length] a new length which is the difference of the receiver and

_other_
# File lib/dyi/coordinate.rb, line 119
def -(other)
  other = self.class.new(other)
  self.class.new(@x - other.x, @y - other.y)
end
-@() click to toggle source

Unary Minus – Returns a coordinate whose x-coordinate and y-coordinate negated. @return [Length] the negated receiver’s value

# File lib/dyi/coordinate.rb, line 99
def -@
  self.class.new(-@x, -@y)
end
/(number) click to toggle source

Returns a new divisional length of the receiver by number. @param [Numeric] other the operand value @return [Length] a new divisional length @raise [TypeError] other can’t be coerced into Numeric

# File lib/dyi/coordinate.rb, line 142
def /(number)
  raise TypeError, "#{number.class} can't be coerced into Numeric" unless number.kind_of?(Numeric)
  self.class.new(@x.quo(number.to_f), @y.quo(number.to_f))
end
Also aliased as: quo
==(other) click to toggle source

Returns whether the receiver equals to other. @param [Object] other an object @return [Boolean] true if other is an instance of Coordinate and

each coordinate of receiver equals to a coordinate of _other_, false
otherwise
# File lib/dyi/coordinate.rb, line 168
def ==(other)
  return false unless other.kind_of?(self.class)
  @x == other.x && @y == other.y
end
abs() click to toggle source

Returns a distance between receiver and the origin point. @return [Length] a distance between receiver and the origin point

# File lib/dyi/coordinate.rb, line 175
def abs
  (@x ** 2 + @y ** 2) ** 0.5
end
distance(other) click to toggle source

Returns a distance between receiver and origin. @return [Length] a distance between receiver and origin

# File lib/dyi/coordinate.rb, line 181
def distance(other)
  (self - other).abs
end
inspect() click to toggle source

@private

# File lib/dyi/coordinate.rb, line 213
def inspect
  "(#{@x.inspect}, #{@y.inspect})"
end
nonzero?() click to toggle source

Returns whether the receiver is not the origin point. @return [Coordinate, nil] self if the receiver is not the origin point,

nil otherwise
# File lib/dyi/coordinate.rb, line 159
def nonzero?
  zero? ? nil : self
end
quo(number)
Alias for: /
to_cls_point() click to toggle source
# File lib/ironruby.rb, line 43
def to_cls_point
  System::Drawing::PointF.new(x.to_f, y.to_f)
end
to_s(format=nil) click to toggle source

Returns a string to represent the receiver.

Format string can be specified for the argument. If no argument is given, {.default_format} is used as format string. About format string, see the documentation of {.default_format} method. @param [String] format a format string @return [Length] a string to represent the receiver @example

point = DYI::Coordinate.new(10, 20)
point.to_s('<x, y>')         # => "<10, 20>"
point.to_s('\\x:x, \\y:y')   # => "x:10, y:20"

@see .default_format= @see .set_default_format

# File lib/dyi/coordinate.rb, line 204
def to_s(format=nil)
  fmts = (format || @@default_format).split('\\\\')
  fmts = fmts.map do |fmt|
    fmt.gsub(/(?!\\x)(.|\G)x/, '\\1' + @x.to_s).gsub(/(?!\\y)(.|\G)y/, '\\1' + @y.to_s).delete('\\')
  end
  fmts.join('\\')
end
to_user_unit() click to toggle source

Returns a coordinate that converted into the user unit. @return [Coordinate] a coordinate that converted into the user unit

# File lib/dyi/coordinate.rb, line 187
def to_user_unit
  self.class.new(@x.to_user_unit, @y.to_user_unit)
end
zero?() click to toggle source

Returns whether the receiver is the origin point. @return [Boolean] true if the receiver is the origin point, false

otherwise
# File lib/dyi/coordinate.rb, line 152
def zero?
  @x.zero? && @y.zero?
end