Home > Back-end >  C how to implement an object between the two coordinate points loop mobile code
C how to implement an object between the two coordinate points loop mobile code

Time:10-06

Set up two coordinate points, an initial point, an end, with the passage of time the object moving at a constant speed between two points in this cycle, how to implement this code? How to determine the object to the finish and return by the way? The coordinates of an object at any time during the things need to be able to say,
One who can write a code?


CodePudding user response:

 # include & lt; Iostream> 
#include
#include
#include

using namespace std;

Typedef struct MOVE {
Vector The start;
Vector end;
Vector The location;
Vector Direction;
Float speedPerMs;
long time=0;
Long endTime=0;
Long timeGap=0;

MOVE (vector Start, vector End, float speedPerMs, long time) : start (start) and end (the end), location (start), speedPerMs (speedPerMs), time (time) {
Auto length=(float) SQRT (pow (end [0] - start [0], 2) + pow (end [1] - start [1], 2));
Direction={(end [0] - start [0])/length, (end [1] - start [1])/length};
TimeGap=length/speedPerMs;
EndTime=time + timeGap;
}
} moveInfo;

Void move (moveInfo& Info, long time) {
Int dt=time - info. Time;
The info. The location of [0] +=info. Direction [0] * * info in dt speedPerMs;
The info. The location of [1] +=info. Direction [1] * * info in dt speedPerMs;
Info. Time=the time;
If (time & gt;=info. EndTime) {
The info. The location=info. End;
Info. End=info. Start;
Info. Start=info. The location;
Info. Direction={- info. Direction [0], - info. Direction [1]}.
The info. The endTime +=info. TimeGap;
}
}

Int main () {
Struct timeval time {};
Mingw_gettimeofday (& amp; Time, nullptr);
MoveInfo info ({0, 0}, {3, 5}, 0.01, and time. The tv_sec * 1000 + time. Tv_usec/1000);
For (int I=0; i <3000; + + I) {
Mingw_gettimeofday (& amp; Time, nullptr);
Move (info, time. Tv_sec * 1000 + time. Tv_usec/1000);
Cout}
}
  • Related