Py5Vector.rotate()#

Rotate vector by a specified angle.

Examples#

example picture for rotate()
 1def setup():
 2    py5.translate(py5.width / 2, py5.height / 2)
 3    py5.text_align(py5.CENTER, py5.CENTER)
 4    v = py5.Py5Vector(0, 40)
 5    py5.fill(0)
 6    for i in range(1, 13):
 7        v.rotate(-py5.TWO_PI / 12)
 8        py5.text(f'{i}', v.x, -v.y)
 9    py5.line(0, 0, 0, -30)
10    py5.line(0, 0, 20, 0)
example picture for rotate()
 1def setup():
 2    py5.size(100, 100, py5.P3D)
 3    py5.translate(10, 20, -20)
 4    # rotate so the viewer can better see the shape
 5    py5.rotate_z(0.5)
 6    py5.rotate_y(-0.5)
 7    v2 = py5.Py5Vector(60, 0, -30)
 8    with py5.begin_shape(py5.TRIANGLE_FAN):
 9        py5.vertex(0, 0, 0)
10        for i in range(16):
11            v2.rotate(-py5.TWO_PI / 15, dim='x')
12            py5.vertex(v2.x, v2.y, v2.z)

Description#

Rotate vector by a specified angle. This method is only applicable to 2D and 3D vectors. Use the angle parameter to specify the rotation angle. To rotate 3D vectors, you must use the dim parameter to specify which dimension to rotate around. The dimension can be specified with the values 1, 2, or 3, or by using the strings 'x', 'y', or 'z'.

A 2D vector will be rotated in the counter-clockwise direction for positive angle values and in the clockwise direction for negative angle values.

A 3D vector’s rotation will follow the right-hand rule. Using your right hand, point your thumb in the direction of the axis to rotate around. Your fingers will curl in the direction of rotation when the angle parameter is positive.

Syntax#

rotate(angle: float) -> Py5Vector2D
rotate(angle: float, dim: Union[int, str]) -> Py5Vector3D

Parameters#

  • angle: float - angle of rotation, measured in radians

  • dim: Union[int, str] - dimension to rotate around

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