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(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With