#include <iostream>
#include <cmath>
struct point { double x = 0 ; double y = 0 ; };
std::ostream& operator<< ( std::ostream& stm, point p )
{ return stm << '(' << p.x << ',' << p.y << ')' ; }
double euclidean_distance( point a, point b )
{
constdouble dx = a.x - b.x ;
constdouble dy = a.y - b.y ;
return std::sqrt( dx*dx + dy*dy ) ;
}
struct object
{
point current_position ; // co-ordinates are in metres
// return time taken (in seconds) to move to the destination
// assumes that the object can move in the straight-line path to destination
double move_to( point destination ) // co-ordinates are in metres
{
constdouble distance = euclidean_distance( current_position, destination ) ;
current_position = destination ; // move to destination: update the current position
return distance / velocity ; // return the time taken to move
}
staticconstexprdouble velocity = 2.0 ; // metres per second
};
int main()
{
object x ;
for( point destination : { point{1,2}, point{24,6}, point{9,22}, point{0,0} } )
{
std::cout << "moved from " << x.current_position << " to " << destination ;
std::cout << " in " << x.move_to(destination) << " seconds.\n" ;
}
}
The easier it is for people to read your code, the more likely they are to look at it and figure out the problem.
Also, it helps to be more specific about the error message you're getting. In particularly, the full message will include the line number on which the error is occurring. Giving us the full information that your compiler is giving you, will make it easier for us to help you.
JLBorges, your code gives the time needed to move from A to B, but the OP's function should give the distance walked in a specific amount of time.
compute the (B-A) vector, normalise it and multiply it by the velocity.
that will give you the delta that you'll move, so multiply it by the time and add it to the current position.
As I said, you don't need even that much.
because you have the velocity, all you need is D = VT if you just want the distance. With no accelerations, its that simple. You are re-computing V, but it was a given :) Here, V is 2 (provided) and T is any time > 0 (start).
The direction is irrelevant to the total distance at any given time. You are out in space ... moving 10 km/sec. How far do you go in 10 seconds? The answer is the same no matter what direction you are going.
You need the components to get the current position at a time, though. That is a different question, which I also answered, cheating a little to get there, but it works.
this is true, i called it time, its sort of pseudo code. the important thing was the technique to boil it down to a simple iteration without any distance formula. It certainly won't compile or anything like that.