Lets say you have the following method:

CGPoint vectorSubtraction(CGPoint a, CGPoint b) {
    return CGPointMake(a.x - b.x, a.y - b.y);
}

And you use the following points:

Point A: (1,4)
Point B: (10,3)

If you use the vectorSubtraction method, you get:

A POINT at (9,-1).

Now, I would understand if (9,-1) was the vector from the first to the second point (and it is), but I don't understand why the vector itself is a POINT. Vectors by definition have a direction and magnitude. How can a point have direction and magnitude?

有帮助吗?

解决方案

For those interested in the background of this question, it's almost certainly form Ray Wenderlich's Sprite Kit Tutorial for Beginners. Search that link for the heading "Shooting Projectiles".

In terms of the situations where a point can be considered a vector, it can if the other end of the vector is implicit rather than explicit.

In this particular case, the point is being treated as a vector from the origin (0,0) to the point given.

So the actual vector calculation is (you appear to have the points reversed by the way):

(0,0)->(10,3) - (0,0)->(1,4) = (0,0)->(9,-1)

其他提示

Approaching the problem from a (not actually) different direction:

How can a point have direction and magnitude?

So like this:

double direction = atan2(point.y, point.x);
double magnitude = hypot(point.x, point.y);

Good old Descartes coordinate system.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top