Hi,
I'm trying to animate a box moving from point A to point B. I'm using a .Net timer to
animate but I'm not sure on the math to get the box moving in a straight line. So far
the box finishes moving down before it finishes moving to the right.
Point A
x=9,y=6
Point B
x=291,y=178
I'm not very good at math but I'm thinking the equation should be straight foreward?
Thanks in advance.
19 Replies - 12909 Views - Last Post: 15 June 2011 - 03:49 PM
Replies To: Equation for a straight line
#2
Re: Equation for a straight line
Posted 14 June 2011 - 04:55 PM
What did you Google for, when trying to answer your own question? How did the results not help, and what can we do that is more helpful than Google?
#3
Re: Equation for a straight line
Posted 14 June 2011 - 04:59 PM
I'm not sure if this will help but here is some info on your line from WolframAlpha
#4
Re: Equation for a straight line
Posted 14 June 2011 - 05:04 PM
I used google and found equations but none helped me with animating from point A to point B.
#5
Re: Equation for a straight line
Posted 14 June 2011 - 05:08 PM
> I'm using a .Net timer to
animate but I'm not sure on the math to get the box moving in a straight line. So far
the box finishes moving down before it finishes moving to the right.
> but none helped me with animating from point A to point B.
You can already animate, can you not? The only issue is deciding the position of the box. Doesn't the equation of a line give you the position of the box?
animate but I'm not sure on the math to get the box moving in a straight line. So far
the box finishes moving down before it finishes moving to the right.
> but none helped me with animating from point A to point B.
You can already animate, can you not? The only issue is deciding the position of the box. Doesn't the equation of a line give you the position of the box?
#6
Re: Equation for a straight line
Posted 14 June 2011 - 05:24 PM
I'm just not sure which variables to increment so the box animates.
#7
Re: Equation for a straight line
Posted 14 June 2011 - 05:25 PM
> I'm just not sure which variables to increment so the box animates.
This I do not understand. How are you currently animating the box?
This I do not understand. How are you currently animating the box?
#8
Re: Equation for a straight line
Posted 14 June 2011 - 05:50 PM
I'm animating it using using a timer. Incrementing both box.x and box.y until it reaches point B.
#9
Re: Equation for a straight line
Posted 14 June 2011 - 06:31 PM
> Incrementing both box.x and box.y until it reaches point B.
Yes, precisely. But the problem is that you incremented x and y independently, even though they aren't separate. x and y are related because the box follows a particular line, yes?
So if you are incrementing x, you use the line equation to determine how much to increment y.
Yes, precisely. But the problem is that you incremented x and y independently, even though they aren't separate. x and y are related because the box follows a particular line, yes?
So if you are incrementing x, you use the line equation to determine how much to increment y.
#10
Re: Equation for a straight line
Posted 14 June 2011 - 10:58 PM
Of course, if you base the movement on x or y, the speed will vary with the angle of the line. You'll want to calculate the change in x and y per unit of change along the line.
#12
Re: Equation for a straight line
Posted 15 June 2011 - 04:04 AM
I understand, if you don't have the vocabulary, you can't search. The magic word you're looking for is "slope." More here: http://en.wikipedia.org/wiki/Slope
You can move a point by simply adding the change in x to x, and y to y. You'll then find that you get gaps. Choose one axis, x or y, and move through every single point, modifying the other value based on the slope. The axis you choose will be the longest one.
Of course, x1=x2 and y1=y2 are special cases and should probably be treated as such.
Good luck.
You can move a point by simply adding the change in x to x, and y to y. You'll then find that you get gaps. Choose one axis, x or y, and move through every single point, modifying the other value based on the slope. The axis you choose will be the longest one.
Of course, x1=x2 and y1=y2 are special cases and should probably be treated as such.
Good luck.
#13
Re: Equation for a straight line
Posted 15 June 2011 - 06:33 AM
That fits if you have two fixed points and simple animation. If you want your object to move in a given direction (as in, given in degrees or radians) with a given speed, then you can use something along the lines of:
*) You may wish to prefix - to angle_rad, or not.That would depend on how you have your coordinate system set up, I think. edit: uh, no, it'd depend on how your engine views rotation in radians - i.e. if radians going positive are translated into rotation counter- or clock-wise.
Notice, that I didn't test it, but it shouldn't be too far away from usable snippet.
/**
* @param angle_rad angle in radians
* @param speed units/frame
*/
Object::move(double angle_rad, double speed) {
Point p;
p.x = std::sin(angle_rad); // *
p.y = std::cos(angle_rad); // *
p *= speed;
my_position += p;
}
*) You may wish to prefix - to angle_rad, or not.
Notice, that I didn't test it, but it shouldn't be too far away from usable snippet.
This post has been edited by Xupicor: 15 June 2011 - 03:14 PM
#14
Re: Equation for a straight line
Posted 15 June 2011 - 08:27 AM
y = mx + b
m = (y2-y1)/(x2-x1) = (178 - 6)/(291-9) = 86/141
y = (86/141)x + b
we must find 'b'. well if we substitute any point on that line, that equation will hold true. Lets plug in P1
6 = (86/141)(9) + b
b = 6 - (86/141)(9) = 24/47
y = (86/141)x + 24/47
Hopefully I didn't mess that up lol
m = (y2-y1)/(x2-x1) = (178 - 6)/(291-9) = 86/141
y = (86/141)x + b
we must find 'b'. well if we substitute any point on that line, that equation will hold true. Lets plug in P1
6 = (86/141)(9) + b
b = 6 - (86/141)(9) = 24/47
y = (86/141)x + 24/47
Hopefully I didn't mess that up lol
This post has been edited by ImaSexy: 15 June 2011 - 09:12 AM
#15
Re: Equation for a straight line
Posted 15 June 2011 - 09:06 AM
So there is this great little thing called "liner interpolation" and that is basically the process of finding the points between two points. I use it a great deal in programming so I thought I would tell you about it.
It is not exactly the same as finding the equation for a line passing between two points but it is related.
So the idea of linear interpolation is that I have a number x1 and another number x2 I can make a function (math function not necessarily programming function) f(x) that will give me
f(0) = x1
f(1) = x2
f(1/2) = halfway between x1 and x2
and for all 0 <= a <= 1, x1 <= f(a) <= x2
more importantly f(a) lies on the line between x1 and x2.
How does this wonderful function work?
f(x) = (x2 - x1) * x + x1
So:
f(0) = x1
f(1/2) = (x2 - x1)/2 + x1 = 1/2 of the way between x1 and x2 -- try it out
f(1) = (x2- x1) * 1 + x1 = (x2 - x1) + x1 = x2
That is just great! it can be used for estimating intermediate values. Say I had 100$ today and 200$ a month from now and I know my investment increased steadily what might I have had half way though:
(200 - 100)/2 + 100 = 150$
This is all great and wonderful but what if I actually wanted to know that value for each day in a 30 day month... well I would loop though in steps of 1/30... so for example:
f(day) = (200 - 100) * (day/30) + 100
or
f(day) = (x2 - x1) * (day/30) + x1
OR even more abstractly
f(n) = (x2 - x1) * (n/N) + x1
where n is the day, and N is the total number of days between having x1 dollars and x2
so if N = 30 and n = 3 then I would have:
f(3) = (200 - 100) * (3/30) + 100 = 110$ So I had 110$ on day 3
f(6) = (200 - 100) * (3/30) + 100 = 120$ and so on...
f(29) = (200 - 100)*(29/30) + 100 = 196.67$
So now lets ask another question... say I put the money in on the 5th with 100$ and I reached 200$ on the 25th -- what was in the account after 10 days?
Well:
f(n) = (x2 - x1) * (n/N) + x1
N = 25 - 5 = 20 days
f(10) = (200 - 100)* (10/20) + 100 = 150
in general if I know the value is x1 on day d1 and x2 on day d2 then N = d2 - d1
f(d) = (x2 - x1) * d / (d2 - d1) + x1
move things about a little and you have:
f(d) = ((x2 - x1)/(d2 - d1)) * d + x1
...here I realize that I probably should not have used x as I have since in coordinates you generally think of x as the horizontal or (x-axis) -- but here we would want to plot amount of money on the y-axis and time on the x-axis so lets just swap that around:
f(s) = ((y2-y1)/(x2-x1) * s + y1)
of course there is still a little problem with this formula (i.e. it is not the formula for a line between two points quite yet).
In our logic s here always goes from 0 to N with 0 corresponding to the value at x1 and N corresponding to the value at x2 -- so we need to shift the value of s so that it is zero when at x1 and N when at x2
remember that N = (x2 - x1) (the difference between the days: day 25 - day 5 = 20 = N etc.)
s(x) = (x - x1)
s(x1) = (x1 - x1) = 0
s(x2) = (x2 - x1) = N
So
f(x) = (y2 - y1)/(x2 - x1) * (x - x1) + y1
or
(y - y1) = (y2 - y1)/(x2 - x1) * (x - x1)
So long story that you will never remember (and you don't have to, I don't do pop-quizzes) but you can derive the equation for a line between two points from linear interpolation. There ARE easier ways to get there -- such as the using the definition of slope:
slope is "rise over run" or (dy/dx) where dy = difference in y, dx = difference in x
m = dy/dx = (y2 - y1)/(x2 - x1)
another difference might be the change from our starting point to another point (x, y) on the line:
m' = dy/dx = (y - y1)/(x - x1)
the slope of a line is the same everywhere on the line so m = m'
(y2 - y1)/(x2 - x1) = (y - y1)/(x - x1) -- lets put the unknowns on opposite sides
((y2 - y1)/(x2 - x1))* (x- x1) = (y - y1) -- solve for y
y = ((y2 - y1)/(x2 - x1)) * (x - x1) + y1
and we have the formula for a line passing though two point (x1, y1), (x2, y2)
So -- memorizing forulas is great and all. But understanding and a little algebra can save you from having to memorize a lot... or memorizing a lot can save you from some algebra and maybe a little understanding.
It is not exactly the same as finding the equation for a line passing between two points but it is related.
So the idea of linear interpolation is that I have a number x1 and another number x2 I can make a function (math function not necessarily programming function) f(x) that will give me
f(0) = x1
f(1) = x2
f(1/2) = halfway between x1 and x2
and for all 0 <= a <= 1, x1 <= f(a) <= x2
more importantly f(a) lies on the line between x1 and x2.
How does this wonderful function work?
f(x) = (x2 - x1) * x + x1
So:
f(0) = x1
f(1/2) = (x2 - x1)/2 + x1 = 1/2 of the way between x1 and x2 -- try it out
f(1) = (x2- x1) * 1 + x1 = (x2 - x1) + x1 = x2
That is just great! it can be used for estimating intermediate values. Say I had 100$ today and 200$ a month from now and I know my investment increased steadily what might I have had half way though:
(200 - 100)/2 + 100 = 150$
This is all great and wonderful but what if I actually wanted to know that value for each day in a 30 day month... well I would loop though in steps of 1/30... so for example:
f(day) = (200 - 100) * (day/30) + 100
or
f(day) = (x2 - x1) * (day/30) + x1
OR even more abstractly
f(n) = (x2 - x1) * (n/N) + x1
where n is the day, and N is the total number of days between having x1 dollars and x2
so if N = 30 and n = 3 then I would have:
f(3) = (200 - 100) * (3/30) + 100 = 110$ So I had 110$ on day 3
f(6) = (200 - 100) * (3/30) + 100 = 120$ and so on...
f(29) = (200 - 100)*(29/30) + 100 = 196.67$
So now lets ask another question... say I put the money in on the 5th with 100$ and I reached 200$ on the 25th -- what was in the account after 10 days?
Well:
f(n) = (x2 - x1) * (n/N) + x1
N = 25 - 5 = 20 days
f(10) = (200 - 100)* (10/20) + 100 = 150
in general if I know the value is x1 on day d1 and x2 on day d2 then N = d2 - d1
f(d) = (x2 - x1) * d / (d2 - d1) + x1
move things about a little and you have:
f(d) = ((x2 - x1)/(d2 - d1)) * d + x1
...here I realize that I probably should not have used x as I have since in coordinates you generally think of x as the horizontal or (x-axis) -- but here we would want to plot amount of money on the y-axis and time on the x-axis so lets just swap that around:
f(s) = ((y2-y1)/(x2-x1) * s + y1)
of course there is still a little problem with this formula (i.e. it is not the formula for a line between two points quite yet).
In our logic s here always goes from 0 to N with 0 corresponding to the value at x1 and N corresponding to the value at x2 -- so we need to shift the value of s so that it is zero when at x1 and N when at x2
remember that N = (x2 - x1) (the difference between the days: day 25 - day 5 = 20 = N etc.)
s(x) = (x - x1)
s(x1) = (x1 - x1) = 0
s(x2) = (x2 - x1) = N
So
f(x) = (y2 - y1)/(x2 - x1) * (x - x1) + y1
or
(y - y1) = (y2 - y1)/(x2 - x1) * (x - x1)
So long story that you will never remember (and you don't have to, I don't do pop-quizzes) but you can derive the equation for a line between two points from linear interpolation. There ARE easier ways to get there -- such as the using the definition of slope:
slope is "rise over run" or (dy/dx) where dy = difference in y, dx = difference in x
m = dy/dx = (y2 - y1)/(x2 - x1)
another difference might be the change from our starting point to another point (x, y) on the line:
m' = dy/dx = (y - y1)/(x - x1)
the slope of a line is the same everywhere on the line so m = m'
(y2 - y1)/(x2 - x1) = (y - y1)/(x - x1) -- lets put the unknowns on opposite sides
((y2 - y1)/(x2 - x1))* (x- x1) = (y - y1) -- solve for y
y = ((y2 - y1)/(x2 - x1)) * (x - x1) + y1
and we have the formula for a line passing though two point (x1, y1), (x2, y2)
So -- memorizing forulas is great and all. But understanding and a little algebra can save you from having to memorize a lot... or memorizing a lot can save you from some algebra and maybe a little understanding.

New Topic/Question
Reply



MultiQuote







|