汽缸“切割”蟒蛇中的一个球体 [英] cylinder "cuts" a sphere in python
问题描述
由于我无法亲手绘制,我使用python / matplotlib绘制一个球体和一个圆柱,而不是切入并绘制一条曲线。
圆柱体有方程
x 2 + y ^ 2-a * x = 0
球体是用下面的公式完成的:
lockquote
x ^ 2 + y ^ 2 + z ^ 2 = a ^ 2
我输入a并用
绘制球体
# - * - 编码:utf-8 - * - 从numpy导入的
*
导入matplotlib.pyplot为plt
from matplotlib import cm $ b $ from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
a = input('a?')
u,v = mgrid [0:2 * pi:20j,0:pi / 2:10j]
x = a * cos(u)* sin(v)
y = a * sin(u)* sin(v)
z = a * cos(v)
ax.plot_wireframe(x,y,z,color ='b')
ax.set_xlabel(x); ax.set_ylabel( Y); ax.set_zlabel(z)
plt.show()
plt.close()
现在我想绘制圆柱体,如果能够绘制出这两个数字相交的曲线......但不知道......
我想我忘了说这是xyz coordenates的第一部分(全部是正面的)
解决方案圆柱的等价参数方程be
pre $ code> x = b * sin(u)+ xoff
y = b * cos(u)+ yoff
z = v
假设您需要不同的半径和原点。
我真的不明白你想要作为输出。大概不是两个表面的交集 - 这将是一个曲线(我会认为一个球形楔形是一个坚实的形状 http://en.wikipedia.org/wiki/Spherical_wedge )你可以用搅拌机做你想做的吗?
编辑 - 偏移量。
您可以按照您计算的点的数组创建曲线 http://en.wikipedia.org/wiki/Sphere%E2%80%93cylinder_intersection (这些照片是你想要的东西吗?)
编辑2 - 只是看着你的代码,注意到一些小问题,比如没有制作一个完整的球体。无论如何,这里有一些开始 - 你需要过滤的对象不相交负数的平方根!
...
u,v = mgrid [0:2 * pi:50j,0:pi:25j]
a1 = input('a1?')
x1 = a1 * cos(u)* sin(v)
y1 = a1 * sin(u)* sin(v)
z1 = a1 * cos(v)
ax.plot_wireframe(x1,y1 ,z1,color ='b')
a2 = input('a2?')
xoff = input('offset?')
x2 = a2 * cos(u )+ xoff
y2 = a2 * sin(u)
z2 =(v-pi / 2)* a1 /(pi / 2)
ax.plot_wireframe(x2,y2,z2, color ='g')
x3 = a2 * cos(u [:,0])+ xoff
y3 = a2 * sin(u [:,0])
zsq =(a1 ** 2 - x3 ** 2 - y3 ** 2)
掩码=其中(zsq> 0)
x3 = x3 [掩码]
y3 = y3 [掩码]
z3 = zsq [mask] ** 0.5
ax.plot(x3,y3,z3,color ='r')
ax.plot(x3,y3,-z3,color ='r')
...
As i can't draw it well by hand, I am using python/matplotlib to draw a sphere and a cylinder than "cut through" and make a curve. The cylinder has equation
x^2+y^2-a*x=0
The sphere is done with the equation:
x^2+y^2+z^2=a^2
I input a and draw the sphere with
# -*- coding: utf-8 -*-
from numpy import*
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
fig=plt.figure()
ax=Axes3D(fig)
a=input('a? ')
u,v=mgrid[0:2*pi:20j,0:pi/2:10j]
x=a*cos(u)*sin(v)
y=a*sin(u)*sin(v)
z=a*cos(v)
ax.plot_wireframe(x,y,z,color='b')
ax.set_xlabel("x"); ax.set_ylabel("y"); ax.set_zlabel("z")
plt.show()
plt.close()
Now i want to draw the cylinder and if able, the curve that makes the intersection of the two figures... but no idea...
I think i forgot to say that is first section (all positive) of xyz coordenates
the equivalent parametric equations for the cylinder would be
x = b * sin(u) + xoff
y = b * cos(u) + yoff
z = v
assuming you want a different radius and origin.
I don't really understand what you are trying to get as output. Presumably not the intersection of two surfaces - which would be a curve (I would have thought a sphere wedge was a solid shape http://en.wikipedia.org/wiki/Spherical_wedge) Could you do what you want with blender?
EDIT - offsets.
You can create the curve as array of points which you calculate along these lines http://en.wikipedia.org/wiki/Sphere%E2%80%93cylinder_intersection (are those pictures the kind of thing you want?)
EDIT2 - just looked at your code and noticed a couple of minor issues such as not making a full sphere. Anyway here's something to start with - you need to filter out the square roots of negative numbers where the object don't intersect!
...
u,v=mgrid[0:2*pi:50j,0:pi:25j]
a1 = input('a1? ')
x1 = a1 * cos(u) * sin(v)
y1 = a1 * sin(u) * sin(v)
z1 = a1 * cos(v)
ax.plot_wireframe(x1, y1, z1, color='b')
a2 = input('a2? ')
xoff = input('offset? ')
x2 = a2 * cos(u) + xoff
y2 = a2 * sin(u)
z2 = (v - pi/2) * a1 / (pi/2)
ax.plot_wireframe(x2, y2, z2, color='g')
x3 = a2 * cos(u[:,0]) + xoff
y3 = a2 * sin(u[:,0])
zsq = (a1**2 - x3**2 - y3**2)
mask = where(zsq > 0)
x3 = x3[mask]
y3 = y3[mask]
z3 = zsq[mask] ** 0.5
ax.plot(x3, y3, z3, color='r')
ax.plot(x3, y3, -z3, color='r')
...
这篇关于汽缸“切割”蟒蛇中的一个球体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!