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
Related
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.
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.
See image below:
illustration http://img28.imageshack.us/img28/5286/pic1we.png
Assume it's on android device screen. Dot #1 is the current position. I want to get the (x,y) position of Dot #2 following the linear blue line (let's say the range between dot #2 and dot #1 is 50dp). The black box is the source point of the blue line.
What is the term for this? Translation?
Is there anyone know the algorithm?
I developed android application using AndEngine, if you know there is built-in function for that, please let me know. Same too for android openGL ES library.
Note: I'm not looking for a way to move object using MoveModifier.
Thx in advance.
Authman's answer is good, but doesn't include how to get a specific distance away. If you're looking for p2 along the line, IMO it's easier to think about this in vectors. First you find the vector along the blue line, which is similar to the process that Authman describes above. If the black box has position v0 = (x0, y0) and the first dot has position v1 = (x1, y1), then your "direction vector" is going to be v1 - v0 = (x1 - x0, y1 - y0). This is a vector that points along the blue line. Once you have that, you normalize it so that the length is 1, and then multiply it by your desired distance, in your case 50. If you want to do it component-wise, here's some quick pseudocode:
find_v2(x0, y0, x1, y1):
direction_x = x1 - x0
direction_y = y1 - y0
norm = sqrt(direction_x^2 + direction_y^2)
direction_x *= 50 / norm
direction_y *= 50 / norm
x2 = x1 + direction_x
y2 = y1 + direction_y
Hope that helps! I'm not sure how comfortable you are with vector math, but if you're doing graphics stuff, it can be really, really useful for problems like this; I'd recommend checking out some of the Khan Academy Linear Algebra courses.
The term you are looking for is extrapolation.
If the black dot is dot '0', then you first calculate your line equation's slope:
m = ( (d0.y-d1.y) / (d0.x-d1.x) )
Then you can extrapolate along that line, either positively (away from point 0) or negatively (towards it, until you get to it, and then eventually again away from in continuing in the line of motion).
dot2.x = d0.x + m*percentage
dot2.y = d0.y + m*percentage
Where percentage is how close you are to dot 1. Thus 1 (eg 100%) would put you on dot 1 exactly. percentage=0 would put you on dot 0 exactly. >1, would put you closer to dot 2. <1 would move you past dot 0, etc.
--
Slight correction there:
dot2.y = d0.x + m*percentage
dot2.x = (dot2.y - dot0.y) / m
The slope as i set it up is only useful for calculating the y coordinate of dot2. Since you already have the equation of the line, you can solve for dot2's x-coord directly using it.
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
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.