Search code examples
pythonpolygonpsychopy

How can I draw a right-angled triangle in Python/PsychoPy?


I want to draw a right-angled triangle with a grey covered area in Psychopy but I only get an equilateral triangle. It should cover the half of a square. The blue lines indicate the frame of interest.

#!/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()

I am using PsychoPy2 v1.85.6


Solution

  • How about with visual.ShapeStim, which lets you define an arbitrary shape from a list of vertices? Here's a modified version of your example, with the right triangle superimposed in green.

    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()
    

    Result: enter image description here