Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

math

ios

swift

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).

like image 217
msqar Avatar asked Jun 25 '16 15:06

msqar


1 Answers

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(2, 3)
let pB = CGPoint(x: 7, y: 8) // Point B(7, 8)
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 can read vecAB as "move 5 units in x-direction and 5 units in y-direction", and you would get the same result for A(0, 0) and B(5, 5).

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

like image 190
Martin R Avatar answered Nov 15 '22 10:11

Martin R