#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# How to randomly select 'target' rectangle from xy_circle

edited April 2018

For an experiment, I want create a canvas which shows rectangles around a central fixation dot. After showing this canvas to the participant, I want to show the canvas again, but in some cases with the 'target' rectangle in a different orientation. As you can see below, I have now used the xy_circle function to create the rectangles around the fixation dot. But I don't know how to randomly select a target, and change its orientation. Does anybody have any tips for me to do this? Or is it better to use a function other than xy_circle?
Thank you for your help!

``````my_canvas4 = canvas()
my_canvas4.fixdot()
for x, y in xy_circle(8, 200):
my_canvas4.rect(x-20, y-45, 40, 90, fill=True, color=u'red')
``````

• Hi Minne,

There are a few ways in which you could do this, but here's one: shuffle the order of the list of positions, and then just use the first position from the list as the target. This will make the target appear at a random location.

Cheers!
Sebastiaan

``````import random

# Create a list of circular position, and shuffle their order!
xy_list = xy_circle(8, 200)
random.shuffle(xy_list)

my_canvas4 = canvas()
my_canvas4.fixdot()
# Pop the target from the list
x, y = xy_list.pop()
my_canvas4.rect(x-45, y-20, 90, 40, fill=True, color=u'red')
# And draw the remaining as distractors
for x, y in xy_list:
my_canvas4.rect(x-20, y-45, 40, 90, fill=True, color=u'red')
my_canvas4.show()
``````

There's much bigger issues in the world, I know. But I first have to take care of the world I know.
cogsci.nl/smathot

• Hi Sebastiaan,

Thank you very much for your help.
If you don't mind I have another question. After I have implemented your suggestions, it seems the target rectangle is somehow always 90 degrees rotated. For that reason I want to rotate the target 270 degrees further to get it back in line with the other rectangles, but I'm not sure how to do this. It seems changing orientation is not one of the **style_args for the canvas.rect() function?
Your help is greatly appreciated!

Minne

• Hi Minne,

The target is drawn like this, with a width of 90 and a height of 40:

``````my_canvas4.rect(x-45, y-20, 90, 40, fill=True, color=u'red')
``````

Whereas the distractors are drawn like this, with a width of 40 and a height of 90:

``````my_canvas4.rect(x-20, y-45, 40, 90, fill=True, color=u'red')
``````

So there's no rotation as such, just different widths and heights, which I used so that you'd be able to tell the target apart from the distractors.

Cheers!
Sebastiaan

There's much bigger issues in the world, I know. But I first have to take care of the world I know.
cogsci.nl/smathot