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

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

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.

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

Parametric trajectory equation?

I'm creating a very very simple game for fun. Realizing I needed the trajectory of an object given an angle and a velocity, it seemed logical to use this parametric equation:
x = (v*cos(ø))t and y = (v*sin(ø)t - 16t^2
I know that this equation works for a trajectory, but it isn't working with most ø values I use.
Do java angles work any differently from normal angle calculation?
My goal is for the object to start from bottom left of the window and follow an arc that is determined by the speed and angle given. However it tends to go strange directions.
The value of ø should be horizontal at 0 degrees and vertical at 90, and in the equation it refers to the angle at which the arc begins.
This is my first ever question post on this site, so if I'm missing anything in that regard please let me know.
Here is the calculating part of my code
not shown is the void time() that counts for each 5ms
also I should mention that the parX and parY are used to refer to the x and y coordinates in an unrounded form, as graphics coordinates require integer values.
Any help is much appreciated, and thank you in advance!
public void parametric()
{
parX = (float) ((speed*cos(-ø))*time);
gravity = (time*time)*(16);
parY = (float) ((float) ((speed*sin(-ø))*time)+gravity)+500;
xCoord = round(parX);
yCoord = round(parY);
}
Do java angles work any differently from normal angle calculation?
You only need to read the docs
public static double cos(double a)
Parameters:
a - an angle, in radians.
I guess you are using degrees instead of radians?

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);

Generate random lat lon

I need it to stress test some location based web service. The input is 4 pairs of lat/lon defining a bounding rectangle or a set of points defining a polygon.
Are there any libraries/algorithms for generating random point on a map? (Python/java)
In java you can use Math.random()
For example, if you want to generate a random number between 1 and 10:
int randomNumGenerated = (int)(Math.Random()*10) + 1;
You can apply this to the issue you are trying to solve easily.
Take a look at this question, which deals with generating points inside an arbitrary 4-point convex polygon.
Random points inside a 4-sided Polygon
This article, on sphere point picking explains far better than I could why the naive approach of generating 2 random numbers on the interval [0,1) will lead to a poor distribution of points across the surface of the sphere. That may or may not be a concern of OP.
However, it ought to be of concern to OP that randomly generating a set of 4 points on the surface of the Earth might necessitate some tricky programming. Consider the case of the 'polygon' defined by the points (lat/long, all in degrees) (+5,90),(+5,-90),(-5,-90),(-5,90). Does the point (0,0) lie inside this polygon or outside it ? What about the point (0,180) ? It's very easy to generate such ambiguous polygons -- the surface of a sphere is not well modelled by the Euclidean plane.
I'd take a completely different approach -- generate 1 point at random, then generate lat and long offsets. This will give you a quasi-rectangular patch on the surface, and you can tune the generation of the offsets to avoid ambiguous polygons. If you want to generate polygons which are not quasi-rectangular, generate a series of points and angles which, when combined, define a polygon which suits your needs.
Simple: Generate two random numbers, one for latitude and one for longitude, inside the bounding rectangle of the map, for each point.
double longitude = Math.random() * Math.PI * 2;
or use
public static LatLng random(Random r) {
return new LatLng((r.nextDouble() * -180.0) + 90.0,
(r.nextDouble() * -360.0) + 180.0);
}
Why wouldn't you just generate the latitude as a random number between -90 and 90, and the longitude as another random number between -180 and 180?
Then you have a point. Yo can then generate as many points as you need to make a polygon.
You can generate a random number between a and b with something like:
rnum = a + rnd() * (b-a); // where rnd() gives a number from 0 to 1

Categories