Calculating points of a circle dynamically in an Android game - android

If my X co-ordinates increment every time a frame is drawn, what formula can I employ from the math library in order to have the Y co-ordinate moving around the circumference of a circle frame by frame, creating the illusion of an orbiting object around a continuously moving central point?
I have seen it may involve sin or cos but am not sure how to adjust the variables accordingly per frame.
Many thanks for your help

You can't make a complete circle if your X coordinate increments every time, because half the time your X coordinate has to be decrementing.
What you want is polar coordinates: theta for angle and r for radius. Your r will remain constant, and your theta will increment continuously. Then your x and y are:
x = r * cos(theta)
y = r * sin(theta)

let ox,oy be the origin of your circle, and px,py be a point on the edge of the circle, with a radius of r
given: (px-ox)^2 + (py-oy)^2 = r^2 definition of circle
solve for py:
(py-oy)^2 = r^2 - (px-ox)^2
(py-oy) = sqrt(r^2 - (px-ox)^2)
py = sqrt(r^2 - (px-ox)^2) + oy <---
So as you increment px with your frames, you can find the appropriate py by recalculating the above formula.

Related

How do I Calculate the coords of a Point on a non-circular arc

I'm trying to draw what looks like an old Analog meter. So I have an Arc (that may NOT be circular) drawn using the canvas.drawArc() method.
I need to be able to add "tick" marks (perpendicular to the arc) on the arc every X degrees.
So I want to find the coordinates of the end points of the tick mark line.
Anyone know the math needed for that?
use parametric ellipse equations:
x = x0 + rx * cos(a)
y = y0 + ry * sin(a)
x0,y0 is ellipse center
rx,ry are half axises
a is angle

How to draw a diameter of a circle in Android

I referred the question Draw a circle within circle at a distance of 10
which comes close to my requirement. I just needed two concentric circles one inside the other inner one with radius scaled to 300 mts and outer one with radius scaled to 500 mts.
I have been able to draw the two circles scaled in pixels on my screen using a transformation method that transformed distance in metres to corresponding pixel distances.
The next step is to draw a plus inside the circle; the lines being the diameters. Hence they will pass through the center and contain two points on the circle.
1)I have the lat long and pixel details of the center of the circle.
2)I know that the angle between the center and the either points on the circle need to be 90 degrees.
3)I must use canvas.drawLine().
But what would be the best way to get these points on the circle so that a line can be drawn through these three points.
(Point on the circle at the top, center of the circle,Point on the circle at the bottom).
Greatly appreciate your help.
EDIT:
I tried the following code after some searching
//double degrees = 90.0;
//double radians = Math.toRadians(degrees);
//int x1 = (int) (500 * Math.cos(radians) + x);
//int y1 = (int) (500 * Math.sin(radians) + y);
//canvas.drawLine(x, y, x1, y1, mSelectionBrush);
canvas.drawLine(x, y-500, x, y+500, mSelectionBrush);
canvas.drawLine(x-500, y, x+500, y, mSelectionBrush);
x,y are the coordinates of the center. 500 is the radius of the outer circle.
The output I see is this. The line extends below. Am I going the right way?
I think you're on the right track. Notice that only Y changes for the vertical line.
Assuming center is given by C(a,b)
So the end-points would be
(a,b-r) and (a,b+r)
For the horizontal line only x changes:
(a-r,b) and (a+r,b) would be the end-points.
r is the radius.

android how many pixels between 2 points

i want to calculate how many pixels there are between 2 points on my screen.
I've seen that i can draw a straight line between 2 points using Path class, but i don't really want to draw that line, i only want to know how long is it in pixels..
i really need it for my MapView clusters implementation..
i can get each marker position on screen with no problem, but don't know how to calculate pixel's "distance" between them ...
i know that there are cluster's implementations available but i want to try and create one of my own
help will be appreciated :)
This is very straightforward using a bit of algebra :)
Take the co-ordinates of both points and calculate the difference between their x and y values e.g.
dx = p1.x - p2.x;
dy = p1.y - p2.y;
distance = Math.sqrt( (dx * dx) + (dy * dy) );
Where p1 and p2 are the points you want get find the distance between, and distance is the result. It will be a double but you can round it to the nearest int if you wish

Android - Parabolic motion

I have a moving object in surface view it stat moving from fixed location.
I need set to it to be move like parabolic way ..my object drawn like using x and y so I do
not know using that thread how to calculate the X and y plz help?
The basic formula is:
y=x^2 + m
(m being the x offset)
(the 2 can be an even number higher that 2, but use 2 for now)
Your starting location has the coordinates y0, x0
So you first location is:
y = x0^2 + y0
For the next step calculate your x value like this
x = x0+(n*stepsize)
x0 is you initial x value (see above).
stepsize is the could of pixels offset to each step (simply use 1 for now)
n is the current step inside your drawing loop, like this: for(int n=0;n<100;n++)
then calculate your y value with this x value
y = x^2 + y0
Major Edit:
So i thought of another way to do what you're trying to do which is much simpler:
First you need to define your start (x0|y0) and end (x1|y1) coordinates.
Then use separate formulas to calculate the new position during the animation.
Calculate the distances
x0x1 = x1 - x0
y0y1 = y1 - y0
Define how many animation steps you want (let's say 20)
Devide the distances by this value and in each animation step add that step distance to the last coordinate.
Now to make the whole thing a parabola you'll have to split the distances not evently but logarithmical, at least at the start of the motion. But please try the former before attempting this.
This is a more numerical approach - i hope it helps.

Find point on Circle on Android

Everything seemed so plain and simple until I had to actually program it.
What I've got
I uploaded an image to explain it better.
I have a circle and I know
it's radius
center point coordinates
each button's initial coordinates (the red circles).
I want to be able, when I rotate the gray circle image, with 10 degrees, to calculate red buttons new coordinates (x1y1, x2y2).
This shouldn't be hard to achieve for someone who knows math, but I didn't manage to find a suitable solution. I've also searched around here and couldn't find a working solution. Any help is greatly appreciated.
Thank you
The working solution, as Felice stated below is:
-first take care of rotation angle, on each redraw simply increment it
angle = angle+mainRotationAngle;
float x = (float) (center.X + Math.cos(angle*Math.PI / 180F) * radius
float y = (float) (center.Y + Math.sin(angle*Math.PI / 180F) * radius
button.setX(x);
button.setY(y);
It is easyer if you keep with you the button initial angles, then modify the angle to produce the rotation. so in pseudocode:
newAngle = Angle+rot;
xbutton = center.x+cos(newAngle)*radius;
ybutton = center.y+sin(newAngle)*radius;
If you really just have the coordinates of the buttons, you can convert them to the angle by using the function atan2, in pseudocode:
buttonAngle = atan2(button.y-center.y,button.x-center.x);
x1 = x + r sin 10
y1 = y + r cos 10
x2 = x - r sin 10
y2 = y - r cos 10

Categories

Resources