So given an array of edges 'elist' (imported from AutoCAD), I'm checking for intersections.
elist.each do |e1|
# so given this line, we look for intersections, to amend [p1,p2] to [p1,px,pn,p2]
p1 = e1.start.position
p2 = e1.end.position
plist = [p1,p2]
line1 = e1.line # [point . vector]
# for this line segment, compare to all other edges
elist.each do |e2|
if ( e1 != e2 ) # only if not the same edge of course
line2 = e2.line # [point . vector]
v1 = line1[1] # vector
v2 = line2[1] # vector
if ( v1 != v2 ) # if vectors are differnet, they aren't parallel
int = Geom.intersect_line_line(line1,line2) # but assumes infinite!
if ( int ) # make sure it's on both lines, and not an endpoint of line1
z1 = e2.start.position
z2 = e2.end.position
if ( !plist.include?(int) ) # not end point or already found
if ( pointonline(int,p1,p2) ) # and on line1
if ( pointonline(int,z1,z2) ) # and on line2
# so adding our intersection to line 1
plist << int
end # if co-linear2
end # if co-linear1
end # if not end point
end # if intersection
end # if not parallel
end # if not same segment
end # subloop
# ... do stuff with amended plist ...
end # entity loop
Missing is a standard helper function 'pointonline' which checks if given point falls on the given line segment.
Given about 1,500 edges, it seems it can easily take 30 seconds, which just feels far too long for my taste.
Many thanks if you have any tips to optimize it!
--J