panda3d通过长方体对角线两个坐标点:xyz的最大值和最小值最简单判断一个点是否在这个长方体范围内画出这个点以及长方体边框from directshowbaseShowBase import ShowBasefrom panda3dcore import Point3 LineSegsclass MyAppShowBase def __init__self ShowBase
可能是因为在绘制长方体时,没有调用 ls.create() 方法将绘制的线段添加到场景中。修改代码如下:
ls.create(self.render)
修改为:
node = ls.create()
self.render.attachNewNode(node)
完整的代码如下:
from direct.showbase.ShowBase import ShowBase
from panda3d.core import Point3, LineSegs
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# 定义长方体对角线两个坐标点的最大值和最小值
xmin, ymin, zmin = (-1, -1, -1)
xmax, ymax, zmax = (1, 1, 1)
# 定义需要判断的点的坐标
x, y, z = (0.5, 5, 0.5)
# 判断点是否在长方体范围内
if xmin <= x <= xmax and ymin <= y <= ymax and zmin <= z <= zmax:
print("The point is inside the box.")
else:
print("The point is outside the box.")
# 画出长方体边框和点
ls = LineSegs()
ls.moveTo(xmin, ymin, zmin)
ls.drawTo(xmax, ymin, zmin)
ls.drawTo(xmax, ymax, zmin)
ls.drawTo(xmin, ymax, zmin)
ls.drawTo(xmin, ymin, zmin)
ls.moveTo(xmin, ymin, zmax)
ls.drawTo(xmax, ymin, zmax)
ls.drawTo(xmax, ymax, zmax)
ls.drawTo(xmin, ymax, zmax)
ls.drawTo(xmin, ymin, zmax)
ls.moveTo(xmin, ymin, zmin)
ls.drawTo(xmin, ymin, zmax)
ls.moveTo(xmax, ymin, zmin)
ls.drawTo(xmax, ymin, zmax)
ls.moveTo(xmax, ymax, zmin)
ls.drawTo(xmax, ymax, zmax)
ls.moveTo(xmin, ymax, zmin)
ls.drawTo(xmin, ymax, zmax)
node = ls.create()
self.render.attachNewNode(node)
# 在屏幕上显示长方体和点
point = loader.loadModel("models/misc/smiley")
point.setScale(0.1)
point.setPos(x, y, z)
point.reparentTo(self.render)
app = MyApp()
app.run()
这样就可以在屏幕上看到长方体和点了
原文地址: https://www.cveoy.top/t/topic/fJRC 著作权归作者所有。请勿转载和采集!