Geometry: How to determine if two lines are parallel in 3D based on coordinates of 2 points on each line?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
12
down vote

favorite
5












I am a Belgian engineer working on software in C# to provide smart bending solutions to a manufacturer of press brakes.



In this context I am searching for the best way to determine if two lines are parallel, based on the following information:



  • Each line has two points of which the coordinates are known

  • These coordinates are relative to the same frame

  • So to be clear, we have four points: A (ax, ay, az), B (bx,by,bz), C (cx,cy,cz) and D (dx,dy,dz)

Which is the best way to be able to return a simple boolean that says if these two lines are parallel or not? Can someone please help me out?



Edit after reading answers
Below is my C#-code, where I use two home-made objects, CS3DLine and CSVector, but the meaning of the objects speaks for itself. A toleratedPercentageDifference is used as well.



public static bool AreParallelLinesIn3D(CS3DLine left, CS3DLine right)

double toleratedPercentageDifference = 1;
CSVector vLeft = new CSVector(left.p1, left.p2);
CSVector vRight = new CSVector(right.p1, right.p2);
double ricoX = vLeft.X / vRight.X ;
double ricoY = vLeft.Y / vRight.Y ;
double ricoZ = vLeft.Z / vRight.Z ;
if (Math.Abs(ricoX - ricoY) > Math.Abs(toleratedPercentageDifference * ricoX / 100)) return false;
if (Math.Abs(ricoX - ricoZ) > Math.Abs(toleratedPercentageDifference * ricoX / 100)) return false;
return true;










share|cite|improve this question



















  • 3




    You seem to have used my answer, with the attendant division problems. @YvesDaoust is probably better. Write good unit tests for both and see which you prefer.
    – Ethan Bolker
    Aug 23 at 20:04






  • 1




    For an implementation of the cross-product in C#, maybe check out docs.microsoft.com/en-us/dotnet/api/… ? (the Wikipedia page might be a little overwhelming if you're not very math-y)
    – Ben Bolker
    Aug 24 at 0:10














up vote
12
down vote

favorite
5












I am a Belgian engineer working on software in C# to provide smart bending solutions to a manufacturer of press brakes.



In this context I am searching for the best way to determine if two lines are parallel, based on the following information:



  • Each line has two points of which the coordinates are known

  • These coordinates are relative to the same frame

  • So to be clear, we have four points: A (ax, ay, az), B (bx,by,bz), C (cx,cy,cz) and D (dx,dy,dz)

Which is the best way to be able to return a simple boolean that says if these two lines are parallel or not? Can someone please help me out?



Edit after reading answers
Below is my C#-code, where I use two home-made objects, CS3DLine and CSVector, but the meaning of the objects speaks for itself. A toleratedPercentageDifference is used as well.



public static bool AreParallelLinesIn3D(CS3DLine left, CS3DLine right)

double toleratedPercentageDifference = 1;
CSVector vLeft = new CSVector(left.p1, left.p2);
CSVector vRight = new CSVector(right.p1, right.p2);
double ricoX = vLeft.X / vRight.X ;
double ricoY = vLeft.Y / vRight.Y ;
double ricoZ = vLeft.Z / vRight.Z ;
if (Math.Abs(ricoX - ricoY) > Math.Abs(toleratedPercentageDifference * ricoX / 100)) return false;
if (Math.Abs(ricoX - ricoZ) > Math.Abs(toleratedPercentageDifference * ricoX / 100)) return false;
return true;










share|cite|improve this question



















  • 3




    You seem to have used my answer, with the attendant division problems. @YvesDaoust is probably better. Write good unit tests for both and see which you prefer.
    – Ethan Bolker
    Aug 23 at 20:04






  • 1




    For an implementation of the cross-product in C#, maybe check out docs.microsoft.com/en-us/dotnet/api/… ? (the Wikipedia page might be a little overwhelming if you're not very math-y)
    – Ben Bolker
    Aug 24 at 0:10












up vote
12
down vote

favorite
5









up vote
12
down vote

favorite
5






5





I am a Belgian engineer working on software in C# to provide smart bending solutions to a manufacturer of press brakes.



In this context I am searching for the best way to determine if two lines are parallel, based on the following information:



  • Each line has two points of which the coordinates are known

  • These coordinates are relative to the same frame

  • So to be clear, we have four points: A (ax, ay, az), B (bx,by,bz), C (cx,cy,cz) and D (dx,dy,dz)

Which is the best way to be able to return a simple boolean that says if these two lines are parallel or not? Can someone please help me out?



Edit after reading answers
Below is my C#-code, where I use two home-made objects, CS3DLine and CSVector, but the meaning of the objects speaks for itself. A toleratedPercentageDifference is used as well.



public static bool AreParallelLinesIn3D(CS3DLine left, CS3DLine right)

double toleratedPercentageDifference = 1;
CSVector vLeft = new CSVector(left.p1, left.p2);
CSVector vRight = new CSVector(right.p1, right.p2);
double ricoX = vLeft.X / vRight.X ;
double ricoY = vLeft.Y / vRight.Y ;
double ricoZ = vLeft.Z / vRight.Z ;
if (Math.Abs(ricoX - ricoY) > Math.Abs(toleratedPercentageDifference * ricoX / 100)) return false;
if (Math.Abs(ricoX - ricoZ) > Math.Abs(toleratedPercentageDifference * ricoX / 100)) return false;
return true;










share|cite|improve this question















I am a Belgian engineer working on software in C# to provide smart bending solutions to a manufacturer of press brakes.



In this context I am searching for the best way to determine if two lines are parallel, based on the following information:



  • Each line has two points of which the coordinates are known

  • These coordinates are relative to the same frame

  • So to be clear, we have four points: A (ax, ay, az), B (bx,by,bz), C (cx,cy,cz) and D (dx,dy,dz)

