Class: CTioga2::Graphics::Segment

Inherits:
Line
  • Object
show all
Defined in:
lib/ctioga2/graphics/geometry.rb

Overview

Same as line, but with a beginning and an end

Instance Attribute Summary collapse

Attributes inherited from Line

#dx, #dy, #x, #y

Instance Method Summary collapse

Methods inherited from Line

#intersection

Constructor Details

#initialize(x1, y1, x2, y2) ⇒ Segment

Returns a new instance of Segment.



68
69
70
71
72
# File 'lib/ctioga2/graphics/geometry.rb', line 68

def initialize(x1, y1, x2, y2)
  @x2 = x2
  @y2 = y2
  super(x1, y1, x2 - x1, y2 - y1)
end

Instance Attribute Details

#x2Object

Returns the value of attribute x2.



66
67
68
# File 'lib/ctioga2/graphics/geometry.rb', line 66

def x2
  @x2
end

#y2Object

Returns the value of attribute y2.



66
67
68
# File 'lib/ctioga2/graphics/geometry.rb', line 66

def y2
  @y2
end

Instance Method Details

#to_lineObject



89
90
91
# File 'lib/ctioga2/graphics/geometry.rb', line 89

def to_line()
  return Line.new(@x, @y, @dx, @dy)
end

#within_bounds?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ctioga2/graphics/geometry.rb', line 75

def within_bounds?(x, y)
  return (
          (
           (x - @x) * (x - @x2) <= 0 or
           (x - @x).abs < 1e-15 or
           (x - @x2).abs < 1e-15
           ) and 
          ((y - @y) * (y - @y2) <= 0 or
           (y - @y).abs < 1e-15 or
           (y - @y2).abs < 1e-15
           )
          )
end