rotating an ellipse about an axis - java

if i make an ellipse bounded by a rectangle. how can i rotate it ? as in if i have rx,ry,cx,cy,topleft x,toplefy, bottomrightx,bottomrighty
iused the formula
angle=taninverse(ry/rx)
and i keep adding the angle that is angle=angle + taninverse(ry/rx)
the angle is too small to see the difference.
please provide another formula to calctulate teh angle with the given parmeteres
to have a look at the ellipse with the bounded rectangle frame
http://svg-edit.googlecode.com/svn/branches/2.5.1/editor/svg-editor.html

Have you tried rotating by a larger angle to see what happens? Do the values of rx and ry change?
If not, you probably should compute 'atan(ry/rx)' and store the result in a variable like so:
double delta = Math.atan(ry / rx);
Then when you rotate
angle += delta; // or angle = angle + delta --- these are equivalent
Alternatively, rather than calculating the angle from ry and rx, you may wish to simply keep a value such as
double delta = Math.PI / 6;
This simply gives the angle to rotate through without a calculation. The advantage is that you can simply set this delta variable to any value you wish, large or small. As above, you can increment angle by this delta value.

There are some animation classes in Android. The package android.animation is available since API 11 and provides the ability animate object properties. android.view.animation is available from API 1 and provides animations for resizing, moving, and rotating. Both also offer XML attributes so you can also set you animation in XML. The main classes to check out are android.view.animation.Animation and android.animation.Animator.

Related

Get radius from a circle to screen corners

I followed an online tutorial, I did not understand how the professor managed to get the distance of a circle from the top left corner of the screen:
//calculate the distance from epicenter (of a circle) to the top left corner of the screen
double theta = atan(epicenter.dy/epicenter.dx);
double distanceToCorner = epicenter.dy / sin(theta);
I would like to know how to get the distance from all the other screen corners (and possibly have an explanation of what has been done).
Thank you
Assuming, that in android you can get screen width and height, you can simply count the distance at horizontal and vertical axis separately.
Getting the distances at those axes, you can use Pythagoras equation, like
dist = sqrt( dx^2 * dy^2 )
To make it more felxible, just make a function, that takes corner position as a parameter and make the dx and dy as a absolute difference of corner and epicenter location.
Going back to your question and atan(...), I don't quite understand the need of using this here, except if that's a project for the math class :)
I know this is not the answer but if i understand what you mean, then this image might be helpful.

Random angles of a bouncing ball

