Python 我能用pyglet中的标准化坐标画图吗?
在pyglet图形文档中,它演示了如何使用屏幕坐标在二维空间中绘制点Python 我能用pyglet中的标准化坐标画图吗?,python,pyglet,Python,Pyglet,在pyglet图形文档中,它演示了如何使用屏幕坐标在二维空间中绘制点 pyglet.graphics.draw(2, pyglet.gl.GL_POINTS, ('v2i', (10, 15, 30, 35)) ) 我希望能够使用规范化坐标进行绘制,每个轴上的坐标范围为[-1,1],但通常使用draw调用,我必须根据像素坐标给出顶点 def __iter__(self, *args, **kwargs): if self.type == 'normalized':
pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
('v2i', (10, 15, 30, 35))
)
我希望能够使用规范化坐标进行绘制,每个轴上的坐标范围为[-1,1],但通常使用draw调用,我必须根据像素坐标给出顶点
def __iter__(self, *args, **kwargs):
if self.type == 'normalized':
return self.complicate()
else:
return self.cords
我想这样做的原因是,我将绘制域和范围[-1,1]的函数,因此使用规范化坐标绘制非常合适,不必担心转换。因此,假设我的数学是正确的(当然不是),这些函数应该会返回您想要的:
def normalize(cords, constraint):
x_ratio, y_ratio = 1/constraint.width, 1/constraint.height
result = []
for x,y in zip(*[iter(cords)] * 2):
nx = ((x-(constraint.width/2))*x_ratio)*2
ny = ((y-(constraint.height/2))*y_ratio)*2
result += [nx, ny]
print(x,y, '->', nx, ny)
return result
def complicate(cords, constraint):
x_ratio, y_ratio = 1/constraint.width, 1/constraint.height
result = []
for x,y in zip(*[iter(cords)] * 2):
nx = ((x/x_ratio)/2)+(constraint.width/2)
ny = ((y/y_ratio)/2)+(constraint.height/2)
result += [nx, ny]
print(x, y, '->', nx, ny)
return result
你可以这样使用它们:
pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
('v2i', normalize((10, 15, 30, 35), window))
)
或
您可能需要一种将两个世界存储在一个地方的通用方法。把它们当作一个单独的物体,不管你放进什么东西,都是有意义的。所以你可以把标准坐标和坐标视为同一个东西
你可以这样做:
class cordinates():
def __init__(self, cords, constraint=None):
if not constraint: constraint = window
self.constraint = constraint
if min(cords) > -1 and max(cords) < 1:
self.type = 'normalized'
else:
self.type = 'complicated'
self.cords = cords
def normalize(self, constraint=None):
if not constraint: constraint = self.constraint
x_ratio, y_ratio = 1/constraint.width, 1/constraint.height
result = []
for x,y in zip(*[iter(self.cords)] * 2):
nx = ((x-(constraint.width/2))*x_ratio)*2
ny = ((y-(constraint.height/2))*y_ratio)*2
result += [nx, ny]
print(x,y, '->', nx, ny)
return result
def complicate(self, constraint=None):
if not constraint: constraint = self.constraint
x_ratio, y_ratio = 1/constraint.width, 1/constraint.height
result = []
for x,y in zip(*[iter(self.cords)] * 2):
nx = ((x/x_ratio)/2)+(constraint.width/2)
ny = ((y/y_ratio)/2)+(constraint.height/2)
result += [nx, ny]
print(x,y, '->', nx, ny)
return result
def __iter__(self, *args, **kwargs):
return self.cords
def __repr__(self, *args, **kwargs):
return str(self.cords)
pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
('v2i', coordinates((-0.975, -0.95, -0.925, -0.883)).complicate() )
)
pyglet.graphics.draw(2, pyglet.gl.GL_POINTS,
('v2i', coordinates((10, 15, 30, 35)) )
)
我通常只处理一种对象,所以我可能不是这方面最好的老师。但这对我来说很有意义,希望对你有用。祝你好运 我想你做不到。但是,您可以创建一个可以将值传递到的函数。上面的例子采用了,
x,y
和约束
,但是您可以很容易地调整它,以获取一个元组并以自己的方式完成它。感谢您提供了详细的python式答案。我在代码中实现了类似的功能,我调用from_window和to_window函数来转换规范化坐标和屏幕空间坐标之间的坐标
def __iter__(self, *args, **kwargs):
if self.type == 'normalized':
return self.complicate()
else:
return self.cords