Class: DYI::Coordinate
- Inherits:
-
Object
- Object
- DYI::Coordinate
- 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.
Constant Summary collapse
- ZERO =
The origin point.
new(0,0)
- @@default_format =
'(x,y)'
Instance Attribute Summary collapse
-
#x ⇒ Length
readonly
Returns an x-coordinate.
-
#y ⇒ Length
readonly
Returns a y-coordinate.
Class Method Summary collapse
-
.default_format ⇒ String
Returns a format that is used when called #to_s without an argument.
-
.default_format=(fromat) ⇒ Object
Sets a format string that is used when called #to_s without an argument.
-
.new(*args) ⇒ Object
Creates and returns a new instance of Coordinate provided the argument is not an instace of Coordinate.
-
.new_or_nil(*args) ⇒ Coordinate?
Returns a new instace of Coordinate if the argments is not
nil(callsCoordinate.newmethod), but returnsnilif the argument isnil. -
.orthogonal_coordinates(x, y) ⇒ Object
Creates a new instance of Coordinate using the cartesian coordinates, and returns it.
-
.polar_coordinates(radius, theta) ⇒ Object
Creates a new instance of Coordinate using the polar coordinates, and returns it.
-
.set_default_format(format) ⇒ Object
Invokes block with given format string as default format.
Instance Method Summary collapse
-
#*(number) ⇒ Length
Returns a new muliplicative coordinate of the receiver by number.
-
#**(number) ⇒ Length
Raises a coordinate the number power.
-
#+(other) ⇒ Length
Returns a new coordinate which is the sum of the receiver and other.
-
#+@ ⇒ Length
Unary Plus – Returns the receiver’s value.
-
#-(other) ⇒ Length
Returns a new length which is the difference of the receiver and other.
-
#-@ ⇒ Length
Unary Minus – Returns a coordinate whose x-coordinate and y-coordinate negated.
-
#/(number) ⇒ Length
(also: #quo)
Returns a new divisional length of the receiver by number.
-
#==(other) ⇒ Boolean
Returns whether the receiver equals to other.
-
#abs ⇒ Length
Returns a distance between receiver and the origin point.
-
#distance(other) ⇒ Length
Returns a distance between receiver and origin.
-
#initialize(*args) ⇒ Coordinate
constructor
A new instance of Coordinate.
- #inspect ⇒ Object
-
#nonzero? ⇒ Coordinate?
Returns whether the receiver is not the origin point.
- #to_cls_point ⇒ Object
-
#to_s(format = nil) ⇒ Length
Returns a string to represent the receiver.
-
#to_user_unit ⇒ Coordinate
Returns a coordinate that converted into the user unit.
-
#zero? ⇒ Boolean
Returns whether the receiver is the origin point.
Constructor Details
#initialize(coordinate) ⇒ Coordinate #initialize(array) ⇒ Coordinate #initialize(x, y) ⇒ Coordinate
Returns a new instance of Coordinate.
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
#x ⇒ Length (readonly)
Returns an x-coordinate
46 47 48 |
# File 'lib/dyi/coordinate.rb', line 46 def x @x end |
#y ⇒ Length (readonly)
Returns a y-coordinate
50 51 52 |
# File 'lib/dyi/coordinate.rb', line 50 def y @y end |
Class Method Details
.default_format ⇒ String
Returns a format that is used when called #to_s without an argument.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |
#abs ⇒ Length
Returns a distance between receiver and the origin point.
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.
182 183 184 |
# File 'lib/dyi/coordinate.rb', line 182 def distance(other) (self - other).abs end |
#inspect ⇒ Object
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.
160 161 162 |
# File 'lib/dyi/coordinate.rb', line 160 def nonzero? zero? ? nil : self end |
#to_cls_point ⇒ Object
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.
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_unit ⇒ Coordinate
Returns a coordinate that converted into the user unit.
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.
153 154 155 |
# File 'lib/dyi/coordinate.rb', line 153 def zero? @x.zero? && @y.zero? end |