Vector¶
The Vector represent a 2D vector (x, y). Our implementation is made in top of a Python list.
Exemple for constructing a Vector:
>>> # Construct a point at 82,34
>>> v = Vector(82, 34)
>>> v[0]
82
>>> v.x
82
>>> v[1]
34
>>> v.y
34
>>> # Construct by giving a list of 2 values
>>> pos = (93, 45)
>>> v = Vector(pos)
>>> v[0]
93
>>> v.x
93
>>> v[1]
45
>>> v.y
45
Optimized usage¶
Most of time, you can use a list for arguments, instead of using a Vector. For example, if you want to have the distance between 2 points:
a = (10, 10)
b = (87, 34)
# optimized method
print 'distance between a and b:', Vector(a).distance(b)
# non-optimized method
va = Vector(a)
vb = Vector(b)
print 'distance between a and b:', va.distance(vb)
Vector operators¶
The Vector support some numeric operator like +, -, /:
>>> Vector(1, 1) + Vector(9, 5)
[10, 6]
>>> Vector(9, 5) - Vector(5, 5)
[4, 0]
>>> Vector(10, 10) / Vector(2., 4.)
[5.0, 2.5]
>>> Vector(10, 10) / 5.
[2.0, 2.0]
You can also do in-place operations:
>>> v = Vector(1, 1)
>>> v += 2
>>> v
[3, 3]
>>> v *= 5
[15, 15]
>>> v /= 2.
[7.5, 7.5]
- class kivy.vector.Vector(*largs)¶
Bases: list
Vector class. See module documentation for more information.
- angle(a)¶
Computes the angle between a and b, and return the angle in degrees.
>>> Vector(100, 0).angle((0, 100)) -90.0 >>> Vector(87, 23).angle((-77, 10)) -157.7920283010705
- distance(to)¶
Returns the distance between two points.
>>> Vector(10, 10).distance((5, 10)) 5. >>> a = (90, 33) >>> b = (76, 34) >>> Vector(a).distance(b) 14.035668847618199
- distance2(to)¶
Returns the distance between two points squared.
>>> Vector(10, 10).distance2((5, 10)) 25
- dot(a)¶
Computes the dot product of a and b.
>>> Vector(2, 4).dot((2, 2)) 12
- static in_bbox(point, a, b)¶
Return a true if point is in bbox defined by a and b.
>>> bmin = (0, 0) >>> bmax = (100, 100) >>> Vector.in_bbox((50, 50), bmin, bmax) True >>> Vector.in_bbox((647, -10), bmin, bmax) False
- length()¶
Returns the length of a vector.
>>> Vector(10, 10).length() 14.142135623730951 >>> pos = (10, 10) >>> Vector(pos).length() 14.142135623730951
- length2()¶
Returns the length of a vector squared.
>>> Vector(10, 10).length2() 200 >>> pos = (10, 10) >>> Vector(pos).length2() 200
- static line_intersection(v1, v2, v3, v4)¶
Finds the intersection point between the lines (1)v1->v2 and (2)v3->v4 and returns it as a vector object.
>>> a = (98, 28) >>> b = (72, 33) >>> c = (10, -5) >>> d = (20, 88) >>> Vector.line_intersection(a, b, c, d) [15.25931928687196, 43.911669367909241]
Warning
This is a line intersection method, not a segment intersection.
For math see: http://en.wikipedia.org/wiki/Line-line_intersection
- normalize()¶
Returns a new vector that has the same direction as vec, but has a length of one.
>>> v = Vector(88, 33).normalize() >>> v [0.93632917756904444, 0.3511234415883917] >>> v.length() 1.0
- rotate(angle)¶
Rotate the vector with an angle in degrees.
>>> v = Vector(100, 0) >>> v.rotate(45) >>> v [70.710678118654755, 70.710678118654741]