Sobes.tech
Junior — Middle

How to perform rotation of a set of 3D coordinate points by 30 degrees around a chosen axis?

sobes.tech AI

Answer from AI

To rotate a set of 3D points by 30 degrees around a chosen axis (for example, the X, Y, or Z axis), rotation matrices can be used. In Python, the NumPy library is convenient for working with matrices and vectors.

Example of rotating around the Z axis by 30 degrees:

import numpy as np

def rotate_points(points, angle_deg, axis='z'):
    angle_rad = np.radians(angle_deg)
    if axis == 'x':
        R = np.array([
            [1, 0, 0],
            [0, np.cos(angle_rad), -np.sin(angle_rad)],
            [0, np.sin(angle_rad), np.cos(angle_rad)]
        ])
    elif axis == 'y':
        R = np.array([
            [np.cos(angle_rad), 0, np.sin(angle_rad)],
            [0, 1, 0],
            [-np.sin(angle_rad), 0, np.cos(angle_rad)]
        ])
    elif axis == 'z':
        R = np.array([
            [np.cos(angle_rad), -np.sin(angle_rad), 0],
            [np.sin(angle_rad), np.cos(angle_rad), 0],
            [0, 0, 1]
        ])
    else:
        raise ValueError("Axis must be 'x', 'y' or 'z'")

    rotated_points = points @ R.T  # multiply each point by the rotation matrix
    return rotated_points

# Example usage:
points = np.array([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
])
rotated = rotate_points(points, 30, axis='z')
print(rotated)

Here, points is an array with the coordinates of the points (each row is a point), and the function returns the new coordinates after rotation.