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.
– Sorack
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.
– Sorack
@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.
– José
@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.
– HenriPK
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.
– José
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.
– Pedro Henrique Monforte
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).
– Bacco