Which is the best way to be able to return a simple boolean that says if these two lines are parallel or not? Can someone please help me out?



Edit after reading answers
Below is my C#-code, where I use two home-made objects, CS3DLine and CSVector, but the meaning of the objects speaks for itself. A toleratedPercentageDifference is used as well.



public static bool AreParallelLinesIn3D(CS3DLine left, CS3DLine right)

double toleratedPercentageDifference = 1;
CSVector vLeft = new CSVector(left.p1, left.p2);
CSVector vRight = new CSVector(right.p1, right.p2);
double ricoX = vLeft.X / vRight.X ;
double ricoY = vLeft.Y / vRight.Y ;
double ricoZ = vLeft.Z / vRight.Z ;
if (Math.Abs(ricoX - ricoY) > Math.Abs(toleratedPercentageDifference * ricoX / 100)) return false;
if (Math.Abs(ricoX - ricoZ) > Math.Abs(toleratedPercentageDifference * ricoX / 100)) return false;
return true;







linear-algebra






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Aug 27 at 13:56

























asked Aug 23 at 14:10









JAlly

635




635







  • 3




    You seem to have used my answer, with the attendant division problems. @YvesDaoust is probably better. Write good unit tests for both and see which you prefer.
    – Ethan Bolker
    Aug 23 at 20:04






  • 1




    For an implementation of the cross-product in C#, maybe check out docs.microsoft.com/en-us/dotnet/api/… ? (the Wikipedia page might be a little overwhelming if you're not very math-y)
    – Ben Bolker
    Aug 24 at 0:10












  • 3




    You seem to have used my answer, with the attendant division problems. @YvesDaoust is probably better. Write good unit tests for both and see which you prefer.
    – Ethan Bolker
    Aug 23 at 20:04






  • 1




    For an implementation of the cross-product in C#, maybe check out docs.microsoft.com/en-us/dotnet/api/… ? (the Wikipedia page might be a little overwhelming if you're not very math-y)
    – Ben Bolker
    Aug 24 at 0:10







3




3




You seem to have used my answer, with the attendant division problems. @YvesDaoust is probably better. Write good unit tests for both and see which you prefer.
– Ethan Bolker
Aug 23 at 20:04




You seem to have used my answer, with the attendant division problems. @YvesDaoust is probably better. Write good unit tests for both and see which you prefer.
– Ethan Bolker
Aug 23 at 20:04




1




1




For an implementation of the cross-product in C#, maybe check out docs.microsoft.com/en-us/dotnet/api/… ? (the Wikipedia page might be a little overwhelming if you're not very math-y)
– Ben Bolker
Aug 24 at 0:10




For an implementation of the cross-product in C#, maybe check out docs.microsoft.com/en-us/dotnet/api/… ? (the Wikipedia page might be a little overwhelming if you're not very math-y)
– Ben Bolker
Aug 24 at 0:10










6 Answers
6






active

oldest

votes

















up vote
20
down vote



accepted










Compute $$ABtimes CD$$
which is zero for parallel lines.



In practice there are truncation errors and you won't get zero exactly, so it is better to compute the (Euclidean) norm and compare it to the product of the norms. Hence



$$(ABtimes CD)^2<epsilon^2,AB^2,CD^2.$$




Note that this is the same as normalizing the vectors to unit length and computing the norm of the cross-product, which is the sine of the angle between them.



So in the above formula, you have $epsilonapproxsinepsilon$ and $epsilon$ can be interpreted as an angle tolerance, in radians.






share|cite|improve this answer






















  • Thank you for the extra feedback, Yves. I just got extra information from an elderly colleague. It turned out we already had a built-in method to calculate the angle between two vectors, starting from calculating the cross product as suggested here.
    – JAlly
    Aug 27 at 14:43










  • @JAlly: as I wrote it, the expression is optimized to avoid divisions and trigonometric functions.
    – Yves Daoust
    Aug 27 at 14:45

















up vote
6
down vote













The two lines are parallel just when the following three ratios are all equal:
$$
fracax-bxcx-dx,
fracay-bycy-dy,
fracaz-bzcz-dz .
$$
It's easy to write a function that returns the boolean value you need. But the floating point calculations may be problematical. If any of the denominators is $0$ you will have to use the reciprocals. If your points are close together or some of the denominators are near $0$ you will encounter numerical instabilities in the fractions and in the test for equality. Take care. Program defensively.






share|cite|improve this answer
















  • 2




    The cross-product doesn't suffer these problems and allows to tame the numerical issues.
    – Yves Daoust
    Aug 23 at 14:56

















up vote
5
down vote













All you need to do is calculate the DotProduct. (Google "Dot Product" for more information.)



In detail:



If line #1 contains points A and B, and line #2 contains points C and D, then:



Calculate vector #1: Vector1 = A - B.



Calculate vector #2: Vector2 = C - D.



Then, normalize both vectors.



