Finding the closest point on a line to another point












1















I have a affine equation y = ax + b where a is the coefficient (coeff).
Let D be a line that goes through axis and described by the previous equation.



I'm trying with this piece of code to find the coordinates of the closest point on D to position (ignoring the y coordinate, because 2D in 3D)



    double a = coeff;
double b = position.getZ();
double c = axis.getZ() - axis.getX() * coeff;
double x0 = position.getX();
double y0 = position.getZ();

return new Vector((b * (b * x0 - a * y0) - a * c) / (a * a + b * b), position.getY(),
(a * (-b * x0 + a * y0) - b * c) / (a * a + b * b));


Using this as a refernece



However, this does not work and return weird results










share|improve this question























  • How are you not getting a compile error? afaik Vector has no constructor for three arguments: docs.oracle.com/javase/8/docs/api/java/util/Vector.html

    – Jere
    Nov 20 '18 at 22:11











  • What is "position" in your code? Is it an instance of a point in 3d? If so, then getZ is probably getting the 3rd dim and not what you'd want here.

    – sofend
    Nov 20 '18 at 22:24











  • I think this is a 3D solution. (Top tip: Write the simplest thing first. Then adapt it. Possibly keep the original solution in a separate method and call that.) @Jere Vector doesn't appear to be java.util.Vector but a 3D Vector. @sofend axis appears to refer to one of the X/Y/Z axises. Can't be bothered to look through this in detail given the state of the question, but straight away, if converting to the cited but not quoted Wikipedia article, it looks as if a should be -coeffs. Also confusing using the same letters for different things.

    – Tom Hawtin - tackline
    Nov 20 '18 at 22:43













  • @TomHawtin-tackline yes Vector is a homemade class. Position is another vector, but I only want the closest point on the (x,z) plane, so that's why I'm not using the y coordinate. But I don't see how -coeff instead of coeff for a should change anything, a can be negative or positive

    – Vinz243
    Nov 21 '18 at 8:07


















1















I have a affine equation y = ax + b where a is the coefficient (coeff).
Let D be a line that goes through axis and described by the previous equation.



I'm trying with this piece of code to find the coordinates of the closest point on D to position (ignoring the y coordinate, because 2D in 3D)



    double a = coeff;
double b = position.getZ();
double c = axis.getZ() - axis.getX() * coeff;
double x0 = position.getX();
double y0 = position.getZ();

return new Vector((b * (b * x0 - a * y0) - a * c) / (a * a + b * b), position.getY(),
(a * (-b * x0 + a * y0) - b * c) / (a * a + b * b));


Using this as a refernece



However, this does not work and return weird results










share|improve this question























  • How are you not getting a compile error? afaik Vector has no constructor for three arguments: docs.oracle.com/javase/8/docs/api/java/util/Vector.html

    – Jere
    Nov 20 '18 at 22:11











  • What is "position" in your code? Is it an instance of a point in 3d? If so, then getZ is probably getting the 3rd dim and not what you'd want here.

    – sofend
    Nov 20 '18 at 22:24











  • I think this is a 3D solution. (Top tip: Write the simplest thing first. Then adapt it. Possibly keep the original solution in a separate method and call that.) @Jere Vector doesn't appear to be java.util.Vector but a 3D Vector. @sofend axis appears to refer to one of the X/Y/Z axises. Can't be bothered to look through this in detail given the state of the question, but straight away, if converting to the cited but not quoted Wikipedia article, it looks as if a should be -coeffs. Also confusing using the same letters for different things.

    – Tom Hawtin - tackline
    Nov 20 '18 at 22:43













  • @TomHawtin-tackline yes Vector is a homemade class. Position is another vector, but I only want the closest point on the (x,z) plane, so that's why I'm not using the y coordinate. But I don't see how -coeff instead of coeff for a should change anything, a can be negative or positive

    – Vinz243
    Nov 21 '18 at 8:07
















1












1








1








I have a affine equation y = ax + b where a is the coefficient (coeff).
Let D be a line that goes through axis and described by the previous equation.



I'm trying with this piece of code to find the coordinates of the closest point on D to position (ignoring the y coordinate, because 2D in 3D)



    double a = coeff;
double b = position.getZ();
double c = axis.getZ() - axis.getX() * coeff;
double x0 = position.getX();
double y0 = position.getZ();

return new Vector((b * (b * x0 - a * y0) - a * c) / (a * a + b * b), position.getY(),
(a * (-b * x0 + a * y0) - b * c) / (a * a + b * b));


Using this as a refernece



However, this does not work and return weird results










share|improve this question














I have a affine equation y = ax + b where a is the coefficient (coeff).
Let D be a line that goes through axis and described by the previous equation.



