Class: Geom::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/geom/line.rb

Overview

Line defined by parametric equations X = X0 + XAt, Y = Y0 + YAt, ,Z = Z0 + ZAt

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Line

Returns a new instance of Line.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/geom/line.rb', line 6

def initialize(*args)

  case args.size
  when 2 # Start point and End point
    start_point = args[0]
    end_point = args[1]

    line_direction = Vector.new(start_point, end_point)

    @x0 = start_point.x
    @y0 = start_point.y
    @z0 = start_point.z
    @xa = line_direction.x
    @ya = line_direction.y
    @za = line_direction.z

  else # Coefficients of line equation
    @x0, @xa, @y0, @ya, @z0, @za = args.flatten
  end
end

Instance Attribute Details

#x0Object

Returns the value of attribute x0.



4
5
6
# File 'lib/geom/line.rb', line 4

def x0
  @x0
end

#xaObject

Returns the value of attribute xa.



4
5
6
# File 'lib/geom/line.rb', line 4

def xa
  @xa
end

#y0Object

Returns the value of attribute y0.



4
5
6
# File 'lib/geom/line.rb', line 4

def y0
  @y0
end

#yaObject

Returns the value of attribute ya.



4
5
6
# File 'lib/geom/line.rb', line 4

def ya
  @ya
end

#z0Object

Returns the value of attribute z0.



4
5
6
# File 'lib/geom/line.rb', line 4

def z0
  @z0
end

#zaObject

Returns the value of attribute za.



4
5
6
# File 'lib/geom/line.rb', line 4

def za
  @za
end

Instance Method Details

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



27
28
29
30
31
32
33
34
# File 'lib/geom/line.rb', line 27

def ==(line)
  (@x0 - line.x0).abs < TOLERANCE &&
  (@y0 - line.y0).abs < TOLERANCE &&
  (@z0 - line.z0).abs < TOLERANCE &&
  (@xa - line.xa).abs < TOLERANCE &&
  (@ya - line.ya).abs < TOLERANCE &&
  (@za - line.za).abs < TOLERANCE
end

#closest_approach_parameter(line) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/geom/line.rb', line 60

def closest_approach_parameter(line)
  if self.direction.parallel?(line.direction)
    raise ArgumentError.new("Lines are parallel")
  else
    s1 = Vector.new(@x0, @y0, @z0)
    v1 = Vector.new(@xa, @ya, @za)
    s2 = Vector.new(line.x0, line.y0, line.z0)
    v2 = Vector.new(line.xa, line.ya, line.za)

    i = 1 / ((v1.dot(v2) * v1.dot(v2)) - (v1.length * v1.length) * (v2.length * v2.length))
    j11 = -(v2.length * v2.length)
    j12 = v1.dot(v2)

    vk = Vector.new(s1, s2)

    k1 = vk.dot(v1)
    k2 = vk.dot(v2)

    i * (j11 * k1 + j12 * k2)
  end
end

#directionObject



42
43
44
# File 'lib/geom/line.rb', line 42

def direction
  Vector.new(point_at_parameter(0), point_at_parameter(1))
end

#hashObject



38
39
40
# File 'lib/geom/line.rb', line 38

def hash
  (@x0.to_int ^ @y0.to_int ^ @z0.to_int ^ @xa.to_int ^ @ya.to_int ^ @za.to_int)
end

#intersection_with_line(line) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/geom/line.rb', line 82

def intersection_with_line(line)
  t1 = self.closest_approach_parameter(line)
  t2 = line.closest_approach_parameter(self)

  p1 = self.point_at_parameter(t1)
  p2 = line.point_at_parameter(t2)

  dist = p1.distance_to_point(p2)

  if (dist < TOLERANCE)
    p1
  else
    raise ArgumentError.new("Lines do not intersect")
  end
end

#intersection_with_plane(plane) ⇒ Object

Raises:

  • (ArgumentError)


98
99
100
101
102
103
104
105
106
107
# File 'lib/geom/line.rb', line 98

def intersection_with_plane(plane)
  var = [0,0]
  var[0] = @x0 * plane.a + @y0 * plane.b + @z0 * plane.c
  var[1] = @xa * plane.a + @ya * plane.b + @za * plane.c

  raise ArgumentError.new("Line is parallel to the plane") if var[1] == 0

  t = (plane.d - var[0]) / var[1]
  Point.new((@x0 + @xa * t), (@y0 + @ya * t), (@z0 + @za * t))
end

#on_plane?(plane) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/geom/line.rb', line 109

def on_plane?(plane)
  self.point_at_parameter(0).on_plane?(plane) && self.point_at_parameter(1).on_plane?(plane)
end

#parameter_at_point(point) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/geom/line.rb', line 50

def parameter_at_point(point)
  if (@xa != 0)
    ((point.x - @x0) / @xa)
  elsif (@ya != 0)
    ((point.y - @y0) / @ya)
  else
    ((point.z - @z0) / @za)
  end
end

#point_at_parameter(t) ⇒ Object



46
47
48
# File 'lib/geom/line.rb', line 46

def point_at_parameter(t)
  Point.new((@x0 + @xa * t), (@y0 + @ya * t), (@z0 + @za * t))
end

#to_sObject



113
114
115
# File 'lib/geom/line.rb', line 113

def to_s
  "Line(%.3f,%.3f,%.3f,%.3f,%.3f,%.3f)" % [@x0, @xa, @y0, @ya, @z0, @za]
end