Distance between a line and a point in three-dimensional space

Asked

Viewed 181 times

0

Let’s say there are three points in a 3D space (dots A, B and P). Let’s also say that there is a straight line through A and B.

What I’m trying to do, actually, is find out if a reta AB intercept one esfera de raio r that has origin in P. For this I just need to check if the distance of the line AB and the point P is less than the radius of the sphere.

Vector A, B, P; //A minha classe Vector, guarda três valores (x, y e z)

if(distance(A, B, P) < r){
   return sphere.color;
}

My problem is this method distance(Vector A, Vector B, Vector C)... How can I calculate the distance from a point to a line (given two points of the same), and I have to write this in JAVA?

The distance between a point and a line is the length of a semi-straight starting at P, and ending at line AB, forming an angle of 90 degrees with the same.

Discover the distance between a point and a straight line, drawing for example, it is relatively easy, just draw a straight line from the point to the other line and measure the size.

In 2D, this would be easy, since the reta D (the distance) forms a rectangular triangle, the hypotenuse being the vetor V representing the position of ponto P, and the adjacent cathode is this same vector designed (which I don’t know the length) on reta AB.

This way I could calculate the reta D, since I have the means to discover all three angles of the triangle (one is a right angle, and the other is the difference of the angles of the reta AB and the vetor V, and the third complete 180 degrees with the others), I can use the only side I have the value (the hypotenuse) and calculate the opposite cathode.

It turns out that I’m working in an environment three-dimensional, where it is not so simple to find an angle between the reta AB and the vetor V to complete the triangle. Also, I’m not a mathematician, but a programmer, So I wanted a method of figuring this out using algebraic expressions (and methods in Java), rather than geometric techniques, so that it worked for any line and point. After all math writing is not easy to convert into actual code.

Finally, I made this post here, because I may be trying to find the distance in an unnecessarily complicated way, or simply wrong.

From now on, thank you.

  • It seems to me that you didn’t even try to create a solution, didn’t look for what the algebraic expression would be and didn’t try to implement any of the problem. One tip I give you is: break the I problem in part according to the theory involved and bring only the parts that you could not solve after trying hard. Another tip is to read the material: Manual on how NOT to ask questions - Thinking we’ll do all your work for free.

  • 1

    In the case of the expression, it does not seem to me related to programming, you can ask in this other site of the Stack Exchange: Mathematics.

  • 1

    @Sorack It is related to programming yes: apparently he wants to use the function to solve the problem but does not know how. He doesn’t seem to know how to use mathematics and the Java function together. I’m wondering if he’s having trouble identifying the problem. Maybe it fits more in one XY problem than the one suggested by you. He must have trapped himself in the programming and thought he should ask how to create an algebraic expression in Java. He asked X (how to do it) but said that the problem is Y (the function). I see this a lot when teaching.

  • 1

    @Sorack Forgive me if it seemed so, but I have no intention of that do all my work. I will edit the question, to include more details of my reflections on the subject.

  • After seeing the accepted answer, I believe @Sorack is correct. The text implied otherwise but the answer accepts that the question should be moved to the Mathematics, in fact.

  • Are questions in Portuguese accepted in Mathematics? If so, the question should be moved. If not, I don’t see the problem that a programmer’s question originates from a mathematical problem. As for the answer, someone should edit it using Java (My Java is so rusty that I doubt it can produce correct code). I’ve already received answers in Matlab for Python questions. As for Edit in the original answer: mathematics is essential and perhaps accelerate your development time give a studied in the basics, mainly linear Algebra.

  • If the problem was in the implementation in Java would be in site scope, which is clearly not the case given the answer accepted. The fact of not having a specific site for mathematics in Portuguese by itself does not make the subject to be accepted here (as well as football, cooking and others).

Show 2 more comments

1 answer

-1


"Let’s say there are three points in a 3D space (points A, B and P). Let’s also say that there is a line that goes through A and B."

What I’m trying to do, in fact, is to find out if an AB line intersects a sphere of radius r that originates in P. For this I just need to check if the distance from the AB line and the P point is less than the radius of the sphere.

So what you want to find out is, given a straight R = [x,y,z]t + [a,b,c] (take a look here to know how to write the equation. You want to know if ||R - P|| <= r

And answer of this inequality of the "t" where the line and sphere intersect, is of second order. If there are real solutions the intersection exists, if not, there is no.

  • And where Java comes in?

  • His problem is mathematical, not Java.

  • 1

    and here is a place for nonmathematical programming problems

  • Programming is mathematics

  • 2

    I disagree. In some points programming and mathematics are related, but one is definitely not the other.

  • Truth, relating to equality is incorrect. The correct relational would be contained

  • But one of the tags of the question is mathematics, giving a mathematical solution is valid for the question. But the ideal place to ask would be Mathematics, but this is the one forum in English and I thought it would be more empathic of me to give the solution right here.

  • Opinion as a user and not as a moderator: I see no problem in helping the colleague (I often do this, but usually put as a community wiki or on a site the Pastebin-like part, when it is outside the scope). But it is important to understand the risk of taking a negative (which happens not to be mine). Considering that he takes very little of the score, I think if the intention is to help, it’s a valid price to pay for the scope problem. The most important thing is the closing of the question, so the site does not get messy, but the author does not leave without solution.

  • About the fact of having the mathematical tag, follows the wiki’s own tag: Any math questions on this site should be related to programming

  • The question is related to programming. Problems with programming can arise both from syntax, conventions and implementation and from logic and mathematics. Still I found the question valid. And the price is right as long as the fellow can solve his problem.

  • the problem I had, which has already been solved, was that although I understood the subject I was dealing with, in geometry, I could not imagine it within a programming context -- in the case in Java. type, I just had to draw a line that would run on the other line perpendicular. But it doesn’t mean anything to me being a programmer, and that’s what I asked for help with. this answer in question, nor was it so definitive (after all he answered me with a mathematical check ||R - P|| <= r but from that I managed to build my method.

  • In addition, I asked a similar question in a mathematics forum, about spherical coordinates, which in the end related to programming, and there I received a lot of criticism about exactly this. That’s why I came to ask this question here. The reason why I wanted a numerical way to represent the scalar product I ended up using is because I had to use it in Java, with programming. There is no way for me to simply write vetor1.projetar(vetor2), I had to convert the mathematical explanation of the subject and convert it into Java code -- which was precisely my problem.

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.