Class: Sevgi::Geometry::Point

Inherits:
Data
  • Object
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

#xObject (readonly)

Returns the value of attribute x

Returns:

  • (Object)

    the current value of x



25
26
27
# File 'lib/sevgi/geometry/point.rb', line 25

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y

Returns:

  • (Object)

    the current value of 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

Returns:

  • (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

.originObject



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

Returns:

  • (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

Returns:

  • (Boolean)


56
# File 'lib/sevgi/geometry/point.rb', line 56

def below?(other) = y >= Tuple[Point, other].y

#eq?(other, precision: nil) ⇒ Boolean

Returns:

  • (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: ==

Returns:

  • (Boolean)


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

#hashObject



62
# File 'lib/sevgi/geometry/point.rb', line 62

def hash = [self.class, *deconstruct].hash

#infinite?Boolean

Returns:

  • (Boolean)


64
# File 'lib/sevgi/geometry/point.rb', line 64

def infinite? = deconstruct.any?(&:infinite?)

#left?(other) ⇒ Boolean

Returns:

  • (Boolean)


66
# File 'lib/sevgi/geometry/point.rb', line 66

def left?(other) = x <= Tuple[Point, other].x

#nan?Boolean

Returns:

  • (Boolean)


68
# File 'lib/sevgi/geometry/point.rb', line 68

def nan? = deconstruct.any?(&:nan?)

#right?(other) ⇒ Boolean

Returns:

  • (Boolean)


70
# File 'lib/sevgi/geometry/point.rb', line 70

def right?(other) = x >= Tuple[Point, other].x

#to_csObject



72
# File 'lib/sevgi/geometry/point.rb', line 72

def to_cs = "#{F.approx(x)},#{F.approx(y)}"

#to_sObject



74
# File 'lib/sevgi/geometry/point.rb', line 74

def to_s = "(#{to_cs})"