This code basically makes the dot follow a certain point on the screen. It consumes a significant amount of computing time in my game, so I'd like it to be best optimized. I wrote this code after quickly reading some pages in a high school mathematics book, so it's unlikely to be a computationally efficient solution.The mj_ prefix is just what I use instead of a namespace in my C code.Suggest some good possible optimizations.struct mj_dot {    double x;    double y;    double th;};void mj_dot_rotate(struct mj_dot *, double, double);void mj_dot_move(struct mj_dot *, double, double, double, double);static double mj_distance(double x, double y, double x2, double y2) {    return sqrt(pow(x - x2, 2) + pow(y - y2, 2));}void mj_dot_rotate(struct mj_dot *this, double x, double y) {    double dx = x - this->x;    double dy = y - this->y;    this->th = atan(dy / dx);    if (isnan(this->th)) {        this->th = 0;        return;    }    if (dx < 0) {        this->th += MJ_MATH_PI;    }}void mj_dot_move(struct mj_dot *this, double x, double y, double speed, double seconds) {    double d = speed * seconds;    if (d >= mj_distance(this->x, this->y, x, y)) {        this->x = x;        this->y = y;    } else {        mj_dot_rotate(this, x, y);        this->x += d * cos(this->th);        this->y += d * sin(this->th);    }}
Algorithm to follow a point
performance;c;game;mathematics
SimplificationThere's actually no need for the function mj_dot_rotate() with its complicated trigonometry.  That function does lot of work to find an angle which isn't ever needed.  In fact, it would be most efficient to inline the code for mj_distance() as well since you can reuse parts of it in mj_dot_move().  The key thing to realize is that you can determine the amounts to move in each direction purely as a function of dx, dy, and distance.  Like this:void mj_dot_move(struct mj_dot *this, double x, double y, double speed, double seconds) {    double d  = speed * seconds;    double dx = x - this->x;    double dy = y - this->y;    double distance = sqrt(dx*dx + dy*dy);    if (d >= distance) {        this->x = x;        this->y = y;    } else {        double fraction = d / distance;        this->x += dx * fraction;        this->y += dy * fraction;    }}Variable namingI agree with the other reviewer who said not to use this as a variable name.  I thought for a second that you were using C++ and I started to look for a class definition.