I'm trying to make a ball bounce around a window. Depending on how far away the ball hits the wall and at what angle will determine its reflection. You can see in the pic that the black trajectory hits the opposite wall on the inner half... and the gray trajectory represents if it were to reflect and hit the other half... which would decrease the angle of reflection.
I'm not sure if I'm thinking about it correctly... I'm trying to put the coordinates in terms of degrees.
So given the pic... You would take those deltas, then get degrees...
degree = Math.atan2(opposite/adjacent) = (-4/-2)
My code
public class Calculate {
public Calculate() {
System.out.println(getCalc(7,5,4,0));
}
public double getCalc(int x1, int x2, int y1, int y2) {
double deltaX = Math.abs(x2-x1);
double deltaY = Math.abs(y2-y1);
double degrees = Math.toDegrees((java.lang.Math.atan2(deltaX, deltaY)));
return degrees;
}
}
Gives the output: 26.56505117707799
So now I know the ball would reflect off the wall at 26 degrees (since that's the angle of incidence). But I don't want the ball to necessarily reflect uniformly off each wall so it adds variability.
My questions:
Am I calculating the angle of the ball correctly?
How can I add variability to the bounce based on where it hits on the wall?
Once I have the angle in degrees, how can I translate that back to coordinates?
Thank you!
Am I calculating the angle of the ball correctly?
Your drawing is not to scale. The 26 degrees is measured from a line perpendicular to the wall.
How can I add variability to the bounce based on where it hits on the wall?
You already suggested a random angle. You can also adjust the angle of reflection based on the distance from the center of the wall.
Put your angle of reflection calculation into its own method. You can adjust the calculation until your calculations give you the "randomness" you're looking for.
Once I have the angle in degrees, how can I translate that back to coordinates?
Convert the degrees to radians, then calculate the SAS of the triangle. Just leave your angles in radians in the model, and convert to degrees in your display / diagnostic methods.
I think that the distance of the ball from the surface doesn't really have an effect on the angle. The angle of the ball before hitting the surface should be the same (mirror reflected) when it leaves the surface for it to be natural.
You can add some variability by thinking what happens to a rubber ball, since it changes a little on impact depending on the force etc., the reflection is not exactly the same every time. You could simply add or remove a degree or two randomly and see how it goes.
Once you have an angle, its once again down to trigonometry. You have an angle, and you know the hypotenuse (I presume depending on your frame-rate and ball speed the ball would have travelled a certain amount from the surface). So from that you need to get the adjacent and opposite lines of the triangle.
For example:
sin(angle) * hypothenuse = opposite (so Y offset from the surface).
cos(angle) * hypothenuse = adjacent (so X offset from the point of contact).
Just add or remove (depending on the direction) the adjacent and opposite values from the coordinates of the contact point.
If you want to make it seem a little random, you could model spin somewhat--it doesn't have to be too complicated.
If it was not spinning and hit a wall at a 45 degree angle, it would impart a spin to the ball. When it hit the next wall, the spin would added to the angle and the spin would be increased (or decreased) by the angle. I think the spin/angle combination would also effect the speed at which it came off the wall (Just visualizing real-life situations)
That would make it vary without it being truly random--but I don't know how it would actually look--you may have to apply other restrictions (I think that there must be a way to limit the max spin).
I bet there is a simple physics book around that could give enough to model this without going too deep into the math if you didn't just want to make up rules.

Finding angle from center point on circle [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Find angle of a point from center of circle
Imagine a circle, the center point is fixed, as is the point at the top of the circle. What i want is to give a third point anywhere around the circle (or outside the circle) and get the degrees from the center to top point line from 0-359. (I actually graphed out a nice picture illustrating but im new and cant post it)
To give some context, the circle is a little planet and I want do place a little building on the surface with the base of the building at a tangent. I need the rotation angle for my building bitmap.
edit: Thanks for the help, i'm still struggling with this one though. I wonder could it be relevant that I'm using android and the y0 coordinate is at the top? Is it the other way around on other platforms? would that affect the calculation?
Solution: Because I am in android and the y coords are counted from top to bottom I had to change a - witha +
degrees = Math.atan2(x - centerX, -y + centerY);
// and to make it count 0-360
if (degrees < 0) {degrees += 2 * Math.PI;}
Use Math.atan2() to get the angle in radians from east, and then rotate and convert as appropriate.
Note that atan2 is defined as atan2(y, x) NOT atan2(x, y), as you might expect.
Get the horizontal distance and the vertical difference between the center and the point, divide one by the other, and pass the result to the method Math.asin(double).
The result will be the angle in radians. When you need it in degree, you can use the method Math.toDegrees(double). Most APIs I know prefer radians, though.

3D to 2D projection

I'm trying to write a game in Java (Processing, actually) with some cool 3D effects.
I have a choice of two 3D renderers, but neither have the quality or flexibility of the default renderer. I was thinking that if I could get a function to proje
So say I have a set of coordinates (x, y, z) floating in 3D space. How would I get where on the 2D screen that point should be drawn (perspective)?
Just to clarify, I need only the bare minimum (not taking the position of the camera into account, I can get that effect just by offsetting the points) - I'm not re-writing OpenGL.
And yes, I see that there are many other questions on this - but none of them seem to really have a definitive answer.
Here is your bare minimum.
column = X*focal/Z + width/2
row = -Y*focal/Z + height/2
The notation is:
X, Y, Z are 3D coordinates in distance units such as mm or meters;
focal is a focal length of the camera in pixels (focal= 500 for VGA resolution is a reasonable choice since it will generate a field of view about 60 deg; if you have a larger image size scale your focal length proportionally); note that physically focal~1cm << Z, which simplifies formulas presented in the previous answer.
height and width are the dimensions of the image or sensor in pixels;
row, column - are image pixels coordinates (note: row starts on top and goes down, while Y goes up). This is a standard set of coordinate systems centered on camera center (for X, Y, Z) and on the upper-left image corner (for row, column, see green lines).
You don't need to use OpenGL indeed since these formulas are easy to implement. But there will be some side-effects such as whenever your object has several surfaces they won't display correctly since you have no way to simulate occlusions. Thus you can add a depth buffer which is a simple 2D array with float Z values that keeps track which pixels are closer and which are further; if there is an attempt to write more than once at the same projected location, the closer pixel always wins. Good luck.
Look into Pin hole camera model
http://en.wikipedia.org/wiki/Pinhole_camera_model
ProjectedX = WorldX * D / ( D + worldZ )
ProjectedY = WorldY * D / ( D + worldZ )
where D is the distance between the projection plane and eye

How do you calculate diagonal velocity?

I'm writing a game of sorts to practice some programming, and i've come across this problem. In my game, there are circles on the screen. And when the user clicks on the screen, the circles should move away from the click. I've got the x and y position of the point where the mouse button was pressed, and i have the x and y position of each cicle object.
I found the center of the circles with the following code
float cx = circle.getX()+circle.getRadius();
float cy = circle.getY()+circle.getRadius();
And to find the distance from the edge of the circle to the mouse click I did this
float distance = (float) Math.sqrt( ((cx-x)*(cx-x)) + ((cy-y)*(cy-y)) ) - circle.getRadius();
Now after I check if the circle is close enough to the click, how can I split a velocity of 1f, for example, to the circle's variables vx and vy?
EDIT: Well actually I wanted acceleration, but I guess it's all the same.
This sounds like a job for sin and cos in java.lang.Math : http://download.oracle.com/javase/6/docs/api/java/lang/Math.html .
Once you know the total velocity (1f in your example above) and the angle (in radians), the horizontal component of the velocity is v * cos(angle), and the vertical component is v * sin(angle).
You probably need to negate the angle if you want to move it away.
To calculate an angle from horizontal and vertical distances, use atan2.
Btw, if you don't want to take unnecessary square roots, and want to avoid the cost of computing series the way trigenometric functions do, take a look at http://www.youtube.com/user/njwildberger#p/u/368/9wd0i44vK04 .
Find the line from the mouse to the center of the circle and that should be the "force" vector. This vector will give you the direction, now you just need to figure out how distance affects the magnitude.
You could first find angle as Mike suggested and use cos and sin functions.
Or use:
velHoriz = velocity * (cx-x) / distance
velVert = velocity * (cy-y) / distance

Categories