简体   繁体   English

心理学:同时创建多个线对象

[英]Psychopy: Creating multiple line objects simultaneously

My code here works, but my current method is very inefficient and time consuming. 我的代码在这里工作,但我目前的方法是非常低效和耗时的。 I'm generating 4 Cartesian coordinates and appending them to a list. 我正在生成4个笛卡尔坐标并将它们附加到列表中。 I'm then creating 4 Psychopy line objects (visual.Line) and assigning each object an x,y coordinate from my coordinates list (zdot_list). 然后我创建4个Psychopy线对象(visual.Line)并从我的坐标列表(zdot_list)中为每个对象分配x,y坐标。 Currently, I'm creating 4 lines objects one after the other, and assigning an xy position to each 'start' parameter. 目前,我一个接一个地创建4个线对象,并为每个'start'参数指定xy位置。

from psychopy import visual, core, event, sound
import random
import math

win = visual.Window([800,600],color=(0,0,0), colorSpace='rgb', rgb=None, allowGUI=True, monitor='testMonitor', units='deg', fullscr=True, screen=2)

# input polar, output cartesian coords
def pol_to_cart(distance, angle, x_origin=0, y_origin=0):

    x=distance*math.cos(math.radians(angle))
    y=distance*math.sin(math.radians(angle))

    return x +x_origin, y + y_origin


zdots = 4
zdot_list = []
j=(-8)

# generate 4 xy coordinates and append to list
for i in range(zdots):

    angle=0

    line_cart = pol_to_cart(j, angle)
    dotx = line_cart[0]
    doty = line_cart[1]
    zdot_list.append([dotx, doty])

    j += .2

# generate 4 lines, add xy coordinate (from list^) to 'start' argument of each
linea = visual.Line(win, start=(zdot_list[0]), end=(4,0), lineColor="white")
lineb = visual.Line(win, start=(zdot_list[1]), end=(4,0), lineColor="white")
linec = visual.Line(win, start=(zdot_list[2]), end=(4,0), lineColor="white")
lined = visual.Line(win, start=(zdot_list[3]), end=(4,0), lineColor="white")

lines = [linea, lineb, linec, lined]

# display lines
for line in lines:
    line.draw()
    win.flip()
    core.wait(3)
    win.close()

Is there a more efficient way to create line (or any shape) objects using a loop? 有没有更有效的方法使用循环创建线(或任何形状)对象? I'd like to auto-generate the objects I need, adding my xy coordinate to each objects 'start' argument respectively. 我想自动生成我需要的对象,分别将xy坐标添加到每个对象的'start'参数中。 There are only 4 line objects in the image, but in reality I will need 80+, each with a different xy start coordinate. 图像中只有4个线对象,但实际上我需要80+,每个都有不同的xy开始坐标。

Cheers, Jon 干杯,乔恩

You can try and explore Polygon from visual module. 您可以尝试从可视模块中探索Polygon。 Sample of Polygon usage below 下面的Polygon用法示例

from psychopy import visual, core, event, sound
win = visual.Window([680,480],color=(0,0,0), colorSpace='rgb', rgb=None, allowGUI=True, monitor='testMonitor', units='deg', fullscr=False, screen=2)
pgon = visual.Polygon(win, edges=4)
pgon.draw()
win.flip()

You can explore vertices and other options by going through psychophy documentation. 您可以通过阅读psychophy文档来探索顶点和其他选项。

An optimisation to your code would be: 对代码的优化将是:

zdots=40
zdot_list = []

for i in range(zdots):
    angle=0
    line_cart = pol_to_cart(j, angle)
    zdot_list.append(visual.Line(win, start=([line_cart[0], line_cart[1]]), end=(4,0), lineColor="white"))
    j += .2

for i in zdot_list:
    i.draw()

win.flip()
core.wait(3)
win.close()

With 4 lines the suggested solution is fine. 有4行,建议的解决方案很好。 With more lines in the array you should use ElementArrayStim for more efficient drawing of large numbers of objects. 如果数组中有更多行,则应使用ElementArrayStim更有效地绘制大量对象。 This was originally designed for drawing bitmap textures, but if you set the size parameter to be long and thin, and set texture to nothing, then you effectively get a line. 这最初是为绘制位图纹理而设计的,但是如果将size参数设置为long和thin,并将纹理设置为空,那么您将有效地获得一条线。 See the code here for an example: 请参阅此处的代码以获取示例:

https://github.com/psychopy/psychopy/issues/1350 https://github.com/psychopy/psychopy/issues/1350

So, for scaling up to 80+ lines you should go that route. 因此,要扩展到80多行,您应该走这条路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM