Question-and-Answer Resource for the Building Energy Modeling Community
Get s tarted with the Help page

# Revision history [back]

Based on this, I wrote the following function that I've just shared.

  def self.get_length(vertices_a, vertices_b)
length = 0.0

vertices_a.each_with_index do |vertex_a, index_a|
prev_a = vertices_a[index_a-1]
vertices_b.each_with_index do |vertex_b, index_b|
prev_b = vertices_b[index_b-1]

p1 = prev_a
p2 = vertex_a
p3 = prev_b
p4 = vertex_b

p12 = p2-p1
p34 = p4-p3

next unless p12.cross(p34).length < 1e-6

p13 = p3-p1

next unless p12.cross(p13).length < 1e-6

p14 = p4-p1

k2 = p12.dot(p12)
k3 = p12.dot(p13)
k4 = p12.dot(p14)

table = [
[0.0, p14.length, p12.length],
[p13.length, p34.length, (p3-p2).length],
[p12.length, (p4-p2).length, 0.0]
]

row = if k3 < 1e-6 then
0
elsif k3 < k2 then
1
else
2
end

col = if k4 < 1e-6 then
0
elsif k4 < k2 then
1
else
2
end

length += table[row][col]
end
end
length /= 2 if vertices_a.length.eql?(2)
length /= 2 if vertices_b.length.eql?(2)

return length
end