Quick search

Vector

The Vector represents a 2D vector (x, y). Our implementation is made on top of a Python list.

Example 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 the time, you can use a list for arguments instead of using a Vector. For example, if you want to calculate 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 supports some numeric operators 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)[source]

Bases: list

Vector class. See module documentation for more information.

angle(a)[source]

Computes the angle between a and b, and returns the angle in degrees.

>>> Vector(100, 0).angle((0, 100))
-90.0
>>> Vector(87, 23).angle((-77, 10))
-157.7920283010705
distance(to)[source]

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)[source]

Returns the distance between two points squared.

>>> Vector(10, 10).distance2((5, 10))
25
dot(a)[source]

Computes the dot product of a and b.

>>> Vector(2, 4).dot((2, 2))
12
static in_bbox(point, a, b)[source]

Return True if point is in the bounding box 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()[source]

Returns the length of a vector.

>>> Vector(10, 10).length()
14.142135623730951
>>> pos = (10, 10)
>>> Vector(pos).length()
14.142135623730951
length2()[source]

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)[source]

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()[source]

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)[source]

Rotate the vector with an angle in degrees.

>>> v = Vector(100, 0)
>>> v.rotate(45)
>>> v
[70.710678118654755, 70.710678118654741]
static segment_intersection(v1, v2, v3, v4)[source]

Finds the intersection point between segments (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.segment_intersection(a, b, c, d)
None
>>> a = (0, 0)
>>> b = (10, 10)
>>> c = (0, 10)
>>> d = (10, 0)
>>> Vector.segment_intersection(a, b, c, d)
[5, 5]
x

x represents the first element in the list.

>>> v = Vector(12, 23)
>>> v[0]
12
>>> v.x
12
y

y represents the second element in the list.

>>> v = Vector(12, 23)
>>> v[1]
23
>>> v.y
23