Intersect_line_line question
-
Hi,
I'm having trouble with Geom.intersect_line_line not giving the expected results so I was hoping someone could either tell me what I'm doing wrong or that the function is bugged so I know whether to rewrite it.According to the documentation in the API
@unknownuser said:
Geom.intersect_line_lineSketchUp 6.0+
The intersect_line_line computes the intersection of two lines.Arguments:
line1
The first line to intersect.
line2
The second line to intersect.
Returns:point
The intersection point. Returns nil if they do not intersect.
line1 = [Geom::Point3d.new(10,0,0), Geom::Vector3d.new(1,0,0)]
line2 = [Geom::Point3d.new(0,10,0), Geom::Point3d.new(20,10,0)]
pt = Geom.intersect_line_line(line1, line2)This will return the point (10,10,0).
If you type in their example into the Ruby editor it doesn't actually work as advertised, it returns nil.
Here is an example of two parallel lines returning a value...
l1 = [[0,0,0], [10,0,0]] [[0, 0, 0], [10, 0, 0]] l2 = [[0,5,0], [10,5,0]] [[0, 5, 0], [10, 5, 0]] i = Geom.intersect_line_line(l1, l2) Point3d(-10, 0, 0)
Thoughts?
Sincerely, Paul.
-
@s_k_e_t_c_h_y said:
If you type in their example into the Ruby editor it doesn't actually work as advertised, it returns
nil
.Yes there are many errors in the API dictionary.
The code example has the wrong vector argument for
line1
, and the example should read:line1 = [Geom;;Point3d.new(10,0,0), Geom;;Vector3d.new(0,1,0)] line2 = [Geom;;Point3d.new(0,10,0), Geom;;Point3d.new(20,10,0)] pt = Geom.intersect_line_line(line1, line2) # This will return the point (10,10,0). line3 = [Geom;;Point3d.new(10,0,0), Geom;;Vector3d.new(1,0,0)] pt2 = Geom.intersect_line_line(line2, line3) # This will return nil, as the lines are parallel to each other.
-
Thanks Dan,
That makes sense. I see that the way the function operates however isn't what I was after. The function as it stands turns the lines into infinite rays defined by the line's two points and then checks if the infinite rays intersect. I just want to check if the line segments bounded by the pairs of points intersect.I'm muddling through implementing this now, unless you know of an existing coded examples. There are 5 cases to check for
- Parallel Lines
- Normal Intersection within line segments
- Intersection, but outside limits defined by line segment points
- No intersection at all
- Lines are coincident (intersection is a line, not a point)
Sincerely, Paul.
-
@s_k_e_t_c_h_y said:
The function as it stands turns the lines into infinite rays defined by the line's two points and ...
In SketchUp lines are infinite, ... edges are not.
You might try using bounding boxes around your "segemnts"...
see API Geom::BoundingBox#intersect() -
Thanks again Dan,
I'll see what I can come up with =). -
Not the most mathematically efficient I think, but this will do for now...
https://groups.google.com/forum/#!topic/sketchupruby/2y8fU2wMZYo
-
If you have two points p1 = [x1,y1,z1] and p2 = [x2,y2,z2] the line passing through them is given by
[x,y,z] = p1 + k(p2-p1)
The segment of the line between the two points is for 0.0 <=k <= 1.0
So, if intersect_line_line returns a point (meaning that your two lines intersected), you just have to test that the intersection point satisfies the equation above for a k in the allowed range. It will necessarily be the same k for x, y, and z.
-
Ah,
Good point sl. I used a similar test for a line-plane intersection test.
Advertisement