使用 VPython 制作文本对象
VPython可以轻松创建可导航的 3D 显示和动画,即使对于那些编程经验有限的人来说也是如此。因为它是基于Python的,所以它也为有经验的程序员和研究人员提供了很多东西。 VPython 允许用户在 3D 空间中创建球体和圆锥体等对象,并将这些对象显示在窗口中。这使得创建简单的可视化变得容易,使程序员可以更多地关注他们程序的计算方面。 VPython 的简单性使其成为说明简单物理的工具,尤其是在教育环境中。
安装 :
pip install vpython
文本对象用于显示 3D 文本数据。我们可以使用 text() 方法在 VPython 中生成一个文本对象。
文本()方法
Syntax : text(parameters)
Parameters :
- pos : It is the position of the text object. Assign a vector containing 3 values, example pos = vector(0, 0, 0)
- align : It is the alignment of the text object. Assign a string with either of the options, “center”, “right” and “left”, default is “left”
- height : It is the height of an uppercase letter. Assign a floating value, the default is 1, example height = 18
- length : It is the length of the displayed text. Assign a floating value, example length = 4
- depth : It is the depth of the displayed text. Assign a floating value, the default is 0.2 * height, example depth = 2
- axis : It is the axis of alignment of the text object. Assign a vector containing 3 values, example axis = vector(1, 2, 1)
- up : It is the orientation of the text object. Assign a vector containing 3 values, example up = vector(0, 1, 0)
- font : It is the font of the text. Assign a string with values either “sans or “serif”
- color : It is the color of the text. Assign a vector containing 3 values, example color = vector(1, 1, 1) will give the color white
- background : It is the color of the background of the label. Assign a vector containing 3 values, example color = vector(1, 1, 1) will give the background color white
- billboard : It determines whether the text object always faces you or not. Assign a boolean value in which True is yes and False is no
- opacity : It is the opacity of the text object. Assign a floating value in which 1 is the most opaque and 0 the least opaque, example opacity = 0.5
- shininess : It is the shininess of the text object. Assign a floating value in which 1 is the most shiny and 0 the least shiny, example shininess = 0.6
- emissive : It is the emissivity of the text object. Assign a boolean value in which True is emissive and False is not emissive, example emissivity = False
- text : It is the text to be displayed. HTML styles can also be included while assigning the text.
- descender : It is the height of the descender on lower-case letters such as y. Assign a floating value, the default is 0.3 * height, example descender = 8
- upper_left, upper_right, lower_right, lower_left : They are the bounding box of the displayed text
- start, end : They are the left-most and right-most locations on the baseline
- vertical_spacing : It is the vertical distance from one baseline to the next in a multiline text
All the parameters are optional.
示例 1:只有 text 参数的文本对象,所有其他参数将具有默认值。
Python3
# import the module
from vpython import * text(text = "text")
Python3
# import the module
from vpython import * text(text = "text",
color = vector(0, 0, 1),
opacity = 0.5,
shininess = 1,
emissive = False)
Python3
# import the module
from vpython import *
# the first text object
text(text = "text object 1",
pos = vector(-5, 2, 0),
height = 3,
depth = 1,
color = vector(0.5, 1, 1))
# the second text object
text(text = "text object 2",
pos = vector(1, -1, 5),
color = vector(0, 1, 0))
Python3
# import the module
from vpython import * text(text = "text",
color = vector(1, 0.5, 0),
axis = vector(-1, 4, 0),
up = vector(1, 2, 2))
输出 :
示例 2:使用参数颜色、不透明度、光泽度和发射率的文本对象。
Python3
# import the module
from vpython import * text(text = "text",
color = vector(0, 0, 1),
opacity = 0.5,
shininess = 1,
emissive = False)
输出 :
示例 3:显示 2 个文本对象以可视化属性 pos、height 和 depth。
Python3
# import the module
from vpython import *
# the first text object
text(text = "text object 1",
pos = vector(-5, 2, 0),
height = 3,
depth = 1,
color = vector(0.5, 1, 1))
# the second text object
text(text = "text object 2",
pos = vector(1, -1, 5),
color = vector(0, 1, 0))
输出 :
示例 4:使用参数 axis 和 up 的圆柱体。
Python3
# import the module
from vpython import * text(text = "text",
color = vector(1, 0.5, 0),
axis = vector(-1, 4, 0),
up = vector(1, 2, 2))
输出 :