I'm trying with this piece of code to find the coordinates of the closest point on D to position (ignoring the y coordinate, because 2D in 3D)



    double a = coeff;
double b = position.getZ();
double c = axis.getZ() - axis.getX() * coeff;
double x0 = position.getX();
double y0 = position.getZ();

return new Vector((b * (b * x0 - a * y0) - a * c) / (a * a + b * b), position.getY(),
(a * (-b * x0 + a * y0) - b * c) / (a * a + b * b));


Using this as a refernece



However, this does not work and return weird results







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 21:54









Vinz243Vinz243

3,91153165




3,91153165













  • How are you not getting a compile error? afaik Vector has no constructor for three arguments: docs.oracle.com/javase/8/docs/api/java/util/Vector.html

    – Jere
    Nov 20 '18 at 22:11











  • What is "position" in your code? Is it an instance of a point in 3d? If so, then getZ is probably getting the 3rd dim and not what you'd want here.

    – sofend
    Nov 20 '18 at 22:24











  • I think this is a 3D solution. (Top tip: Write the simplest thing first. Then adapt it. Possibly keep the original solution in a separate method and call that.) @Jere Vector doesn't appear to be java.util.Vector but a 3D Vector. @sofend axis appears to refer to one of the X/Y/Z axises. Can't be bothered to look through this in detail given the state of the question, but straight away, if converting to the cited but not quoted Wikipedia article, it looks as if a should be -coeffs. Also confusing using the same letters for different things.

    – Tom Hawtin - tackline
    Nov 20 '18 at 22:43













  • @TomHawtin-tackline yes Vector is a homemade class. Position is another vector, but I only want the closest point on the (x,z) plane, so that's why I'm not using the y coordinate. But I don't see how -coeff instead of coeff for a should change anything, a can be negative or positive

    – Vinz243
    Nov 21 '18 at 8:07





















  • How are you not getting a compile error? afaik Vector has no constructor for three arguments: docs.oracle.com/javase/8/docs/api/java/util/Vector.html

    – Jere
    Nov 20 '18 at 22:11











  • What is "position" in your code? Is it an instance of a point in 3d? If so, then getZ is probably getting the 3rd dim and not what you'd want here.

    – sofend
    Nov 20 '18 at 22:24











  • I think this is a 3D solution. (Top tip: Write the simplest thing first. Then adapt it. Possibly keep the original solution in a separate method and call that.) @Jere Vector doesn't appear to be java.util.Vector but a 3D Vector. @sofend axis appears to refer to one of the X/Y/Z axises. Can't be bothered to look through this in detail given the state of the question, but straight away, if converting to the cited but not quoted Wikipedia article, it looks as if a should be -coeffs. Also confusing using the same letters for different things.

    – Tom Hawtin - tackline
    Nov 20 '18 at 22:43













  • @TomHawtin-tackline yes Vector is a homemade class. Position is another vector, but I only want the closest point on the (x,z) plane, so that's why I'm not using the y coordinate. But I don't see how -coeff instead of coeff for a should change anything, a can be negative or positive

    – Vinz243
    Nov 21 '18 at 8:07



















How are you not getting a compile error? afaik Vector has no constructor for three arguments: docs.oracle.com/javase/8/docs/api/java/util/Vector.html

– Jere
Nov 20 '18 at 22:11





How are you not getting a compile error? afaik Vector has no constructor for three arguments: docs.oracle.com/javase/8/docs/api/java/util/Vector.html

– Jere
Nov 20 '18 at 22:11













What is "position" in your code? Is it an instance of a point in 3d? If so, then getZ is probably getting the 3rd dim and not what you'd want here.

– sofend
Nov 20 '18 at 22:24





What is "position" in your code? Is it an instance of a point in 3d? If so, then getZ is probably getting the 3rd dim and not what you'd want here.

– sofend
Nov 20 '18 at 22:24













I think this is a 3D solution. (Top tip: Write the simplest thing first. Then adapt it. Possibly keep the original solution in a separate method and call that.) @Jere Vector doesn't appear to be java.util.Vector but a 3D Vector. @sofend axis appears to refer to one of the X/Y/Z axises. Can't be bothered to look through this in detail given the state of the question, but straight away, if converting to the cited but not quoted Wikipedia article, it looks as if a should be -coeffs. Also confusing using the same letters for different things.

– Tom Hawtin - tackline
Nov 20 '18 at 22:43







I think this is a 3D solution. (Top tip: Write the simplest thing first. Then adapt it. Possibly keep the original solution in a separate method and call that.) @Jere Vector doesn't appear to be java.util.Vector but a 3D Vector. @sofend axis appears to refer to one of the X/Y/Z axises. Can't be bothered to look through this in detail given the state of the question, but straight away, if converting to the cited but not quoted Wikipedia article, it looks as if a should be -coeffs. Also confusing using the same letters for different things.

