Class: Sevgi::Geometry::Point
- Inherits:
-
Data
- Object
- Data
- Sevgi::Geometry::Point
show all
- Includes:
- Comparable, Affinity
- Defined in:
- lib/sevgi/geometry/point.rb,
lib/sevgi/geometry/equation.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Affinity
#reflect, #reflect_x, #reflect_y, #rotate, #scale, #skew, #skew_x, #skew_y, #translate
Constructor Details
#initialize(x:, y:) ⇒ Point
Returns a new instance of Point.
48
|
# File 'lib/sevgi/geometry/point.rb', line 48
def initialize(x:, y:) = super(x: x.to_f, y: y.to_f)
|
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x
25
26
27
|
# File 'lib/sevgi/geometry/point.rb', line 25
def x
@x
end
|
#y ⇒ Object
Returns the value of attribute y
25
26
27
|
# File 'lib/sevgi/geometry/point.rb', line 25
def y
@y
end
|
Class Method Details
.angle(starting, ending) ⇒ Object
29
30
31
32
|
# File 'lib/sevgi/geometry/point.rb', line 29
def self.angle(starting, ending)
starting, ending = Tuples[Point, starting, ending]
F.atan2(ending.y - starting.y, ending.x - starting.x)
end
|
.eq?(this, that, precision: nil) ⇒ Boolean
34
35
36
37
|
# File 'lib/sevgi/geometry/point.rb', line 34
def self.eq?(this, that, precision: nil)
this, that = Tuples[self, this, that]
F.eq?(this.x, that.x, precision:) && F.eq?(this.y, that.y, precision:)
end
|
.length(starting, ending) ⇒ Object
39
40
41
42
|
# File 'lib/sevgi/geometry/point.rb', line 39
def self.length(starting, ending)
starting, ending = Tuples[Point, starting, ending]
::Math.sqrt(((starting.y - ending.y) ** 2) + ((starting.x - ending.x) ** 2))
end
|
.origin ⇒ Object
44
45
46
|
# File 'lib/sevgi/geometry/point.rb', line 44
def self.origin
new(x: 0.0, y: 0.0)
end
|
Instance Method Details
#<=>(other) ⇒ Object
50
|
# File 'lib/sevgi/geometry/point.rb', line 50
def <=>(other) = (other = Tuple[Point, other]).nan? || nan? ? nil : deconstruct <=> other.deconstruct
|
#above?(other) ⇒ Boolean
52
|
# File 'lib/sevgi/geometry/point.rb', line 52
def above?(other) = y <= Tuple[Point, other].y
|
#approx(precision = nil) ⇒ Object
54
|
# File 'lib/sevgi/geometry/point.rb', line 54
def approx(precision = nil) = with(x: F.approx(x, precision), y: F.approx(y, precision))
|
#below?(other) ⇒ Boolean
56
|
# File 'lib/sevgi/geometry/point.rb', line 56
def below?(other) = y >= Tuple[Point, other].y
|
#eq?(other, precision: nil) ⇒ Boolean
58
|
# File 'lib/sevgi/geometry/point.rb', line 58
def eq?(other, precision: nil) = self.class.eq?(self, other, precision:)
|
#eql?(other) ⇒ Boolean
Also known as:
==
60
|
# File 'lib/sevgi/geometry/point.rb', line 60
def eql?(other) = self.class == other.class && deconstruct == other.deconstruct
|
#equation(angle) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/sevgi/geometry/equation.rb', line 58
def equation(angle)
return Equation.horizontal(y) if F.zero?(angle % 180.0)
return Equation.vertical(x) if F.zero?(angle % 90.0)
Equation.diagonal(slope: (slope = F.tan(angle)), intercept: y - (slope * x))
end
|
#hash ⇒ Object
62
|
# File 'lib/sevgi/geometry/point.rb', line 62
def hash = [self.class, *deconstruct].hash
|
#infinite? ⇒ Boolean
64
|
# File 'lib/sevgi/geometry/point.rb', line 64
def infinite? = deconstruct.any?(&:infinite?)
|
#left?(other) ⇒ Boolean
66
|
# File 'lib/sevgi/geometry/point.rb', line 66
def left?(other) = x <= Tuple[Point, other].x
|
#nan? ⇒ Boolean
68
|
# File 'lib/sevgi/geometry/point.rb', line 68
def nan? = deconstruct.any?(&:nan?)
|
#right?(other) ⇒ Boolean
70
|
# File 'lib/sevgi/geometry/point.rb', line 70
def right?(other) = x >= Tuple[Point, other].x
|
#to_cs ⇒ Object
72
|
# File 'lib/sevgi/geometry/point.rb', line 72
def to_cs = "#{F.approx(x)},#{F.approx(y)}"
|
#to_s ⇒ Object
74
|
# File 'lib/sevgi/geometry/point.rb', line 74
def to_s = "(#{to_cs})"
|