Then, calculate the dot product of the two vectors. (The dot product is a pretty standard operation for vectors so it's likely already in the C# library.) This will give you a value that ranges from -1.0 to 1.0.



If Vector1 and Vector2 are parallel, then the dot product will be 1.0. If the vector C->D happens to be going in the opposite direction as A->B, then the dot product will be -1.0, but the two lines will still be parallel.



There could be some rounding errors, so you could test if the dot product is greater than 0.99 or less than -0.99. In either case, the lines are parallel or nearly parallel.



If you google "dot product" there are some illustrations that describe the values of the dot product given different vectors. Here's one: http://www.kimonmatara.com/wp-content/uploads/2015/12/dot_prod.jpg






share|cite|improve this answer





























    up vote
    2
    down vote













    Hint: Write your equation in the form
    $$vecx=[ax,ay,az]+s[bx-ax,by-ay,bz-az]$$ where $s$ is a real number.
    Can you proceed? Or do you need further assistance?
    the other one
    $$vecx=[cx,cy,cz]+t[dx-cx,dy-cy,dz-cz]$$ where $t$ is a real number.
    Now you have to discover if exist a real number $Lambda such that



    $$[bx-ax,by-ay,bz-az]=lambda[dx-cx,dy-cy,dz-cz]$$






    share|cite|improve this answer




















    • Have you got an example for all parameters?
      – Dr. Sonnhard Graubner
      Aug 23 at 14:48










    • CS3DLine left is for example a point with following coördinates: A(0.5606601717797951,-0.18933982822044659,-1.8106601717795994) -> B(0.060660171779919336,-1.0428932188138047,-1.6642135623729404) CS3DLine righti s for example a point with following coördinates: C(0.060660171780597794,-1.0428932188138855,-1.6642135623730743)->D(0.56066017177995031,-0.18933982822021733,-1.8106601717797126) The long figures are due to transformations done, it all started with unity vectors. :)
      – JAlly
      Aug 23 at 14:55










    • Ok, to much numbers for me!
      – Dr. Sonnhard Graubner
      Aug 23 at 14:56

















    up vote
    1
    down vote













    Recall that given $2$ points $P$ and $Q$ the parametric equation for the line passing through them is



    $$P+t(Q-P)=P+tv$$



    with $v=Q-P$ the direction vector.



    Then, let consider the direction vectors



    • $v_AB=(ax-bx,ay-by,az-bz)$

    • $v_CD=(cx-dx,cy-dy,cz-dz)$

    if they are multiple, that is linearly dependent, the two lines are parallel.






    share|cite|improve this answer




















    • @JAlly You are welcome! Bye
      – gimusi
      Aug 23 at 14:47

















    up vote
    0
    down vote













    Write a helper function to calculate the dot product:



    double Dot(CSVector a, CSVector b)

    return a.X*b.X + a.Y*b.Y + a.Z*b.Z;



    then the two lines are parallel if:



    double productOfLengths = Math.Sqrt(Dot(left,left) * Dot(right, right));
    bool parallel = productOfLengths > epsilon && Math.Abs(Dot(left, right)) > Math.Cos(tolerance) * productOfLengths


    where tolerance is an angle (measured in radians) and epsilon catches the corner case where one or both of the vectors has length 0



    Unlike the solution you have now, this will work if the vectors are parallel or near-parallel to one of the coordinate axes.



    Also make sure you write unit tests, even if the math seems clear. Include corner cases, where one or more components of the vectors are 0 or close to 0, e.g.




    $left = (1e-12,1e-5,1); right = (1e-5,1e-8,1)$




    should be parallel and




    $left = (1e-5,1,0.1); right = (1e-12,0.2,1)$




    should not - I think your code gives exactly the opposite result.



    Note: I think this is essentially Brit Clousing's answer. But since you implemented the one answer that's performs worst numerically, I thought maybe his answer wasn't clear anough and some C# code would be helpful






    share|cite|improve this answer






















    • Regarding numerical stability, the choice between the dot product and cross-product is uneasy. It is worth to note that for small angles, the sine is roughly the argument, whereas the cosine is the quadratic expression 1-t²/2 having an extremum at 0, so that the indeterminacy on the angle is higher.
      – Yves Daoust
      Aug 24 at 12:42











    • @YvesDaoust: I don't think the choice is uneasy - cross product is more stable, numerically, for exactly the reasons you said. But my impression was that the tolerance the OP is looking for is so far from accuracy limits that it didn't matter. And the dot product is (slightly) easier to implement.
      – nikie
      Aug 24 at 20:45










    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "69"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    noCode: true, onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2892165%2fgeometry-how-to-determine-if-two-lines-are-parallel-in-3d-based-on-coordinates%23new-answer', 'question_page');

    );

    Post as a guest






























    6 Answers
    6






    active

    oldest

    votes








    6 Answers
    6






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    20
    down vote



    accepted










    Compute $$ABtimes CD$$
    which is zero for parallel lines.



    In practice there are truncation errors and you won't get zero exactly, so it is better to compute the (Euclidean) norm and compare it to the product of the norms. Hence



    $$(ABtimes CD)^2<epsilon^2,AB^2,CD^2.$$




    Note that this is the same as normalizing the vectors to unit length and computing the norm of the cross-product, which is the sine of the angle between them.



    So in the above formula, you have $epsilonapproxsinepsilon$ and $epsilon$ can be interpreted as an angle tolerance, in radians.






    share|cite|improve this answer






















    • Thank you for the extra feedback, Yves. I just got extra information from an elderly colleague. It turned out we already had a built-in method to calculate the angle between two vectors, starting from calculating the cross product as suggested here.
      – JAlly
      Aug 27 at 14:43










    • @JAlly: as I wrote it, the expression is optimized to avoid divisions and trigonometric functions.
      – Yves Daoust
      Aug 27 at 14:45














    up vote
    20
    down vote



    accepted










    Compute $$ABtimes CD$$
    which is zero for parallel lines.



    In practice there are truncation errors and you won't get zero exactly, so it is better to compute the (Euclidean) norm and compare it to the product of the norms. Hence



    $$(ABtimes CD)^2<epsilon^2,AB^2,CD^2.$$




    Note that this is the same as normalizing the vectors to unit length and computing the norm of the cross-product, which is the sine of the angle between them.



    So in the above formula, you have $epsilonapproxsinepsilon$ and $epsilon$ can be interpreted as an angle tolerance, in radians.






    share|cite|improve this answer






















    • Thank you for the extra feedback, Yves. I just got extra information from an elderly colleague. It turned out we already had a built-in method to calculate the angle between two vectors, starting from calculating the cross product as suggested here.
      – JAlly
      Aug 27 at 14:43










    • @JAlly: as I wrote it, the expression is optimized to avoid divisions and trigonometric functions.
      – Yves Daoust
      Aug 27 at 14:45












    up vote
    20
    down vote



    accepted







    up vote
    20
    down vote



    accepted






    Compute $$ABtimes CD$$
    which is zero for parallel lines.



    In practice there are truncation errors and you won't get zero exactly, so it is better to compute the (Euclidean) norm and compare it to the product of the norms. Hence



    $$(ABtimes CD)^2<epsilon^2,AB^2,CD^2.$$




    Note that this is the same as normalizing the vectors to unit length and computing the norm of the cross-product, which is the sine of the angle between them.



    So in the above formula, you have $epsilonapproxsinepsilon$ and $epsilon$ can be interpreted as an angle tolerance, in radians.






    share|cite|improve this answer














    Compute $$ABtimes CD$$
    which is zero for parallel lines.



    In practice there are truncation errors and you won't get zero exactly, so it is better to compute the (Euclidean) norm and compare it to the product of the norms. Hence



    $$(ABtimes CD)^2<epsilon^2,AB^2,CD^2.$$




    Note that this is the same as normalizing the vectors to unit length and computing the norm of the cross-product, which is the sine of the angle between them.



    So in the above formula, you have $epsilonapproxsinepsilon$ and $epsilon$ can be interpreted as an angle tolerance, in radians.







    share|cite|improve this answer














    share|cite|improve this answer



    share|cite|improve this answer








    edited Aug 24 at 8:50

























    answered Aug 23 at 14:20









    Yves Daoust

    115k667209




    115k667209











    • Thank you for the extra feedback, Yves. I just got extra information from an elderly colleague. It turned out we already had a built-in method to calculate the angle between two vectors, starting from calculating the cross product as suggested here.
      – JAlly
      Aug 27 at 14:43










    • @JAlly: as I wrote it, the expression is optimized to avoid divisions and trigonometric functions.
      – Yves Daoust
      Aug 27 at 14:45
















    • Thank you for the extra feedback, Yves. I just got extra information from an elderly colleague. It turned out we already had a built-in method to calculate the angle between two vectors, starting from calculating the cross product as suggested here.
      – JAlly
      Aug 27 at 14:43










    • @JAlly: as I wrote it, the expression is optimized to avoid divisions and trigonometric functions.
      – Yves Daoust
      Aug 27 at 14:45















    Thank you for the extra feedback, Yves. I just got extra information from an elderly colleague. It turned out we already had a built-in method to calculate the angle between two vectors, starting from calculating the cross product as suggested here.
    – JAlly
    Aug 27 at 14:43




    Thank you for the extra feedback, Yves. I just got extra information from an elderly colleague. It turned out we already had a built-in method to calculate the angle between two vectors, starting from calculating the cross product as suggested here.
    – JAlly
    Aug 27 at 14:43












    @JAlly: as I wrote it, the expression is optimized to avoid divisions and trigonometric functions.
    – Yves Daoust
    Aug 27 at 14:45




    @JAlly: as I wrote it, the expression is optimized to avoid divisions and trigonometric functions.
    – Yves Daoust
    Aug 27 at 14:45










    up vote
    6
    down vote













    The two lines are parallel just when the following three ratios are all equal:
    $$
    fracax-bxcx-dx,
    fracay-bycy-dy,
    fracaz-bzcz-dz .
    $$
    It's easy to write a function that returns the boolean value you need. But the floating point calculations may be problematical. If any of the denominators is $0$ you will have to use the reciprocals. If your points are close together or some of the denominators are near $0$ you will encounter numerical instabilities in the fractions and in the test for equality. Take care. Program defensively.






    share|cite|improve this answer
















    • 2




      The cross-product doesn't suffer these problems and allows to tame the numerical issues.
      – Yves Daoust
      Aug 23 at 14:56














    up vote
    6
    down vote













    The two lines are parallel just when the following three ratios are all equal:
    $$
    fracax-bxcx-dx,
    fracay-bycy-dy,
    fracaz-bzcz-dz .
    $$
    It's easy to write a function that returns the boolean value you need. But the floating point calculations may be problematical. If any of the denominators is $0$ you will have to use the reciprocals. If your points are close together or some of the denominators are near $0$ you will encounter numerical instabilities in the fractions and in the test for equality. Take care. Program defensively.






    share|cite|improve this answer
















    • 2




      The cross-product doesn't suffer these problems and allows to tame the numerical issues.
      – Yves Daoust
      Aug 23 at 14:56












    up vote
    6
    down vote










    up vote
    6
    down vote









    The two lines are parallel just when the following three ratios are all equal:
    $$
    fracax-bxcx-dx,
    fracay-bycy-dy,
    fracaz-bzcz-dz .
    $$
    It's easy to write a function that returns the boolean value you need. But the floating point calculations may be problematical. If any of the denominators is $0$ you will have to use the reciprocals. If your points are close together or some of the denominators are near $0$ you will encounter numerical instabilities in the fractions and in the test for equality. Take care. Program defensively.






    share|cite|improve this answer












    The two lines are parallel just when the following three ratios are all equal:
    $$
    fracax-bxcx-dx,
    fracay-bycy-dy,
    fracaz-bzcz-dz .
    $$
    It's easy to write a function that returns the boolean value you need. But the floating point calculations may be problematical. If any of the denominators is $0$ you will have to use the reciprocals. If your points are close together or some of the denominators are near $0$ you will encounter numerical instabilities in the fractions and in the test for equality. Take care. Program defensively.







    share|cite|improve this answer












    share|cite|improve this answer



    share|cite|improve this answer










    answered Aug 23 at 14:24









    Ethan Bolker

    36.7k54299




    36.7k54299







    • 2




      The cross-product doesn't suffer these problems and allows to tame the numerical issues.
      – Yves Daoust
      Aug 23 at 14:56












    • 2




      The cross-product doesn't suffer these problems and allows to tame the numerical issues.
      – Yves Daoust
      Aug 23 at 14:56







    2




    2




    The cross-product doesn't suffer these problems and allows to tame the numerical issues.
    – Yves Daoust
    Aug 23 at 14:56




    The cross-product doesn't suffer these problems and allows to tame the numerical issues.
    – Yves Daoust
    Aug 23 at 14:56










    up vote
    5
    down vote













    All you need to do is calculate the DotProduct. (Google "Dot Product" for more information.)



    In detail:



    If line #1 contains points A and B, and line #2 contains points C and D, then:



    Calculate vector #1: Vector1 = A - B.



    Calculate vector #2: Vector2 = C - D.



    Then, normalize both vectors.



    Then, calculate the dot product of the two vectors. (The dot product is a pretty standard operation for vectors so it's likely already in the C# library.) This will give you a value that ranges from -1.0 to 1.0.



    If Vector1 and Vector2 are parallel, then the dot product will be 1.0. If the vector C->D happens to be going in the opposite direction as A->B, then the dot product will be -1.0, but the two lines will still be parallel.



    There could be some rounding errors, so you could test if the dot product is greater than 0.99 or less than -0.99. In either case, the lines are parallel or nearly parallel.



    If you google "dot product" there are some illustrations that describe the values of the dot product given different vectors. Here's one: http://www.kimonmatara.com/wp-content/uploads/2015/12/dot_prod.jpg






    share|cite|improve this answer


























      up vote
      5
      down vote













      All you need to do is calculate the DotProduct. (Google "Dot Product" for more information.)



      In detail:



      If line #1 contains points A and B, and line #2 contains points C and D, then:



      Calculate vector #1: Vector1 = A - B.



      Calculate vector #2: Vector2 = C - D.



      Then, normalize both vectors.



      Then, calculate the dot product of the two vectors. (The dot product is a pretty standard operation for vectors so it's likely already in the C# library.) This will give you a value that ranges from -1.0 to 1.0.



      If Vector1 and Vector2 are parallel, then the dot product will be 1.0. If the vector C->D happens to be going in the opposite direction as A->B, then the dot product will be -1.0, but the two lines will still be parallel.



      There could be some rounding errors, so you could test if the dot product is greater than 0.99 or less than -0.99. In either case, the lines are parallel or nearly parallel.



      If you google "dot product" there are some illustrations that describe the values of the dot product given different vectors. Here's one: http://www.kimonmatara.com/wp-content/uploads/2015/12/dot_prod.jpg






      share|cite|improve this answer
























        up vote
        5
        down vote










        up vote
        5
        down vote









        All you need to do is calculate the DotProduct. (Google "Dot Product" for more information.)



        In detail:



        If line #1 contains points A and B, and line #2 contains points C and D, then:



        Calculate vector #1: Vector1 = A - B.



        Calculate vector #2: Vector2 = C - D.



        Then, normalize both vectors.



        Then, calculate the dot product of the two vectors. (The dot product is a pretty standard operation for vectors so it's likely already in the C# library.) This will give you a value that ranges from -1.0 to 1.0.



        If Vector1 and Vector2 are parallel, then the dot product will be 1.0. If the vector C->D happens to be going in the opposite direction as A->B, then the dot product will be -1.0, but the two lines will still be parallel.



        There could be some rounding errors, so you could test if the dot product is greater than 0.99 or less than -0.99. In either case, the lines are parallel or nearly parallel.



        If you google "dot product" there are some illustrations that describe the values of the dot product given different vectors. Here's one: http://www.kimonmatara.com/wp-content/uploads/2015/12/dot_prod.jpg






        share|cite|improve this answer














        All you need to do is calculate the DotProduct. (Google "Dot Product" for more information.)



        In detail:



        If line #1 contains points A and B, and line #2 contains points C and D, then:



        Calculate vector #1: Vector1 = A - B.



        Calculate vector #2: Vector2 = C - D.



        Then, normalize both vectors.



        Then, calculate the dot product of the two vectors. (The dot product is a pretty standard operation for vectors so it's likely already in the C# library.) This will give you a value that ranges from -1.0 to 1.0.



        If Vector1 and Vector2 are parallel, then the dot product will be 1.0. If the vector C->D happens to be going in the opposite direction as A->B, then the dot product will be -1.0, but the two lines will still be parallel.



        There could be some rounding errors, so you could test if the dot product is greater than 0.99 or less than -0.99. In either case, the lines are parallel or nearly parallel.



        If you google "dot product" there are some illustrations that describe the values of the dot product given different vectors. Here's one: http://www.kimonmatara.com/wp-content/uploads/2015/12/dot_prod.jpg







        share|cite|improve this answer














        share|cite|improve this answer



        share|cite|improve this answer








        edited Aug 24 at 0:14

























        answered Aug 24 at 0:06









        Brit Clousing

        1512




        1512




















            up vote
            2
            down vote













            Hint: Write your equation in the form
            $$vecx=[ax,ay,az]+s[bx-ax,by-ay,bz-az]$$ where $s$ is a real number.
            Can you proceed? Or do you need further assistance?
            the other one
            $$vecx=[cx,cy,cz]+t[dx-cx,dy-cy,dz-cz]$$ where $t$ is a real number.
            Now you have to discover if exist a real number $Lambda such that



            $$[bx-ax,by-ay,bz-az]=lambda[dx-cx,dy-cy,dz-cz]$$






            share|cite|improve this answer




















            • Have you got an example for all parameters?
              – Dr. Sonnhard Graubner
              Aug 23 at 14:48










            • CS3DLine left is for example a point with following coördinates: A(0.5606601717797951,-0.18933982822044659,-1.8106601717795994) -> B(0.060660171779919336,-1.0428932188138047,-1.6642135623729404) CS3DLine righti s for example a point with following coördinates: C(0.060660171780597794,-1.0428932188138855,-1.6642135623730743)->D(0.56066017177995031,-0.18933982822021733,-1.8106601717797126) The long figures are due to transformations done, it all started with unity vectors. :)
              – JAlly
              Aug 23 at 14:55










            • Ok, to much numbers for me!
              – Dr. Sonnhard Graubner
              Aug 23 at 14:56














            up vote
            2
            down vote













            Hint: Write your equation in the form
            $$vecx=[ax,ay,az]+s[bx-ax,by-ay,bz-az]$$ where $s$ is a real number.
            Can you proceed? Or do you need further assistance?
            the other one
            $$vecx=[cx,cy,cz]+t[dx-cx,dy-cy,dz-cz]$$ where $t$ is a real number.
            Now you have to discover if exist a real number $Lambda such that



            $$[bx-ax,by-ay,bz-az]=lambda[dx-cx,dy-cy,dz-cz]$$






            share|cite|improve this answer




















            • Have you got an example for all parameters?
              – Dr. Sonnhard Graubner
              Aug 23 at 14:48










            • CS3DLine left is for example a point with following coördinates: A(0.5606601717797951,-0.18933982822044659,-1.8106601717795994) -> B(0.060660171779919336,-1.0428932188138047,-1.6642135623729404) CS3DLine righti s for example a point with following coördinates: C(0.060660171780597794,-1.0428932188138855,-1.6642135623730743)->D(0.56066017177995031,-0.18933982822021733,-1.8106601717797126) The long figures are due to transformations done, it all started with unity vectors. :)
              – JAlly
              Aug 23 at 14:55










            • Ok, to much numbers for me!
              – Dr. Sonnhard Graubner
              Aug 23 at 14:56












            up vote
            2
            down vote










            up vote
            2
            down vote









            Hint: Write your equation in the form
            $$vecx=[ax,ay,az]+s[bx-ax,by-ay,bz-az]$$ where $s$ is a real number.
            Can you proceed? Or do you need further assistance?
            the other one
            $$vecx=[cx,cy,cz]+t[dx-cx,dy-cy,dz-cz]$$ where $t$ is a real number.
            Now you have to discover if exist a real number $Lambda such that



            $$[bx-ax,by-ay,bz-az]=lambda[dx-cx,dy-cy,dz-cz]$$






            share|cite|improve this answer












            Hint: Write your equation in the form
            $$vecx=[ax,ay,az]+s[bx-ax,by-ay,bz-az]$$ where $s$ is a real number.
            Can you proceed? Or do you need further assistance?
            the other one
            $$vecx=[cx,cy,cz]+t[dx-cx,dy-cy,dz-cz]$$ where $t$ is a real number.
            Now you have to discover if exist a real number $Lambda such that



            $$[bx-ax,by-ay,bz-az]=lambda[dx-cx,dy-cy,dz-cz]$$







            share|cite|improve this answer












            share|cite|improve this answer



            share|cite|improve this answer










            answered Aug 23 at 14:15









            Dr. Sonnhard Graubner

            69.1k32761




            69.1k32761











            • Have you got an example for all parameters?
              – Dr. Sonnhard Graubner
              Aug 23 at 14:48










            • CS3DLine left is for example a point with following coördinates: A(0.5606601717797951,-0.18933982822044659,-1.8106601717795994) -> B(0.060660171779919336,-1.0428932188138047,-1.6642135623729404) CS3DLine righti s for example a point with following coördinates: C(0.060660171780597794,-1.0428932188138855,-1.6642135623730743)->D(0.56066017177995031,-0.18933982822021733,-1.8106601717797126) The long figures are due to transformations done, it all started with unity vectors. :)
              – JAlly
              Aug 23 at 14:55










            • Ok, to much numbers for me!
              – Dr. Sonnhard Graubner
              Aug 23 at 14:56
















            • Have you got an example for all parameters?
              – Dr. Sonnhard Graubner
              Aug 23 at 14:48










            • CS3DLine left is for example a point with following coördinates: A(0.5606601717797951,-0.18933982822044659,-1.8106601717795994) -> B(0.060660171779919336,-1.0428932188138047,-1.6642135623729404) CS3DLine righti s for example a point with following coördinates: C(0.060660171780597794,-1.0428932188138855,-1.6642135623730743)->D(0.56066017177995031,-0.18933982822021733,-1.8106601717797126) The long figures are due to transformations done, it all started with unity vectors. :)
              – JAlly
              Aug 23 at 14:55










            • Ok, to much numbers for me!
              – Dr. Sonnhard Graubner
              Aug 23 at 14:56















            Have you got an example for all parameters?
            – Dr. Sonnhard Graubner
            Aug 23 at 14:48




            Have you got an example for all parameters?
            – Dr. Sonnhard Graubner
            Aug 23 at 14:48












            CS3DLine left is for example a point with following coördinates: A(0.5606601717797951,-0.18933982822044659,-1.8106601717795994) -> B(0.060660171779919336,-1.0428932188138047,-1.6642135623729404) CS3DLine righti s for example a point with following coördinates: C(0.060660171780597794,-1.0428932188138855,-1.6642135623730743)->D(0.56066017177995031,-0.18933982822021733,-1.8106601717797126) The long figures are due to transformations done, it all started with unity vectors. :)
            – JAlly
            Aug 23 at 14:55




            CS3DLine left is for example a point with following coördinates: A(0.5606601717797951,-0.18933982822044659,-1.8106601717795994) -> B(0.060660171779919336,-1.0428932188138047,-1.6642135623729404) CS3DLine righti s for example a point with following coördinates: C(0.060660171780597794,-1.0428932188138855,-1.6642135623730743)->D(0.56066017177995031,-0.18933982822021733,-1.8106601717797126) The long figures are due to transformations done, it all started with unity vectors. :)
            – JAlly
            Aug 23 at 14:55












            Ok, to much numbers for me!
            – Dr. Sonnhard Graubner
            Aug 23 at 14:56




            Ok, to much numbers for me!
            – Dr. Sonnhard Graubner
            Aug 23 at 14:56










            up vote
            1
            down vote













            Recall that given $2$ points $P$ and $Q$ the parametric equation for the line passing through them is



            $$P+t(Q-P)=P+tv$$



            with $v=Q-P$ the direction vector.



            Then, let consider the direction vectors



            • $v_AB=(ax-bx,ay-by,az-bz)$

            • $v_CD=(cx-dx,cy-dy,cz-dz)$

            if they are multiple, that is linearly dependent, the two lines are parallel.






            share|cite|improve this answer




















            • @JAlly You are welcome! Bye
              – gimusi
              Aug 23 at 14:47














            up vote
            1
            down vote













            Recall that given $2$ points $P$ and $Q$ the parametric equation for the line passing through them is



            $$P+t(Q-P)=P+tv$$



            with $v=Q-P$ the direction vector.



            Then, let consider the direction vectors



            • $v_AB=(ax-bx,ay-by,az-bz)$

            • $v_CD=(cx-dx,cy-dy,cz-dz)$

            if they are multiple, that is linearly dependent, the two lines are parallel.






            share|cite|improve this answer




















            • @JAlly You are welcome! Bye
              – gimusi
              Aug 23 at 14:47












            up vote
            1
            down vote










            up vote
            1
            down vote









            Recall that given $2$ points $P$ and $Q$ the parametric equation for the line passing through them is



            $$P+t(Q-P)=P+tv$$



            with $v=Q-P$ the direction vector.



            Then, let consider the direction vectors



            • $v_AB=(ax-bx,ay-by,az-bz)$

            • $v_CD=(cx-dx,cy-dy,cz-dz)$

            if they are multiple, that is linearly dependent, the two lines are parallel.






            share|cite|improve this answer












            Recall that given $2$ points $P$ and $Q$ the parametric equation for the line passing through them is



            $$P+t(Q-P)=P+tv$$



            with $v=Q-P$ the direction vector.



            Then, let consider the direction vectors



            • $v_AB=(ax-bx,ay-by,az-bz)$

            • $v_CD=(cx-dx,cy-dy,cz-dz)$

            if they are multiple, that is linearly dependent, the two lines are parallel.







            share|cite|improve this answer












            share|cite|improve this answer



            share|cite|improve this answer










            answered Aug 23 at 14:13









            gimusi

            74.3k73889




            74.3k73889











            • @JAlly You are welcome! Bye
              – gimusi
              Aug 23 at 14:47
















            • @JAlly You are welcome! Bye
              – gimusi
              Aug 23 at 14:47















            @JAlly You are welcome! Bye
            – gimusi
            Aug 23 at 14:47




            @JAlly You are welcome! Bye
            – gimusi
            Aug 23 at 14:47










            up vote
            0
            down vote













            Write a helper function to calculate the dot product:



            double Dot(CSVector a, CSVector b)

            return a.X*b.X + a.Y*b.Y + a.Z*b.Z;



            then the two lines are parallel if:



            double productOfLengths = Math.Sqrt(Dot(left,left) * Dot(right, right));
            bool parallel = productOfLengths > epsilon && Math.Abs(Dot(left, right)) > Math.Cos(tolerance) * productOfLengths


            where tolerance is an angle (measured in radians) and epsilon catches the corner case where one or both of the vectors has length 0



            Unlike the solution you have now, this will work if the vectors are parallel or near-parallel to one of the coordinate axes.



            Also make sure you write unit tests, even if the math seems clear. Include corner cases, where one or more components of the vectors are 0 or close to 0, e.g.




            $left = (1e-12,1e-5,1); right = (1e-5,1e-8,1)$




            should be parallel and




            $left = (1e-5,1,0.1); right = (1e-12,0.2,1)$




            should not - I think your code gives exactly the opposite result.



            Note: I think this is essentially Brit Clousing's answer. But since you implemented the one answer that's performs worst numerically, I thought maybe his answer wasn't clear anough and some C# code would be helpful






            share|cite|improve this answer






















            • Regarding numerical stability, the choice between the dot product and cross-product is uneasy. It is worth to note that for small angles, the sine is roughly the argument, whereas the cosine is the quadratic expression 1-t²/2 having an extremum at 0, so that the indeterminacy on the angle is higher.
              – Yves Daoust
              Aug 24 at 12:42











            • @YvesDaoust: I don't think the choice is uneasy - cross product is more stable, numerically, for exactly the reasons you said. But my impression was that the tolerance the OP is looking for is so far from accuracy limits that it didn't matter. And the dot product is (slightly) easier to implement.
              – nikie
              Aug 24 at 20:45














            up vote
            0
            down vote













            Write a helper function to calculate the dot product:



            double Dot(CSVector a, CSVector b)

            return a.X*b.X + a.Y*b.Y + a.Z*b.Z;



            then the two lines are parallel if:



            double productOfLengths = Math.Sqrt(Dot(left,left) * Dot(right, right));
            bool parallel = productOfLengths > epsilon && Math.Abs(Dot(left, right)) > Math.Cos(tolerance) * productOfLengths


            where tolerance is an angle (measured in radians) and epsilon catches the corner case where one or both of the vectors has length 0



            Unlike the solution you have now, this will work if the vectors are parallel or near-parallel to one of the coordinate axes.



            Also make sure you write unit tests, even if the math seems clear. Include corner cases, where one or more components of the vectors are 0 or close to 0, e.g.




            $left = (1e-12,1e-5,1); right = (1e-5,1e-8,1)$




            should be parallel and




            $left = (1e-5,1,0.1); right = (1e-12,0.2,1)$




            should not - I think your code gives exactly the opposite result.



            Note: I think this is essentially Brit Clousing's answer. But since you implemented the one answer that's performs worst numerically, I thought maybe his answer wasn't clear anough and some C# code would be helpful






            share|cite|improve this answer






















            • Regarding numerical stability, the choice between the dot product and cross-product is uneasy. It is worth to note that for small angles, the sine is roughly the argument, whereas the cosine is the quadratic expression 1-t²/2 having an extremum at 0, so that the indeterminacy on the angle is higher.
              – Yves Daoust
              Aug 24 at 12:42











            • @YvesDaoust: I don't think the choice is uneasy - cross product is more stable, numerically, for exactly the reasons you said. But my impression was that the tolerance the OP is looking for is so far from accuracy limits that it didn't matter. And the dot product is (slightly) easier to implement.
              – nikie
              Aug 24 at 20:45












            up vote
            0
            down vote










            up vote
            0
            down vote









            Write a helper function to calculate the dot product:



            double Dot(CSVector a, CSVector b)

            return a.X*b.X + a.Y*b.Y + a.Z*b.Z;



            then the two lines are parallel if:



            double productOfLengths = Math.Sqrt(Dot(left,left) * Dot(right, right));
            bool parallel = productOfLengths > epsilon && Math.Abs(Dot(left, right)) > Math.Cos(tolerance) * productOfLengths


            where tolerance is an angle (measured in radians) and epsilon catches the corner case where one or both of the vectors has length 0



            Unlike the solution you have now, this will work if the vectors are parallel or near-parallel to one of the coordinate axes.



            Also make sure you write unit tests, even if the math seems clear. Include corner cases, where one or more components of the vectors are 0 or close to 0, e.g.




            $left = (1e-12,1e-5,1); right = (1e-5,1e-8,1)$




            should be parallel and




            $left = (1e-5,1,0.1); right = (1e-12,0.2,1)$




            should not - I think your code gives exactly the opposite result.



            Note: I think this is essentially Brit Clousing's answer. But since you implemented the one answer that's performs worst numerically, I thought maybe his answer wasn't clear anough and some C# code would be helpful






            share|cite|improve this answer














            Write a helper function to calculate the dot product:



            double Dot(CSVector a, CSVector b)

            return a.X*b.X + a.Y*b.Y + a.Z*b.Z;



            then the two lines are parallel if:



            double productOfLengths = Math.Sqrt(Dot(left,left) * Dot(right, right));
            bool parallel = productOfLengths > epsilon && Math.Abs(Dot(left, right)) > Math.Cos(tolerance) * productOfLengths


            where tolerance is an angle (measured in radians) and epsilon catches the corner case where one or both of the vectors has length 0



            Unlike the solution you have now, this will work if the vectors are parallel or near-parallel to one of the coordinate axes.



            Also make sure you write unit tests, even if the math seems clear. Include corner cases, where one or more components of the vectors are 0 or close to 0, e.g.




            $left = (1e-12,1e-5,1); right = (1e-5,1e-8,1)$




            should be parallel and




            $left = (1e-5,1,0.1); right = (1e-12,0.2,1)$




            should not - I think your code gives exactly the opposite result.



            Note: I think this is essentially Brit Clousing's answer. But since you implemented the one answer that's performs worst numerically, I thought maybe his answer wasn't clear anough and some C# code would be helpful







            share|cite|improve this answer














            share|cite|improve this answer



            share|cite|improve this answer








            edited Aug 24 at 7:37

























            answered Aug 24 at 7:11









            nikie

            18117




            18117











            • Regarding numerical stability, the choice between the dot product and cross-product is uneasy. It is worth to note that for small angles, the sine is roughly the argument, whereas the cosine is the quadratic expression 1-t²/2 having an extremum at 0, so that the indeterminacy on the angle is higher.
              – Yves Daoust
              Aug 24 at 12:42











            • @YvesDaoust: I don't think the choice is uneasy - cross product is more stable, numerically, for exactly the reasons you said. But my impression was that the tolerance the OP is looking for is so far from accuracy limits that it didn't matter. And the dot product is (slightly) easier to implement.
              – nikie
              Aug 24 at 20:45
















            • Regarding numerical stability, the choice between the dot product and cross-product is uneasy. It is worth to note that for small angles, the sine is roughly the argument, whereas the cosine is the quadratic expression 1-t²/2 having an extremum at 0, so that the indeterminacy on the angle is higher.
              – Yves Daoust
              Aug 24 at 12:42











            • @YvesDaoust: I don't think the choice is uneasy - cross product is more stable, numerically, for exactly the reasons you said. But my impression was that the tolerance the OP is looking for is so far from accuracy limits that it didn't matter. And the dot product is (slightly) easier to implement.
              – nikie
              Aug 24 at 20:45















            Regarding numerical stability, the choice between the dot product and cross-product is uneasy. It is worth to note that for small angles, the sine is roughly the argument, whereas the cosine is the quadratic expression 1-t²/2 having an extremum at 0, so that the indeterminacy on the angle is higher.
            – Yves Daoust
            Aug 24 at 12:42





            Regarding numerical stability, the choice between the dot product and cross-product is uneasy. It is worth to note that for small angles, the sine is roughly the argument, whereas the cosine is the quadratic expression 1-t²/2 having an extremum at 0, so that the indeterminacy on the angle is higher.
            – Yves Daoust
            Aug 24 at 12:42













            @YvesDaoust: I don't think the choice is uneasy - cross product is more stable, numerically, for exactly the reasons you said. But my impression was that the tolerance the OP is looking for is so far from accuracy limits that it didn't matter. And the dot product is (slightly) easier to implement.
            – nikie
            Aug 24 at 20:45




            @YvesDaoust: I don't think the choice is uneasy - cross product is more stable, numerically, for exactly the reasons you said. But my impression was that the tolerance the OP is looking for is so far from accuracy limits that it didn't matter. And the dot product is (slightly) easier to implement.
            – nikie
            Aug 24 at 20:45

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f2892165%2fgeometry-how-to-determine-if-two-lines-are-parallel-in-3d-based-on-coordinates%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay