Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
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
To rotate a point in 3D around a certain angle
alphaalong a line defined by two pointsp1andp2, you can use the following steps:- Calculate the direction vector of the line using the two points:
dir = p2 - p1. - Normalize the direction vector:
dir = dir / np.linalg.norm(dir) - Calculate the cross product of the direction vector and the point to be rotated:
cp = np.cross(dir, point) - Calculate the rotated point using a rotation matrix:
coderotation_matrix = np.array([[np.cos(alpha), -np.sin(alpha), 0], [np.sin(alpha), np.cos(alpha), 0], [0, 0, 1]]) rotated_point = np.dot(rotation_matrix, point)Here's the complete code:
codeimport numpy as np def rotate_point(point, p1, p2, alpha): # Calculate the direction vector of the line dir = p2 - p1 # Normalize the direction vector dir = dir / np.linalg.norm(dir) # Calculate the cross product of the direction vector and the point to be rotated cp = np.cross(dir, point) # Calculate the rotated point using a rotation matrix rotation_matrix = np.array([[np.cos(alpha), -np.sin(alpha), 0], [np.sin(alpha), np.cos(alpha), 0], [0, 0, 1]]) rotated_point = np.dot(rotation_matrix, point) return rotated_point # Test the function point = np.array([1, 0, 0]) p1 = np.array([0, 0, 0]) p2 = np.array([0, 0, 1]) alpha = np.pi / 2 rotated_point = rotate_point(point, p1, p2, alpha) print(rotated_point) # Expected output: [0, 1, 0]Note that this function assumes that the points are given as numpy arrays with three elements, and that
alphais given in radians. You may need to modify the function slightly if you want to use different input formats. - Calculate the direction vector of the line using the two points:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.