msqar - 5 months ago 34
Swift Question

# How is a Swift CGVector created with dx and dy (derivative)?

I'm trying to understand how a Vector is created in Swift, because when I do CGVectorMake(), tells me to pass a dx and dy (derivative) as a CGFloat.
How can I create a Vector (line) with only that information?

Can anyone explain me ? Like for dummies? I searched in Google but I still couldn't find an easy explanation.

Let's say I would like to create a Vector that goes from point (0,0) to point (5,5).

There are many possible representations of vectors, one is as the "distance" or "displacement" from one point to another point (compare Euclidean vector: Representations).

In that sense, the vector from (0,0) to (5,5) is identical to the vector from (2,3) to (7, 8), and the vector from point A to point B can be computed as

``````let pA = CGPoint(x: 2, y: 3) // Point A
let pB = CGPoint(x: 7, y: 8) // Point B
let vecAB = CGVector(dx: pB.x - pA.x, dy: pB.y - pA.y) // Vector from A to B
print(vecAB) // CGVector(dx: 5.0, dy: 5.0)
``````

So `dx`, `dy` stand for "delta X" and "delta Y", the distance of the points in x- and y-direction. In the above case, you could interpret `vecAB` as "move 5 units in x-direction and 5 units in y-direction".

A "line segment" from (0, 0) to (5, 5) cannot be represented by a vector alone. You would need two points, or one point and one vector.