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.
Related
I'd like to calculate two points that align with the real horizon (presuming the user is holding the device virtually). I'd like the line connecting those 2 points to pass through a center point of my choosing.
I'm collecting Sensor.TYPE_ACCELEROMETER and Sensor.TYPE_MAGNETIC_FIELD vectors and have found sample code to rotate bitmaps with rotationmatrix but can't get my mind around calculating just the 2 points I need in terms of the screen's X and Y.
Any hints greatly appreciated!
If I understood everything right, that should be an easy trigonometric question.
You should find the angle to horizon and then It will look like this
X1 = 0
Y1 = Yc + Xc * tan(a)
X2 = screenWidth
Y2 = Yc - (screenWidth - Xc) * tan(a)
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
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.
Turns out I am not explaining what I want to do very well so I am going to re-write the whole question again and add graphics to assist in the explanation.
I am designing an app for android/iPhone. I have designed one algorithm for it already, but am stuck on the next one. What I am aiming to do is place two horizontal lines on an image (any image, just a picture taken by the iPhone/android) and then calculate what pixel the lines lye on, to then calculate the number of pixels between them.
I.e.
Take this image:
http://i.stack.imgur.com/41vS1.png
Then place two horizontal lines anywhere on the image, like so:
http://i.stack.imgur.com/ne4tV.png
What I want to calculate is the value of y, or how many vertical pixels are between the two lines. To do this I must know what pixel the two lines lye on.
Assuming that the horizontal lines are both only 1 pixel in height what would I use to work out what pixel in the image the line lies on. I.e. what is the value of the y-intercept(y=mx+c), or c, on each of the horizontal lines.
To explain what I mean further, lets assume that an image is a graph. Each pixel equals a value of 1, so for an image with a resolution of 1920x2560 the maximum value of the y-axis would be 1920 and the maximum of the x-axis would be 2560. How would I design an algorithm to calculate what the y-intercept of both lines are?
Distance between two points (Pythagora):
dx = x1 - x2;
dy = y1 - y2;
dist = sqrt (dx*dx + dy*dy);
Distance between two horizontal lines:
d = y1 - y2;
If your lines are defined as y1 = k1x + n1 and y2 = k2x + n2, then (they're horizontal, k1 and k2 are 0) the distance between them is n2 - n1.
EDIT: ok, after you edited your question it makes a bit more sense now. But still: since you (or user) is adding the lines, your code always knows where they lie. Their end coordinates would be:
line1: {(0,y1):(picture.width,y1)}
line2: {(0,y2):(picture.width,y2)}
distance: |y2-y1|
Since they're both horizontal they ofcourse never cross.
You should just keep a reference to y1 and y2 (from the line-placing code) in an appropriate space. Since your question is for Android and iOS the answer is: in that part of code that would correspond to model in MVC.
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.