Class: Rubydraw::Point

Inherits:
Object show all
Defined in:
lib/rubydraw/point.rb

Overview

Used for specifying x and y positions in one class, instead of by themselves or arrays. Might look like:

@image.draw(Point[5, 6])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y) ⇒ Point

Create a new point with the given x and y positions.



15
16
17
# File 'lib/rubydraw/point.rb', line 15

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

Instance Attribute Details

#xObject

Returns the value of attribute x.



12
13
14
# File 'lib/rubydraw/point.rb', line 12

def x
  @x
end

#yObject

Returns the value of attribute y.



12
13
14
# File 'lib/rubydraw/point.rb', line 12

def y
  @y
end

Class Method Details

.[](x, y) ⇒ Object

Shorthand new method



8
9
10
# File 'lib/rubydraw/point.rb', line 8

def self.[](x, y)
  self.new(x, y)
end

Instance Method Details

#*(other) ⇒ Object

Same as Point#- and Point#+, but multiply instead.



54
55
56
57
58
59
60
61
# File 'lib/rubydraw/point.rb', line 54

def *(other)
  if other.is_a?(Numeric)
    ox, oy = other, other
  else
    ox, oy = other.x, other.y
  end
  Point[self.x * ox, self.y * oy]
end

#+(other) ⇒ Object

Add this point’s x and y positions to other‘s x and y positions.

Example:

Point[10, 20] + Point[50, -10]
=> #<Rubydraw::Point:0x1010f1958 @y=10, @x=60>

This also works:

Point[10, 20] + 5
=> #<Rubydraw::Point:0x1010f1958 @y=15, @x=25>


34
35
36
37
38
39
40
41
# File 'lib/rubydraw/point.rb', line 34

def +(other)
  if other.is_a?(Numeric)
    ox, oy = other, other
  else
    ox, oy = other.x, other.y
  end
  Point[self.x + ox, self.y + oy]
end

#-(other) ⇒ Object

Subtract other‘s x and y positions from this point’s x and y.



44
45
46
47
48
49
50
51
# File 'lib/rubydraw/point.rb', line 44

def -(other)
  if other.is_a?(Numeric)
    ox, oy = other, other
  else
    ox, oy = other.x, other.y
  end
  Point[self.x - ox, self.y - oy]
end

#/(other) ⇒ Object

Divide.



64
65
66
67
68
69
70
71
# File 'lib/rubydraw/point.rb', line 64

def /(other)
  if other.is_a?(Numeric)
    ox, oy = other, other
  else
    ox, oy = other.x, other.y
  end
  Point[self.x / ox, self.y / oy]
end

#==(other) ⇒ Object Also known as: eql?

Two points are equal if both their x and y positions are the same.



74
75
76
77
78
79
80
# File 'lib/rubydraw/point.rb', line 74

def ==(other)
  result = if other.is_a?(Point)
    @x == other.x and @y == other.y
  else
    false
  end
end

#inside?(rect) ⇒ Boolean

Returns if this self falls within the given rectangle.

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/rubydraw/point.rb', line 20

def inside?(rect)
  min = rect.top_left
  max = rect.bottom_right
  (x.between?(min.x, max.x)) and (y.between?(min.y, max.y))
end

#to_aryObject



84
85
86
# File 'lib/rubydraw/point.rb', line 84

def to_ary
  [@x, @y]
end

#to_sObject



88
89
90
# File 'lib/rubydraw/point.rb', line 88

def to_s
  "#{@x}, #{y}"
end