使用 VPython 制作标签
VPython
可以轻松创建可导航的 3D 显示和动画,即使对于那些编程经验有限的人来说也是如此。因为它是基于Python的,所以它也为有经验的程序员和研究人员提供了很多东西。 VPython
允许用户在 3D 空间中创建球体和圆锥体等对象,并将这些对象显示在窗口中。这使得创建简单的可视化变得容易,使程序员可以更多地关注他们程序的计算方面。 VPython
的简单性使其成为说明简单物理的工具,尤其是在教育环境中。
安装 :
pip install vpython
标签对象用于在框中显示文本。即使画布旋转,标签也将始终朝前。我们可以使用label()
方法在VPython
中生成标签。
标签() 方法
Syntax : cylinder(parameters)
Parameters :
- pos : It is the point in the world space being labelled. Assign a vector containing 3 values, example pos = vector(0, 0, 0) or the object being labeled, example pos = obj.pos
- pixel_pos : It determines the position in terms of pixels. Assign a boolean value
- align : It is the alignment of the label. Assign a string with either of the options, “center”, “right” and “left”, default is “center”
- color : It is the color of the text of the label. 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
- opacity : It is the opacity of the background of the box. Assign a floating value in which 1 is the most opaque and 0 the least opaque, example opacity = 0.5
- xoffset : It is the offset for the the x axis of the label. Assign a floating value, example xoffset = 2
- yoffset : It is the offset for the the y axis of the label. Assign a floating value, example yoffset = 5
- text : It is the text to be displayed in the label. HTML styles can also be included while assigning the text.
- font : It is the font of the text of the label. Assign a string value, the default is “sans”, example font = “serif”
- height : It is the height of the font in pixels. Assign a integer value, the default is 15, example height = 18
- border : It is the distance in pixels from the text to the surrounding box. Assign a floating value, the default length is 5, example border = 10
- radius : It is the radius of the cylinder. Assign a floating value, the default radius is 1, example radius = 5
- box : It determines whether the box should be drawn or not. Assign a boolean value in which True is yes and False is no, the default is True
- line : It determines whether a line from the pos to the box should be drawn or not. Assign a boolean value in which True is yes and False is no, the default is True
- linecolor : It is the color of the line and the box. Assign a vector containing 3 values, example color = vector(1, 1, 1) will give the linecolor white
- linewidth : It is the thickness of the line drawn from the pos to the box, and the edges of the box. Assign a integer value, the default is 1 pixel, example linewidth = 5
- space : It is the radius in pixels of a sphere surrounding pos, into which the connecting line does not go. Assign a integer value, example space = 20
- visible : It determines whether the label is to displayed or not. Assign a boolean value in which True is yes and False is no, the default is True
All the parameters are optional.
示例 1:没有参数的标签,所有参数都将具有默认值。
# import the module
from vpython import * label()
输出 :
示例 2:使用参数颜色、文本、线宽、线色和边框的标签。
# import the module
from vpython import * label(text = "The mass Msys = 103 kg.",
color = vector(1, 0, 0),
linecolor = vector(0, 1, 0),
linewidth = 3,
border = 10)
输出 :
示例 3:对象的标签。
# import the module
from vpython import *
# the box to be labelled
b = box(color = vector(1, 1, 0),
size = vector(1, 1, 1))
# the label for the box
label(pos = b.pos,
text = "This label is for the box",
font = "sans",
color = vector(0, 0, 1),
linecolor = vector(0, 1, 1),
linewidth = 3,
yoffset = 150,
xoffset = 150)
输出 :
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。