如何在Python / PsychoPy中绘制直角三角形?

时间:2018-08-01 13:46:49

标签: python polygon psychopy

我想在Psychopy中绘制一个灰色覆盖区域的直角三角形,但我只能得到一个等边三角形。它应该覆盖正方形的一半。蓝线表示关注的框架。

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

#Designed for PsychoPy v1.85.6

from __future__ import unicode_literals
from psychopy import visual, core, event, gui, data
import sys  # to get file system encoding

reload(sys)  

sys.setdefaultencoding('utf8')

win = visual.Window(fullscr=True, color= '#FFFFFF', monitor ="WorkingSpace", units="deg")

border = visual.Rect(win, width=20,height=20, lineColor='black', lineWidth=5)

diag = visual.Polygon(win, edges=3, radius=11.5, fillColor='#E6E6E6',pos=[-4.2,0], ori =90)

line1 = visual.Line(win, start=(10, -10), end=(-10,10), lineColor="blue", lineWidth=10)

line2 = visual.Line(win, start=(-10, -10), end=(-10,10), lineColor="blue", lineWidth=10)

line3 = visual.Line(win, start=(10, -10), end=(-10,-10), lineColor="blue", lineWidth=10)

border.draw()

diag.draw()

line1.draw()

line2.draw()

line3.draw()

win.flip()

event.waitKeys(keyList=['space']) #press space to continue

win.close()

我正在使用PsychoPy2 v1.85.6

1 个答案:

答案 0 :(得分:2)

使用visual.ShapeStim怎么样,它使您可以从顶点列表中定义任意形状?这是示例的修改版本,直角三角形叠加为绿色。

from psychopy import visual, event

# (switched to pixels to avoid monitor setup, note that sizes of things may be
#  slightly different)
win = visual.Window(fullscr=True, color='#FFFFFF', units="pix")

verts = [(100, -100), (-100, 100), (-100, -100)]
right_tri = visual.ShapeStim(win, fillColor='green',
                             vertices=verts, lineColor='green',
                             opacity=0.5)

border = visual.Rect(win, width=200, height=200,
                     lineColor='black', lineWidth=5)

line1 = visual.Line(win, start=(100, -100), end=(-100, 100),
                    lineColor="blue", lineWidth=10)

line2 = visual.Line(win, start=(-100, -100), end=(-100, 100),
                    lineColor="blue", lineWidth=10)

line3 = visual.Line(win, start=(100, -100), end=(-100, -100),
                    lineColor="blue", lineWidth=10)

border.draw()

line1.draw()
line2.draw()
line3.draw()

right_tri.draw()

win.flip()
# save a screenshot
win.getMovieFrame()
win.saveMovieFrames('screenshot.png')
event.waitKeys(keyList=['space'])  # press space to continue

win.close()

结果: enter image description here