Class: Coords::Shapes::Line2d
- Inherits:
-
Object
- Object
- Coords::Shapes::Line2d
- Defined in:
- lib/coords/shapes/line2d.rb
Direct Known Subclasses
Instance Method Summary collapse
- #==(line) ⇒ Object
- #ends ⇒ Object
-
#initialize(x1, y1, x2, y2) ⇒ Line2d
constructor
A new instance of Line2d.
- #length ⇒ Object
- #length_squared ⇒ Object
- #point1 ⇒ Object
- #point2 ⇒ Object
- #to_s ⇒ Object
- #translate(x2, y2) ⇒ Object
- #translate!(x2, y2) ⇒ Object
Constructor Details
#initialize(x1, y1, x2, y2) ⇒ Line2d
Returns a new instance of Line2d.
5 6 7 8 |
# File 'lib/coords/shapes/line2d.rb', line 5 def initialize(x1, y1, x2, y2) @point1 = Coords::Cartesian2d.new(x1, y1) @point2 = Coords::Cartesian2d.new(x2, y2) end |
Instance Method Details
#==(line) ⇒ Object
34 35 36 |
# File 'lib/coords/shapes/line2d.rb', line 34 def ==(line) ((point1 == line.point1 && point2 == line.point2) || (point1 == line.point2 && point2 == line.point1)) end |
#ends ⇒ Object
18 19 20 |
# File 'lib/coords/shapes/line2d.rb', line 18 def ends [point1, point2] end |
#length ⇒ Object
26 27 28 |
# File 'lib/coords/shapes/line2d.rb', line 26 def length Math.sqrt(length_squared) end |
#length_squared ⇒ Object
22 23 24 |
# File 'lib/coords/shapes/line2d.rb', line 22 def length_squared point1.distance_squared(point2) end |
#point1 ⇒ Object
10 11 12 |
# File 'lib/coords/shapes/line2d.rb', line 10 def point1 @point1 end |
#point2 ⇒ Object
14 15 16 |
# File 'lib/coords/shapes/line2d.rb', line 14 def point2 @point2 end |
#to_s ⇒ Object
30 31 32 |
# File 'lib/coords/shapes/line2d.rb', line 30 def to_s "(#{point1.x.to_s},#{point1.y.to_s}),(#{point2.x.to_s},#{point2.y.to_s})" end |
#translate(x2, y2) ⇒ Object
38 39 40 41 42 |
# File 'lib/coords/shapes/line2d.rb', line 38 def translate(x2, y2) translated_line = Line2d.new(point1.x, point1.y, point2.x, point2.y) translated_line.translate!(x2, y2) translated_line end |
#translate!(x2, y2) ⇒ Object
44 45 46 47 |
# File 'lib/coords/shapes/line2d.rb', line 44 def translate!(x2, y2) point1.translate!(x2, y2) point2.translate!(x2, y2) end |