Class: DYI::Coordinate

Inherits:
Object
  • Object
show all
Defined in:
lib/dyi/coordinate.rb,
lib/ironruby.rb

Overview

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

Constant Summary collapse

ZERO =

The origin point.

Since:

  • 0.0.0

new(0,0)
@@default_format =

Since:

  • 0.0.0

'(x,y)'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coordinate) ⇒ Coordinate #initialize(array) ⇒ Coordinate #initialize(x, y) ⇒ Coordinate

Returns a new instance of Coordinate.

Overloads:

  • #initialize(coordinate) ⇒ Coordinate

    Returns the argument itself.

    Parameters:

    • coordinate (Coordinate)

      the source coordinate

  • #initialize(array) ⇒ Coordinate

    Return a new instance of Coordinate. First element of array is used for x-coordinate, second element of array is used y-coordinate.

    Parameters:

    • array (Array<Length, Number, String>)

      an array converted into Coordinate

    Raises:

    • (ArgumentError)

      size of array does not equal to 2

  • #initialize(x, y) ⇒ Coordinate

    Parameters:

    • x (Length, Number, String)

      an x-cooridnate

    • y (Length, Number, String)

      a y-cooridnate

Raises:

  • (TypeError)

    the argument can not be coerced into Coordinate

See Also:

Since:

  • 0.0.0



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dyi/coordinate.rb', line 66

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

Instance Attribute Details

#xLength (readonly)

Returns an x-coordinate

Returns:

  • (Length)

    an x-coordinate

Since:

  • 0.0.0



46
47
48
# File 'lib/dyi/coordinate.rb', line 46

def x
  @x
end

#yLength (readonly)

Returns a y-coordinate

Returns:

Since:

  • 0.0.0



50
51
52
# File 'lib/dyi/coordinate.rb', line 50

def y
  @y
end

Class Method Details

.default_formatString

Returns a format that is used when called #to_s without an argument.

Returns:

  • (String)

    a format string

See Also:

Since:

  • 0.0.0



315
316
317
# File 'lib/dyi/coordinate.rb', line 315

def default_format
  @@default_format
end

.default_format=(fromat) ⇒ Object

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.



335
336
337
# File 'lib/dyi/coordinate.rb', line 335

def default_format=(fromat)
  @@default_format = fromat.clone
end

.new(coordinate) ⇒ Object .new(array) ⇒ Object .new(x, y) ⇒ Object

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.

Examples:

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

Overloads:

  • .new(coordinate) ⇒ Object

    Returns the argument itself.

    Parameters:

    • coordinate (Coordinate)

      the source coordinate

  • .new(array) ⇒ Object

    Return a new instance of Coordinate. First element of array is used for x-coordinate, second element of array is used y-coordinate.

    Parameters:

    • array (Array<Length, Number, String>)

      an array converted into Coordinate

    Raises:

    • (ArgumentError)

      size of array does not equal to 2

  • .new(x, y) ⇒ Object

    Parameters:

    • x (Length, Number, String)

      an x-cooridnate

    • y (Length, Number, String)

      a y-cooridnate

Raises:

  • (TypeError)

    the argument can not be coerced into Coordinate

Since:

  • 0.0.0



246
247
248
249
# File 'lib/dyi/coordinate.rb', line 246

def new(*args)
  return args.first if args.size == 1 && args.first.instance_of?(self)
  super
end

.new_or_nil(*args) ⇒ Coordinate?

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

Returns:

  • (Coordinate, nil)

    a new instace of Length if the argments is not nil, nil otherwise

See Also:

Since:

  • 0.0.0



256
257
258
# File 'lib/dyi/coordinate.rb', line 256

def new_or_nil(*args)
  (args.size == 1 && args.first.nil?) ? nil : new(*args)
end

.orthogonal_coordinates(x, y) ⇒ Object

Creates a new instance of Coordinate using the cartesian coordinates, and returns it.

Parameters:

  • x (Length, Number, String)

    an x-cooridnate

  • y (Length, Number, String)

    a y-cooridnate

Since:

  • 0.0.0



264
265
266
# File 'lib/dyi/coordinate.rb', line 264

def orthogonal_coordinates(x, y)
  new(x, y)
end

.polar_coordinates(radius, theta) ⇒ Object

Creates a new instance of Coordinate using the polar coordinates, and returns it.

Parameters:

  • radius (Length, Number, String)

    distance from the origin point

  • theta (Numeric)

    the angle from x-direction in degrees

Since:

  • 0.0.0



272
273
274
# File 'lib/dyi/coordinate.rb', line 272

def polar_coordinates(radius, theta)
  new(radius * DYI::Util.cos(theta), radius * DYI::Util.sin(theta))
end

.set_default_format(format) { ... } ⇒ Length .set_default_format(format) ⇒ String

Invokes block with given format string as default format.

Examples:

# 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)"

Overloads:

  • .set_default_format(format) { ... } ⇒ Length

    Invokes block with given format as default format. After invokes the block, the original format is used.

    Parameters:

    • format (String)

      a format string

    Yields:

    • a block which the format string is used in

    Returns:

    • (Length)

      the receiver itself

  • .set_default_format(format) ⇒ String

    Sets default format setring as default_format= method.

    Parameters:

    • format (String)

      a format string

    Returns:

    • (String)

      the given argument

See Also:

Since:

  • 0.0.0



300
301
302
303
304
305
306
307
308
309
310
# File 'lib/dyi/coordinate.rb', line 300

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

Instance Method Details

