tags:

views:

92

answers:

6

hello, i have a mathematical problem:

i have a function where the only parameter is the current time. the return should be a position which is used to place an object on a certain place.

int position(int time)
{
    int x = 0; //TODO implement x depending on time
    return x;
}

so basically the function is called every frame to put the object in motion.

the motion should look like this (this is the actual question):

  1. a linear motion for time A, the object moves at constant speed
  2. no motion for time B, the object is stopped
  3. repeat at 1.

thanks!

edit: ok in other words: imagine a car that drives at constant speed for A minutes and then stops for B minutes. then drives again for A minutes and stops again for B minutes.

where is the car at time X?

+1  A: 

With the limited information you give, nothing more specific than

return x * THE_SPEED

can be suggested.

The second condition might be intended as a maximum on the return value, but it's hard to tell without context.

The third specification baffles me.

Kilian Foth
I think what he's saying is that the move / stop cycle repeats every A+B, i.e. cycle count cc = t / (A+B), time in cycle tc = t % (A+B), time moved this cycle tmc = (tc < A) ? tc : A, distance = ((cc * A) + tmc) + THE_SPEED
Rup
A: 

For the first two time intervals you can do this:

time = min(time, TIME_INTERVAL_A);
return time * CONST_SPEED;

This will cause your object to travel at a constant speed over time interval A and then stop.

bshields
A: 

I'm not sure if I understood your problem correctly, but do you want something like this?

(assuming C)

int position(int time)
{
    static int x = 0;
    x += time * SPEED;
    return x;
}
Nick D
+2  A: 

OK, if I understand correctly:

int position(int time)
{
    int count_of_AB_cycles = time / (A + B);
    int time_in_current_cycle = time % (A + B);
    int time_moved_this_cycle = (time_in_current_cycle < A) ? time_in_current_cycle : A;
    int total_time_moving = (count_of_AB_cycles * A) + time_moved_this_cycle;
    return total_time_moving * speed;
}

assuming integer division, etc - you'll need floor()s if A and B are non-integers etc.

Rup
I was about to comment on / vote-up someone else's answer that has just been deleted: they computed `time_in_current_cycle = time - (count_of_AB_cycles * (A + B))` which will be the correct way to do this if you're dealing with floats rather than the mod I have here.
Rup
thanks, i do have integers infact.
clamp
hi Rup, thanks again for your answer. is it possible to contact you privately for a follow up question? :D
clamp
A: 

If I understand correctly, you have an object which moves at a constant speed for a while, then stops, then starts again (at the same speed ?), then stops, then starts, and so forth ? It moves along a 1D path so distance from origin is the only output you are interested in ?

I suggest that you define an auxiliary function called speedAt(time T) which might look something like:

if 0 < T <= 25 then 5;
if 25 < T <= 32 then 0;
if 32 < T <= 47 then 3;
if 47 < T <= 49 then 0;
if 49 < T <= 125 then 1;
if 125 < T then 0.

Now what you need to do is integrate this function to derive the distance travelled at time T which is represented by the area between the graph of T and the horizontal axis (in an x-y plot).

Since this is not a differentiable or continuous function you won't have to do any difficult maths, you can integrate numerically very easily. A bit like this:

distanceTravelled = 0
for t = 1 to T
   distanceTravelled = distanceTravelled + speedAt(t)
end

This probably looks a bit messy to you. A more sophisticated approach would be to derive the value of speedAt from a data structure, but I hope you get the picture.

High Performance Mark
+1  A: 

Something like

int position(int t)
{
  static int pt = -1;
  static int px = 0;
  static int state = 1; // running...
  static int lastt = 0;
  if (pt < 0) { pt = t; lastt = t; }
  if ( state == 1 ) {
     if ( (t-pt) > A ) {
        state = 0;
        pt = t;
     }
  } else {
     if ( (t-pt) > B ) {
        state = 1;
        pt = t;
     }
  }
  px += state ? SPEED*(t-lastt) : 0; // advance
  lastt = t;
  return px;
}

EDIT Comment of the usage of the prev code

The code is meant to be used "run time": it gives no the result provided the time t once and for all. It is programmed to "move" virtually the car by one step each time the fuction is called, according to how much time is passed from previous calling. Suitable for a "game" where the func is called every "tick" or so, and the position of the car must be updated as tick increases, so that it could be e.g. drawn on the screen, tick by tick, in the current position.

If the OP Question was about knowing where the car is at time t, "mathematically", other solution are the good one (also, read my second comment to the question)

ShinTakezou