【发布时间】:2017-06-24 16:28:48
【问题描述】:
我的代码在这里有效,但我当前的方法效率很低且耗时。我正在生成 4 个笛卡尔坐标并将它们附加到列表中。然后我创建 4 个 Psychopy 线对象 (visual.Line) 并从我的坐标列表 (zdot_list) 中为每个对象分配一个 x,y 坐标。目前,我一个接一个地创建 4 个线对象,并为每个“开始”参数分配一个 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()
有没有更有效的方法来使用循环创建线(或任何形状)对象?我想自动生成我需要的对象,分别将我的 xy 坐标添加到每个对象的“开始”参数。图像中只有 4 个线对象,但实际上我需要 80 多个,每个对象都有不同的 xy 起始坐标。
干杯, 乔恩
【问题讨论】: