3 Points, 1 angle - Java (2D) - java

I have read how to calculate the triangle with 3 points and its angles. The problem is, I have tried different approaches and still get strange results. That can only mean I am doing something simple terribly wrong. But I am too much into my programming to see it; maybe anyone of you can help me?
I have 3 points: u, v and w
u---------------v
w is under u and v. I now want to get the angle in ° at w, which spans the triangle to u and v.
The coordinates are defined by u(ux,uy) v(vx,vy) w(wx,wy)
double dn =
Math.sqrt(Math.pow(ux-wx, 2)+Math.pow(uy-wy, 2))
* Math.sqrt(Math.pow(vx-wx, 2)+Math.pow(vy-wy, 2));
double beta = Math.acos(((ux-wx)*(vx-wx)+(uy-wy)*(vy-wy))/(dn));
beta is always less than 3.5, so I can't really figure out what I did wrong. Or did I understand the formula wrong? All variables are double, so that can be excluded I think.
I used:
(source: matheplanet.com)
.

Related

How to solve equation system by using Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Here is the problem, I have this equation system (as an example) that I need to solve and find x and y values:
(x-0)^2+(y-5)^2=12,25
(x-0)^2+(y-0)^2=2,25
Don't worry about 0'os, this changes according to detected point and this is just an example.
As I understand, I am not able to use Craner's rule here, so I am lost and I have no idea how to program this. It is simple to do it by hand, but how to write algorithm for this?
Any tips?
Edit: here is a picture of how equations look like and how I solve them by hand: http://i.imgur.com/Gm29Cfw.jpg (step by step solution: http://i.imgur.com/ZvraQoZ.jpg)
The process of solving it by hand is pretty simple: I have quadratic equation system. Then, I take the second equation and find what x is equal to in that equation. So, by doing this now I know what x is equal to. After this step I take that x value and put it in the first value. By doing so I make sure that first equation has only one missing variable. I solve it normally and get what y is equal to. Then, I put y value to the x value and I get my answer.
Okay, here is an idea, but I have yet to implement it and check if this actually works.
Mathematically, the two equations describe circles.
Let (a,b) be the center of the first circle and sqrt(r_1) its radius, and let (c,d) be the center of the second circle and sqrt(r_2) its radius. Then, in cartesian coordinates, the points on the circle fullfull respectivelly
(x - a)^2 + (y-b)^2 = r_1
or
(x - c)^2 + (y-d)^2 = r_2
We describe the circle now with two functions: The upper part and the lower part. These are functions involving square roots. So if we have
(x - a)^2 + (y-b)^2 = r_1
Then solving for y gives (via wolfram alpha)
(y-b)^2 = r_1 - (x-a)²
y = b + sqrt(-a²+2ax+r_1-x²)
or
y = b - sqrt(-a²+2ax+r_1-x²)
We can also express the lower and upper part of the other circle with these two equations by exchanging (a,b) with (c,d) and r_1 with r_2.
The point is, once we have two graphs with y_1 = f(x) and y_2 = g(x), then we can find their intersection with f(x) = g(x) or equivalently f(x) - g(x) = 0. For this, we can use approximate solutions found by the Newton's iteration method! We can also compute the needed derivatives:
So, the whole idea is that we split each circle in two functions: Upper andl lower part. Then, we check if the upper part of the first circle intersects the function describing the upper part of the second circle or the lower part of the second circle. Same with the lower part, we check it agains the upper and lower part of the other function. And for finding the intersection, we can use the approximate Newton method.
So, for the above example:
(x-0)^2+(y-5)^2=12,25
We get the upper and lower functions as
y = 5 + sqrt(12.25-x^2)
y = 5 - sqrt(12.25 -x^2)
And we can plot them nicely
Conversely, the second circle ((x-0)^2+(y-0)^2=2,25) is described by the equations
y = 0 + sqrt(2.25-x^2)
y = 0 - sqrt(2.25-x^2)
Now, if we look at all these graphs at once:
We find that there is an intersection! :). Between the lower part of the first circle and the upper part of the second circle. If we now form the difference between these two functions, we get the following graph for the functions:
f(x) = 5 - sqrt(12.25 -x^2)
g(x) = 0 + sqrt(2.25-x^2)
f(x)-g(x) = 5 - sqrt(12.25 -x^2) - sqrt(2.25-x^2)
And if we plot that
We can see that if we find the zeroes of that graph, we will get the correct solution x = 0! :)
Once we have that solution, we can eliminate one variable in either of the equations
(x-a)^2 + (y-b)^2 = r_1
And we will receive an equation ONLY quadratic in y, which can be solved by the general solution formula (pq-formula or abc-formula).
Hope this gives you some ideas.

Collision between a line and a face

