Py5Vector.norm#

Normalized copy of the vector.

Examples#

 1v1 = py5.Py5Vector(40, 0)
 2v2 = py5.Py5Vector(50, 50)
 3
 4print(v1)
 5# Py5Vector2D(40., 0.)
 6print(v1.norm)
 7# Py5Vector2D(1., 0.)
 8print(v2)
 9# Py5Vector2D(50., 50.)
10print(v2.norm)
11# Py5Vector2D(0.70710678, 0.70710678)
 1def setup():
 2    global v1
 3    v1 = py5.Py5Vector(40, 0)
 4
 5
 6def draw():
 7    py5.background(255)
 8    py5.translate(py5.width / 2, py5.height / 2)
 9    py5.stroke(0)
10    py5.stroke_weight(4)
11    py5.line(0, 0, v1.x, v1.y)
12    vm = py5.Py5Vector(py5.mouse_x - py5.width / 2, py5.mouse_y - py5.height / 2)
13    py5.stroke_weight(2)
14    py5.stroke(0, 255, 0)
15    py5.line(0, 0, vm.x, vm.y)
16    v2 = v1.norm.dot(vm) * v1.norm
17    py5.stroke(255, 0, 0)
18    py5.line(0, 0, v2.x, v2.y)

Description#

Normalized copy of the vector. The normalized copy will have a magnitude of 1.0. This property cannot be used on a vector of zeros, because a vector of zeros cannot be normalized.

Updated on January 16, 2022 16:51:21pm UTC