- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个像 [[1, 2, 2.23], [2, 3, 3.6],[-3, 4, 5], ...] 这样的矩阵,每一行表示一个点。
我想做的是这样的:
我想创建一个函数,它有两个参数:
像 [0,0,0] 这样的中心和上面的矩阵。
然后它计算点到中心的最大距离作为球体的半径,并在矩阵中绘制一个球体。
球体是透明的,所以如果我们绘制点,我们可以看到球体内部的点。
我还需要以某种方式区分最大距离的点。比如从中心到点绘制一个矢量或用不同的颜色绘制它。任何帮助将不胜感激。
最佳答案
基于 this answer :
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
import numpy as np
# interactive mode off, can [normally] be safely removed
plt.ioff()
# define an arrow class:
class Arrow3D(FancyArrowPatch):
def __init__(self, start=[0,0,0], end=[1,1,1], *args, **kwargs):
if "arrowstyle" not in kwargs:
kwargs["arrowstyle"] = "-|>"
if "mutation_scale" not in kwargs:
kwargs["mutation_scale"] = 20
if "color" not in kwargs:
kwargs["color"] = "k"
FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs)
xs = [start[0], end[0]]
ys = [start[1], end[1]]
zs = [start[2], end[2]]
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
FancyArrowPatch.draw(self, renderer)
def WireframeSphere(centre=[0.,0.,0.], radius=1.,
n_meridians=20, n_circles_latitude=None):
"""
Create the arrays of values to plot the wireframe of a sphere.
Parameters
----------
centre: array like
A point, defined as an iterable of three numerical values.
radius: number
The radius of the sphere.
n_meridians: int
The number of meridians to display (circles that pass on both poles).
n_circles_latitude: int
The number of horizontal circles (akin to the Equator) to display.
Notice this includes one for each pole, and defaults to 4 or half
of the *n_meridians* if the latter is larger.
Returns
-------
sphere_x, sphere_y, sphere_z: arrays
The arrays with the coordinates of the points to make the wireframe.
Their shape is (n_meridians, n_circles_latitude).
Examples
--------
>>> fig = plt.figure()
>>> ax = fig.gca(projection='3d')
>>> ax.set_aspect("equal")
>>> sphere = ax.plot_wireframe(*WireframeSphere(), color="r", alpha=0.5)
>>> fig.show()
>>> fig = plt.figure()
>>> ax = fig.gca(projection='3d')
>>> ax.set_aspect("equal")
>>> frame_xs, frame_ys, frame_zs = WireframeSphere()
>>> sphere = ax.plot_wireframe(frame_xs, frame_ys, frame_zs, color="r", alpha=0.5)
>>> fig.show()
"""
if n_circles_latitude is None:
n_circles_latitude = max(n_meridians/2, 4)
u, v = np.mgrid[0:2*np.pi:n_meridians*1j, 0:np.pi:n_circles_latitude*1j]
sphere_x = centre[0] + radius * np.cos(u) * np.sin(v)
sphere_y = centre[1] + radius * np.sin(u) * np.sin(v)
sphere_z = centre[2] + radius * np.cos(v)
return sphere_x, sphere_y, sphere_z
def find_most_distants(points, center=[0.,0.,0.], tol=1e-5):
"""
Finds and returns a list of points that are the most distante ones to
the center.
Parameters
----------
points: list
A list of points (see center to know what a point is)
center: array like
A point, defined as an iterable of three numerical values.
"""
# make central point an array to ease vector calculations
center = np.asarray(center)
# find most distant points
max_distance = 0
most_distant_points = []
for point in points:
distance = np.linalg.norm(center-point)
if abs(distance - max_distance) <= tol:
most_distant_points.append(point)
elif distance > max_distance:
most_distant_points = [point]
max_distance = distance
return max_distance, most_distant_points
def list_of_points_TO_lists_of_coordinates(list_of_points):
"""
Converts a list of points to lists of coordinates of those points.
Parameter
---------
list_of_points: list
A list of points (each defined as an iterable of three numerical values)
Returns
-------
points_x, points_y, points_z: array
Lists of coordinates
"""
points_x = []
points_y = []
points_z = []
for point in list_of_points:
points_x.append(point[0])
points_y.append(point[1])
points_z.append(point[2])
return points_x, points_y, points_z
def function(central_point=[0.,0.,0.],
other_points=[[1., 2., 2.23],
[2., 3., 3.6],
[-3., 4., 5.]],):
"""
Draws a wireframe sphere centered on central_point and containing all
points in other_points list. Also draws the points inside the sphere and
marks the most distant ones with an arrow.
Parameters
----------
central_point: array like
A point, defined as an iterable of three numerical values.
other_points: list
A list of points (see central_point to know what a point is)
"""
# find most distant points
max_distance, most_distant_points = find_most_distants(other_points, central_point)
#prepare figure and 3d axis
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")
#draw sphere
ax.plot_wireframe(*WireframeSphere(central_point, max_distance), color="r", alpha=0.5)
# draw points
ax.scatter(*list_of_points_TO_lists_of_coordinates(other_points))
# draw arrows to most distant points:
for extreme_point in most_distant_points:
ax.add_artist(Arrow3D(start=central_point, end=extreme_point))
fig.show()
if __name__ == '__main__':
function([0,0,0], 2*np.random.rand(50,3)-1)
# make a list with equally most distant point:
repeated_max_list = 2*np.random.rand(10,3)-1
distance, points = find_most_distants(repeated_max_list)
repeated_max_list = np.concatenate((repeated_max_list,points))
repeated_max_list[-1][0] = -repeated_max_list[-1][0]
repeated_max_list[-1][1] = -repeated_max_list[-1][1]
repeated_max_list[-1][2] = -repeated_max_list[-1][2]
function([0,0,0], repeated_max_list)
关于python-3.x - 当给定中心点和半径大小时如何绘制球体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460960/
如何让div的中心点作为旋转的中心点。 我遇到了 this虽然我正在做一些研究,但我似乎无法将其融入我的研究。 这就是帖子所建议的。但不起作用。$(area).css('-webkit-transfo
您好,我正在做一个应用程序,在我的应用程序中有一个 map 页面,所以我实现了 Google Maps V2,我遇到了一个问题,当我滚动 map 时,我想获得 map 的中心点,如果我滚动 map ,
我正在玩一个简单的 2d 游戏,我正在尝试从级别 1(场景一)切换到级别 2(场景二)。为了测试,两个级别包含相同的内容。这是我的转换代码: let transition = SKTransi
我有一个覆盖大部分屏幕区域的 map 的 View 。 在 map 的顶部,我有一些图像、按钮、文本框。它们位于 map 的顶部,我在这里称其为“无用 map ”,而底部则用于实际查看 map 。这是
给定一个纯白色背景上的对象,有人知道 OpenCV 是否提供了从捕获的帧中轻松检测对象的功能吗? 我正在尝试定位对象(矩形)的角点/中心点。我目前这样做的方式是通过蛮力(扫描对象的图像)并且不准确。我
我试图确保我的 map 中心点位于 kml 层的边界内,我无法找到很多相关信息,但我修改了来自 here 的一些代码。这似乎可行,但当然行不通。 从 Google Maps API 中,我无法判断 .
我试图在用户滚动 UIScrollView 时创建一个非常简单的视差效果。我在我的 ScrollView 上使用 scrollViewDidScroll 方法,一切正常。我可以记录该方法的所有内容,因
我正在使用 svg.js 和出色的 svg-pan-zoom 插件 ( https://github.com/ariutta/svg-pan-zoom )。 现在我想要一个按钮,当我单击它时,它会在我
我是一名优秀的程序员,十分优秀!