Ok, so on the internet, I have seen equations for solving this, but they require the normal of the plane, and are a lot higher math than I know.
Basically, if I have an x,y,z position (as well as x,y,z rotations) for my ray, and x,y,z for three points that represent my plane, How would I solve for the point of collision?
I have done 2D collisions before, but I am clueless on how this would work in 3D. Also, I work in java, though I understand C# well enough.
Thanks to the answer below, I was able to find the normal of my face. This then allowed me to, through trial and error and http://geomalgorithms.com/a05-_intersect-1.html, come up with the following code (hand made vector math excluded):
Vertice Vertice1 = faces.get(f).getV1();
Vertice Vertice2 = faces.get(f).getV2();
Vertice Vertice3 = faces.get(f).getV3();
Vector v1 = vt.subtractVertices(Vertice2, Vertice1);
Vector v2 = vt.subtractVertices(Vertice3, Vertice1);
Vector normal = vt.dotProduct(v1, v2);
//formula = -(ax + by + cz + d)/n * u where a,b,c = normal(x,y,z) and where u = the vector of the ray from camX,camY,camZ,
// with a rotation of localRotX,localRotY,localRotZ
double Collision =
-(normal.x*camX + normal.y*camY + normal.z*camZ) / vt.dotProduct(normal, vt.subtractVertices(camX,camY,camZ,
camX + Math.sin(localRotY)*Math.cos(localRotX),camY + Math.cos(localRotY)*Math.cos(localRotX),camZ + Math.sin(localRotX)));
This code, mathimatically should work, but I have yet to properly test the code. Tough I will continue working on this, I consider this topic finished. Thank you.
It would be very helpful to post one of the equations that you think would work for your situation. Without more information, I can only suggest using basic linear algebra to get the normal vector for the plane from the data you have.
In R3 (a.k.a. 3d math), the cross product of two vectors will yield a vector that is perpendicular to the two vectors. A plane normal vector is a vector that is perpendicular to the plane.
You can get two vectors that lie in your plane from the three points you mentioned. Let's call them A, B, and C.
v1 = B - A
v2 = C - A
normal = v1 x v2
Stackoverflow doesn't have Mathjax formatting so that's a little ugly, but you should get the idea: construct two vectors from your three points in the plane, take the cross product of your two vectors, and then you have a normal vector. You should then be closer to adapting the equation to your needs.

Getting The current Rotation of a Matrix - using a library?

Firstly lets say I have forgotten most of my maths long ago. I do not need to understand it in depth. The question was asked and answered (see links) but I do not want to derive my own function, is there an existing one ? The image below show a Matrix which I have rotated 45 degrees. Is there a way I could plug in the 0.707 numbers and get 45 back ? At the moment I am keeping track of the rotation on my own (simple solution), but I would prefer a function to derive back the 45 degrees
question 7291053
Matrix Rotation
The function is called "arcus sinus" or arcsin(x). For arcsin(0.707107) = 45 with a bit of rounding error.
In Java Math library you must additionally translate the result from RAD to DEG like this:
Math.asin(0.707) * 180d / Math.PI
Note that you get back something between -90° and +90° as described here.
If you want to know which axis you actually rotate and on which part (lower, upper) of the circle you are, then you must take a look at all 9 values. See here how the matrices look like for each axis.
For the angles of each axis x,y,and z
[0][1][2]
[3][4][5]
[6][7][8]
double x = Math.atan2([7], [8]);
double y = Math.atan2(-[6],Math.sqrt((Math.pow([7],2)+Math.pow([8],2)));
double z = Math.atan2([0], [3]);
then multiply the one you choose by 180/PI

java right triangle angles

Ok, I am supposed to calculate the angle of a right triangle using Java. The measurements of the sides are a = 3 and b = 4 with the hypotenuse being c = 5. If I use Math.sin, it does not calculate the angle created by a and c. Is there another Math function I am not aware of? I have spent a long time trying to figure this out to no avail. Thanks in advance.
The probably source of the problem is that sin works in radians, rather than degrees.
To find the angle bounded by the side a and the side c, try using Math.toDegrees(Math.acos((double)a/c)). The passed value d to Math.acos(d) will have to be double.'Math.acos(d)' will give the arc cos in radian. Go through Math documentation

how to discover an angle between two objects?

what I want to do is the following: I have an object (blue point) and I want to point it to other object no matter where it is located around it (green point). So I need to know the angle between these two objects to do what I want right?
http://s13.postimage.org/6jeuphcdj/android_angle.jpg
The problem is, I don't know what to do to achieve this. I've already used atan, math.tan and so many other functions but without any good results.
Could you help me? Thanks in advance.
Calculate a dot product of object vectors. Use Math.acos on the value you get. That will give you an angle in radians.
So, say your blue dot is at vec1 = (50, 100) and green one at vec2 = (100, 400).
A tuple (x, y) as a two dimensional vector describes object's position and distance from (0, 0) on your screen. To find the angle between these two vectors, you do a standard, binary dot product operation on them. This will get you a scalar (a value, cos(Theta)), but you want the inverse of it (acos) which is the angle you're looking for.
You can get a better understanding on the matter here
Suppose the coordinates of the blue and green points are (xblue, yblue) and (xgreen, ygreen) respectively.
The angle at which the blue point sees the green point is:
double angleRadians = Math.atan2(ygreen-yblue, xgreen-xblue);
If you want the angle in degrees:
double angleDegrees = Math.toDegrees(angleRadians);

Categories