如何在三維坐標系中畫一個標準的球?
LaTeX TikZ
documentclass{standalone}
usepackage{tikz}
usetikzlibrary{calc,fadings,decorations.pathreplacing}
ewcommandpgfmathsinandcos[3]{%
pgfmathsetmacro#1{sin(#3)}%
pgfmathsetmacro#2{cos(#3)}%
}
ewcommandLongitudePlane[3][current plane]{%
pgfmathsinandcossinElcosEl{#2} % elevation
pgfmathsinandcossintcost{#3} % azimuth
ikzset{#1/.estyle={cm={cost,sint*sinEl,0,cosEl,(0,0)}}}
}
ewcommandLatitudePlane[3][current plane]{%
pgfmathsinandcossinElcosEl{#2} % elevation
pgfmathsinandcossintcost{#3} % latitude
pgfmathsetmacroyshift{cosEl*sint}
ikzset{#1/.estyle={cm={cost,0,0,cost*sinEl,(0,yshift)}}} %
}
ewcommandDrawLongitudeCircle[2][1]{
LongitudePlane{angEl}{#2}
ikzset{current plane/.prefix style={scale=#1}}
% angle of "visibility"
pgfmathsetmacroangVis{atan(sin(#2)*cos(angEl)/sin(angEl))} %
draw[current plane] (angVis:1) arc (angVis:angVis+180:1);
draw[current plane,dashed] (angVis-180:1) arc (angVis-180:angVis:1);
}
ewcommandDrawLatitudeCircle[2][1]{
LatitudePlane{angEl}{#2}
ikzset{current plane/.prefix style={scale=#1}}
pgfmathsetmacrosinVis{sin(#2)/cos(#2)*sin(angEl)/cos(angEl)}
% angle of "visibility"
pgfmathsetmacroangVis{asin(min(1,max(sinVis,-1)))}
draw[current plane] (angVis:1) arc (angVis:-angVis-180:1);
draw[current plane,dashed] (180-angVis:1) arc (180-angVis:angVis:1);
}
egin{document}
egin{tikzpicture} % "THE GLOBE" showcase
defR{5} % sphere radius
defangEl{35} % elevation angle
filldraw[ball color=white] (0,0) circle (R);
foreach in {-80,-60,...,80} { DrawLatitudeCircle[R]{ } }
foreach in {-5,-35,...,-175} { DrawLongitudeCircle[R]{ } }
end{tikzpicture}
end{document}
代碼節錄自Tomas M. Trzeciak 的 Stereographic and cylindrical map projections。
基本上所有的3D軟體都有創建球體的功能,這算是三維建模的基礎。這裡使用我寫的一個軟體:數學圖形可視化工具來創建球體。軟體中使用自定義語法的腳本代碼生成數學圖形。該軟體是開源的噢。
在我剛學計算機圖形學的時候,就寫過生成球面的C++程序,代碼發布在球(Sphere)圖形的生成演算法。球與圓相關,一個是三維,一個是二維,可以參考下:圓,橢圓
(1)sphere的第一種公式寫法
vertices = D1:100 D2:100
t = from 0 to (PI*2) D1
r = from 0 to 1 D2
x = 2*r*sin(t)*sqrt(1-r^2)
y = 2*r*cos(t)*sqrt(1-r^2)
z = 1-2*(r^2)
球的網格線:
(2)sphere的另兩種公式寫法
vertices = dimension1:36 dimension2:72
u = from 0 to (2*PI) dimension1
v = from (-PI*0.5) to (PI*0.5) dimension2
r = 10.0
x = r*cos(v)*sin(u)
y = r*sin(v)
z = r*cos(v)*cos(u)
和
vertices = dimension1:36 dimension2:72
u = from 0 to (2*PI) dimension1
v = from 0 to (PI) dimension2
r = 10.0
x = r*sin(v)*sin(u)
y = r*cos(v)
z = r*sin(v)*cos(u)
兩種公式生成的圖形是一樣的
(3)彩色球
在腳本中給rgb變數設值,就能設置頂點色.
vertices = dimension1:72 dimension2:72
u = from 0 to (2*PI) dimension1
v = from (-PI*0.5) to (PI*0.5) dimension2
x = cos(v)*sin(u)
y = sin(v)
z = cos(v)*cos(u)
a = 10.0
r = (x+1.0)/2
g = (y+1.0)/2
b = (z+1.0)/2
x = a*x
y = a*y
z = a*z
(4)圓弧面
將球的第二維度範圍減小,即得到圓弧面
vertices = dimension1:36 dimension2:72
u = from 0 to (2*PI) dimension1
v = from (PI*0.1) to (PI*0.5) dimension2
r = 10.0
x = r*cos(v)*sin(u)
y = r*sin(v)
z = r*cos(v)*cos(u)
(5)橢球
#http://www.mathcurve.com/surfaces/ellipsoid/ellipsoid.shtml
vertices = D1:100 D2:100
u = from 0 to (2*PI) D1
v = from (-PI*0.5) to (PI*0.5) D2
a = rand2(1, 10)
b = rand2(1, 10)
c = rand2(1, 10)
x = a*cos(v)*sin(u)
y = b*sin(v)
z = c*cos(v)*cos(u)
(6)膠囊體
將球面向上下兩頭拉伸,即得到膠囊體,我也曾經寫過膠囊體的C++生成演算法膠囊體(Capsule)圖形的生成演算法
(7)刺球
將球面上頂點到球心的距離,有規律地變化,可以得到多變的球,如刺球
vertices = dimension1:129 dimension2:65
u = from 0 to (2*PI) dimension1
v = from (-PI*0.5) to (PI*0.5) dimension2
n =4
a = from 0 to 128 D1
b = from 0 to 64 D2
t = (mod(a, n) + mod(b, n))/n*4
r = 10.0 + t
x = r*cos(v)*sin(u)
y = r*sin(v)
z = r*cos(v)*cos(u)
照著課本繪製的球草圖:
推薦閱讀:
※是否已有人發展並非基於數字而是一些幾何關係(例如π黃金分割等)的數學?這種數學是否能更有效地描述現實?
※如何計算一組三維空間角度數據的方差(或者說離散程度)?
※在n維空間中有s條直線,那麼它們之間最小的夾角最大可以達到多少?
※如何證明三角形兩邊之和大於第三邊?
※如何判斷兩條軌跡(或曲線)的相似度?