code a program in python to rotate a generic point in 3d a certain angle alpha along any…
- User
code a program in python to rotate a generic point in 3d a certain angle alpha along any line defined by two points
Assistant
Below is a Python program that rotates a generic point in 3D by a certain angle (
alpha) along any line defined by two points. This program utilizes vector operations and the concept of rotation matrices, specifically the Rodrigues' rotation formula, which is efficient for rotating vectors around an arbitrary axis in 3D space.First, ensure you have NumPy installed in your environment, as it's used for vector and matrix operations. You can install NumPy via pip if you haven't already:
bashpip install numpyHere's the Python program:
pythonimport numpy as np def normalize_vector(v): """Normalize a vector to have a length of 1.""" return v / np.linalg.norm(v) def rotation_matrix_from_axis_angle(axis, angle): """ Compute rotation matrix from axis and angle (in radians) using Rodrigues' formula. Parameters: - axis (numpy.array): 3D unit vector representing the rotation axis. - angle (float): Rotation angle in radians. Returns: - rotation_matrix (numpy.array): 3x3 rotation matrix. """ K = np.array([[0, -axis[2], axis[1]], [axis[2], 0, -axis[0]], [-axis[1], axis[0], 0]]) return np.eye(3) + np.sin(angle) * K + (1 - np.cos(angle)) * np.dot(K, K) def rotate_point_along_line(point, line_start, line_end, alpha): """ Rotate a point by an angle alpha (in radians) around the line defined by two points. Parameters: - point (numpy.array): 3D point to be rotated. - line_start (numpy.array): Starting point of the line. - line_end (numpy.array): Ending point of the line. - alpha (float): Rotation angle in radians. Returns: - rotated_point (numpy.array): The point rotated by alpha around the specified line. """ # Calculate the direction vector of the line line_direction = normalize_vector(line_end - line_start) # Compute the rotation matrix rotation_matrix = rotation_matrix_from_axis_angle(line_direction, alpha) # Translate the point to the origin relative to the line start translated_point = point - line_start # Rotate the translated point rotated_translated_point = np.dot(rotation_matrix, translated_point) # Translate the rotated point back rotated_point = rotated_translated_point + line_start return rotated_point # Example usage if __name__ == "__main__": # Define the point to rotate point = np.array([1, 2, 3]) # Define the line (two points) line_start = np.array([0, 0, 0]) # Origin for simplicity line_end = np.array([1, 1, 1]) # Diagonal line for demonstration # Define the rotation angle in radians alpha = np.radians(45) # 45 degrees in radians # Rotate the point rotated_point = rotate_point_along_line(point, line_start, line_end, alpha) print("Original Point:", point) print("Rotated Point:", rotated_point)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.