a bug in Line3D intersection?

53 views
Skip to first unread message

zxo102

unread,
Nov 25, 2014, 8:58:16 AM11/25/14
to sy...@googlegroups.com

Hi there,

  In sympy0.7.6 and python3.4, I would like find a intersection point for two 3D lines but failed at the following 3D case.

  2D case:

>>> from sympy import Line, Point
>>> l1 = Line(Point(4,0),Point(0,4))
>>> l2 = Line(Point(0,0),Point(4,4))
>>> l1.intersection(l2)
[Point(2, 2)]

  3D case:  add z=1 into the above coordinates

>>> from sympy import Line3D, Point3D
>>> l3 = Line3D(Point3D(4,0,1),Point3D(0,4,1))
>>> l4 = Line3D(Point3D(0,0,1),Point3D(4,4,1))
>>> l3.intersection(l4)
[]

l3.intersection(l4) should return [Point3D(2,2,1)].

Is this a bug? or I did something wrong with it? Thanks for your help.

ouyang 

Chris Smith

unread,
Nov 25, 2014, 3:41:16 PM11/25/14
to sy...@googlegroups.com
That's a bug.

zhihua ouyang

unread,
Dec 5, 2014, 8:56:27 AM12/5/14
to sy...@googlegroups.com
I just fixed the bug which is in sympy\geometry\line3d.py:

def intersection(self, o):

        .......

        elif isinstance(o, LinearEntity3D):
            a = self.direction_cosine
            b = o.direction_cosine
            a = [abs(i) for i in a]
            b = [abs(i) for i in b]
            if a == b:  # assume they are parallel
                if isinstance(self, Line3D):
           ......

   Obviously, "if a == b" can not guarantee a is parallel to b. So, two more conditions need to be added for  parallel checking:

            c1 = sum(1 for i in range(len(a)) if a[i]==-b[i])
            c2 = sum(1 for i in range(len(a)) if a[i]==b[i])
            a = [abs(i) for i in a]
            b = [abs(i) for i in b]
            if a == b and (c1==3 or c2==3):  # assume they are parallel
                if isinstance(self, Line3D):
           ....
I have tested it with several examples and it works now.

ouyang
Reply all
Reply to author
Forward
0 new messages