没有 Matplotlib 的 Python 中 x、y、z 点的二维图
2D plot of x,y,z points in Python without Matplotlib
我有一组 3d 点(我根据开普勒方程生成恒星系统中行星和卫星的位置)我有所有点的坐标为 x、y、z,其中中心星为 0, 0,0。生成点的代码完美运行。
然而,目前我从上面绘制了这个系统的可视化图 - 所以我只是为了可视化的目的而忽略了 z 分量,并将 x 和 y 按原样绘制到 canvas。这按预期工作。
我如何生成 x 和 y 坐标以绘制到考虑到 z 坐标的 canvas,以便我可以从正上方以外的另一个角度绘制视图?
除了标准库之外,我唯一可以使用的库是 numpy。我无法使用 Matplotlib。
编辑 感谢我现在可以用一些伪代码澄清的评论。
假设我有一堆具有 xyz 位置的点。
我目前在做什么:
canvas.plot(point.x)
canvas.plot(point.y)
忽略点 z - 这样就好像所有 z 都是 0 并且从 'above'
看
所以我可以使用我当前的绘图代码 - 它考虑了与 canvas 相关的比例和偏移,我需要新的 x 和 y 坐标,就好像视图是从另一个角度其他比 'above'.
从有用的评论看来,我必须做的是旋转整个坐标系,使其具有新的 z 轴,这是整个系统绕 x 和 y 轴旋转的结果。
类似下面的伪代码就可以了。
def rotate_about_axis(x_rotation_degrees, y_rotation_degrees, point.x, point.y, point.z):
new_plot_x = canvas_point_to_plot after magic code to rotate coordinates x_rotation_degrees about x axis
new_plot_y = canvas_point_to_plot after magic code to rotate coordinates y_rotation_degrees about y axis
return new_plot_x, new_plot_y
然后我可以将其应用于我绘制的所有点。
我如何在 python 中执行此操作?
我想出了一个答案,我希望它能帮助别人。
import numpy, math
def rotate_about_axis(x_rotation_degrees, y_rotation_degrees, point_x, point_y, point_z):
xrads = math.radians(x_rotation_degrees)
yrads = math.radians(y_rotation_degrees)
rotation = [xrads, yrads, 0]
rotation_angle = numpy.linalg.norm(rotation)
norm_rotation = rotation / numpy.linalg.norm(rotation)
base_points = [point_x, point_y, point_z]
points = numpy.dot(base_points, norm_rotation) * norm_rotation
points_difference = base_points - points
points_transform = numpy.cross(norm_rotation, base_points)
rotated_points = points + points_difference * numpy.cos(rotation_angle) + points_transform * numpy.sin(rotation_angle)
rotated_point_x = rotated_points[0]
rotated_point_y = rotated_points[1]
return(rotated_point_x, rotated_point_y)
我有一组 3d 点(我根据开普勒方程生成恒星系统中行星和卫星的位置)我有所有点的坐标为 x、y、z,其中中心星为 0, 0,0。生成点的代码完美运行。
然而,目前我从上面绘制了这个系统的可视化图 - 所以我只是为了可视化的目的而忽略了 z 分量,并将 x 和 y 按原样绘制到 canvas。这按预期工作。
我如何生成 x 和 y 坐标以绘制到考虑到 z 坐标的 canvas,以便我可以从正上方以外的另一个角度绘制视图?
除了标准库之外,我唯一可以使用的库是 numpy。我无法使用 Matplotlib。
编辑 感谢我现在可以用一些伪代码澄清的评论。
假设我有一堆具有 xyz 位置的点。
我目前在做什么:
canvas.plot(point.x)
canvas.plot(point.y)
忽略点 z - 这样就好像所有 z 都是 0 并且从 'above'
看所以我可以使用我当前的绘图代码 - 它考虑了与 canvas 相关的比例和偏移,我需要新的 x 和 y 坐标,就好像视图是从另一个角度其他比 'above'.
从有用的评论看来,我必须做的是旋转整个坐标系,使其具有新的 z 轴,这是整个系统绕 x 和 y 轴旋转的结果。
类似下面的伪代码就可以了。
def rotate_about_axis(x_rotation_degrees, y_rotation_degrees, point.x, point.y, point.z):
new_plot_x = canvas_point_to_plot after magic code to rotate coordinates x_rotation_degrees about x axis
new_plot_y = canvas_point_to_plot after magic code to rotate coordinates y_rotation_degrees about y axis
return new_plot_x, new_plot_y
然后我可以将其应用于我绘制的所有点。
我如何在 python 中执行此操作?
我想出了一个答案,我希望它能帮助别人。
import numpy, math
def rotate_about_axis(x_rotation_degrees, y_rotation_degrees, point_x, point_y, point_z):
xrads = math.radians(x_rotation_degrees)
yrads = math.radians(y_rotation_degrees)
rotation = [xrads, yrads, 0]
rotation_angle = numpy.linalg.norm(rotation)
norm_rotation = rotation / numpy.linalg.norm(rotation)
base_points = [point_x, point_y, point_z]
points = numpy.dot(base_points, norm_rotation) * norm_rotation
points_difference = base_points - points
points_transform = numpy.cross(norm_rotation, base_points)
rotated_points = points + points_difference * numpy.cos(rotation_angle) + points_transform * numpy.sin(rotation_angle)
rotated_point_x = rotated_points[0]
rotated_point_y = rotated_points[1]
return(rotated_point_x, rotated_point_y)