#*(number) ⇒ Length

Returns a new muliplicative coordinate of the receiver by number.

Parameters:

  • number (Numeric)

    the operand value

Returns:

  • (Length)

    a new muliplicative length

Since:

  • 0.0.0



128
129
130
# File 'lib/dyi/coordinate.rb', line 128

def *(number)
  self.class.new(@x * number, @y * number)
end

#**(number) ⇒ Length

Raises a coordinate the number power.

Parameters:

  • number (Numeric)

    the operand value

Returns:

  • (Length)

    a coordinate the number power

Since:

  • 0.0.0



135
136
137
# File 'lib/dyi/coordinate.rb', line 135

def **(number)
  self.class.new(@x ** number, @y ** number)
end

#+(other) ⇒ Length

Returns a new coordinate which is the sum of the receiver and other. First, other is converted into Coordinate.

Parameters:

  • other (Coordinate, Array<Length, Number, String>)

    the value that can be converted into Coordinate

Returns:

  • (Length)

    a new length which is the sum of the receiver and other

Since:

  • 0.0.0



109
110
111
112
# File 'lib/dyi/coordinate.rb', line 109

def +(other)
  other = self.class.new(other)
  self.class.new(@x + other.x, @y + other.y)
end

#+@Length

Unary Plus – Returns the receiver’s value.

Returns:

  • (Length)

    receiver itself

Since:

  • 0.0.0



93
94
95
# File 'lib/dyi/coordinate.rb', line 93

def +@
  self
end

#-(other) ⇒ Length

Returns a new length which is the difference of the receiver and other. First other is converted into Coordinate.

Parameters:

  • other (Length, Numeric, String)

    the value that can be converted into Coordinate

Returns:

  • (Length)

    a new length which is the difference of the receiver and other

Since:

  • 0.0.0



120
121
122
123
# File 'lib/dyi/coordinate.rb', line 120

def -(other)
  other = self.class.new(other)
  self.class.new(@x - other.x, @y - other.y)
end

#-@Length

Unary Minus – Returns a coordinate whose x-coordinate and y-coordinate negated.

Returns:

  • (Length)

    the negated receiver’s value

Since:

  • 0.0.0



100
101
102
# File 'lib/dyi/coordinate.rb', line 100

def -@
  self.class.new(-@x, -@y)
end

#/(number) ⇒ Length Also known as: quo

Returns a new divisional length of the receiver by number.

Parameters:

  • other (Numeric)

    the operand value

Returns:

  • (Length)

    a new divisional length

Raises:

  • (TypeError)

    other can’t be coerced into Numeric

Since:

  • 0.0.0



143
144
145
146
# File 'lib/dyi/coordinate.rb', line 143

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

#==(other) ⇒ Boolean

Returns whether the receiver equals to other.

Parameters:

  • other (Object)

    an object

Returns:

  • (Boolean)

    true if other is an instance of Coordinate and each coordinate of receiver equals to a coordinate of other, false otherwise

Since:

  • 0.0.0



169
170
171
172
# File 'lib/dyi/coordinate.rb', line 169

def ==(other)
  return false unless other.kind_of?(self.class)
  @x == other.x && @y == other.y
end

#absLength

Returns a distance between receiver and the origin point.

Returns:

  • (Length)

    a distance between receiver and the origin point

Since:

  • 0.0.0



176
177
178
# File 'lib/dyi/coordinate.rb', line 176

def abs
  (@x ** 2 + @y ** 2) ** 0.5
end

#distance(other) ⇒ Length

Returns a distance between receiver and origin.

Returns:

  • (Length)

    a distance between receiver and origin

Since:

  • 0.0.0



182
183
184
# File 'lib/dyi/coordinate.rb', line 182

def distance(other)
  (self - other).abs
end

#inspectObject

Since:

  • 0.0.0



214
215
216
# File 'lib/dyi/coordinate.rb', line 214

def inspect
  "(#{@x.inspect}, #{@y.inspect})"
end

#nonzero?Coordinate?

Returns whether the receiver is not the origin point.

Returns:

  • (Coordinate, nil)

    self if the receiver is not the origin point, nil otherwise

Since:

  • 0.0.0



160
161
162
# File 'lib/dyi/coordinate.rb', line 160

def nonzero?
  zero? ? nil : self
end

#to_cls_pointObject

Since:

  • 0.0.0



44
45
46
# File 'lib/ironruby.rb', line 44

def to_cls_point
  System::Drawing::PointF.new(x.to_f, y.to_f)
end

#to_s(format = nil) ⇒ Length

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.

Examples:

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

Parameters:

  • format (String) (defaults to: nil)

    a format string

Returns:

  • (Length)

    a string to represent the receiver

See Also:

Since:

  • 0.0.0



205
206
207
208
209
210
211
# File 'lib/dyi/coordinate.rb', line 205

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_unitCoordinate

Returns a coordinate that converted into the user unit.

Returns:

  • (Coordinate)

    a coordinate that converted into the user unit

Since:

  • 0.0.0



188
189
190
# File 'lib/dyi/coordinate.rb', line 188

def to_user_unit
  self.class.new(@x.to_user_unit, @y.to_user_unit)
end

#zero?Boolean

Returns whether the receiver is the origin point.

Returns:

  • (Boolean)

    true if the receiver is the origin point, false otherwise

Since:

  • 0.0.0



153
154
155
# File 'lib/dyi/coordinate.rb', line 153

def zero?
  @x.zero? && @y.zero?
end