– Tom Hawtin - tackline
Nov 20 '18 at 22:43















@TomHawtin-tackline yes Vector is a homemade class. Position is another vector, but I only want the closest point on the (x,z) plane, so that's why I'm not using the y coordinate. But I don't see how -coeff instead of coeff for a should change anything, a can be negative or positive

– Vinz243
Nov 21 '18 at 8:07







@TomHawtin-tackline yes Vector is a homemade class. Position is another vector, but I only want the closest point on the (x,z) plane, so that's why I'm not using the y coordinate. But I don't see how -coeff instead of coeff for a should change anything, a can be negative or positive

– Vinz243
Nov 21 '18 at 8:07














1 Answer
1






active

oldest

votes


















0














If you stay in vector representation it may be easier.
I have an example code but only in C++ (and Direct3D):



D3DXVECTOR3 ProjectOnLine (const D3DXVECTOR3 &point,
const D3DXVECTOR3 &linePoint,
const D3DXVECTOR3 &lineUnityDir)
{
float t = D3DXVec3Dot(&(point-linePoint), &lineUnityDir);
return linePoint + lineUnityDir*t;
}


If I understand you then your parameters can be used like this:



D3DXVECTOR3 point = position;
D3DXVECTOR3 linePoint = axis;
D3DXVECTOR3 lineUnityDir = D3DXVECTOR3(1, a, 0)/sqrt(1+a*a);





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402174%2ffinding-the-closest-point-on-a-line-to-another-point%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If you stay in vector representation it may be easier.
    I have an example code but only in C++ (and Direct3D):



    D3DXVECTOR3 ProjectOnLine (const D3DXVECTOR3 &point,
    const D3DXVECTOR3 &linePoint,
    const D3DXVECTOR3 &lineUnityDir)
    {
    float t = D3DXVec3Dot(&(point-linePoint), &lineUnityDir);
    return linePoint + lineUnityDir*t;
    }


    If I understand you then your parameters can be used like this:



    D3DXVECTOR3 point = position;
    D3DXVECTOR3 linePoint = axis;
    D3DXVECTOR3 lineUnityDir = D3DXVECTOR3(1, a, 0)/sqrt(1+a*a);





    share|improve this answer






























      0














      If you stay in vector representation it may be easier.
      I have an example code but only in C++ (and Direct3D):



      D3DXVECTOR3 ProjectOnLine (const D3DXVECTOR3 &point,
      const D3DXVECTOR3 &linePoint,
      const D3DXVECTOR3 &lineUnityDir)
      {
      float t = D3DXVec3Dot(&(point-linePoint), &lineUnityDir);
      return linePoint + lineUnityDir*t;
      }


      If I understand you then your parameters can be used like this:



      D3DXVECTOR3 point = position;
      D3DXVECTOR3 linePoint = axis;
      D3DXVECTOR3 lineUnityDir = D3DXVECTOR3(1, a, 0)/sqrt(1+a*a);





      share|improve this answer




























        0












        0








        0







        If you stay in vector representation it may be easier.
        I have an example code but only in C++ (and Direct3D):



        D3DXVECTOR3 ProjectOnLine (const D3DXVECTOR3 &point,
        const D3DXVECTOR3 &linePoint,
        const D3DXVECTOR3 &lineUnityDir)
        {
        float t = D3DXVec3Dot(&(point-linePoint), &lineUnityDir);
        return linePoint + lineUnityDir*t;
        }


        If I understand you then your parameters can be used like this:



        D3DXVECTOR3 point = position;
        D3DXVECTOR3 linePoint = axis;
        D3DXVECTOR3 lineUnityDir = D3DXVECTOR3(1, a, 0)/sqrt(1+a*a);





        share|improve this answer















        If you stay in vector representation it may be easier.
        I have an example code but only in C++ (and Direct3D):



        D3DXVECTOR3 ProjectOnLine (const D3DXVECTOR3 &point,
        const D3DXVECTOR3 &linePoint,
        const D3DXVECTOR3 &lineUnityDir)
        {
        float t = D3DXVec3Dot(&(point-linePoint), &lineUnityDir);
        return linePoint + lineUnityDir*t;
        }


        If I understand you then your parameters can be used like this:



        D3DXVECTOR3 point = position;
        D3DXVECTOR3 linePoint = axis;
        D3DXVECTOR3 lineUnityDir = D3DXVECTOR3(1, a, 0)/sqrt(1+a*a);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 15:55

























        answered Nov 21 '18 at 8:12









        Yigal EilamYigal Eilam

        193




        193
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402174%2ffinding-the-closest-point-on-a-line-to-another-point%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings