Displaying shapes in random locations?

Hi everyone!
I am coding an experiment that requires to click the shapes on the screen with mouse clicks. However, I could not find a way to make shapes randomly located on the screen for each trial.
I use 'FillRect' or 'FillOval':
Screen('FillOval',windowPtr,[150 150 150],[? ? ? ?])
I would be very happy if you can help me out with that.
Thank you in advance.

2 Comments

This appears to relate to the third-party Psychtoolbox, which defines Screen()
Exactly. I can draw a shape on the screen by this function. Just need to randomize the location for each trial.

Sign in to comment.

Answers (2)

Instead of filling one oval, you can also specify a list of multiple ovals to be
filled - this is much faster when you need to draw many ovals per frame. To fill
n ovals, provide “rect” as a 4 rows by n columns matrix, each column specifying
one oval, e.g., rect(1,5)=left border of 5th oval, rect(2,5)=top border of 5th
oval, rect(3,5)=right border of 5th oval, rect(4,5)=bottom border of 5th oval
We can deduce from this that a rect is defined by [left, top, right, bottom] . So choose random positions such that the left border + desired width does not exceed the right boundary, and top border + desired height does not exceed the bottom boundary. randi() can help for that.
See also the tutorials at https://peterscarfe.com/ptbtutorials.html which include a few that use randomization.
But one question I would have is whether the random shapes need to avoid the locations of existing objects in the frame? Packing randomly positioned objects in a non-overlapping way into a constrained area can get difficult.
See my attached demo code that makes a bunch or randomly oriented shapes.

Asked:

on 27 Dec 2019

Answered:

on 28 Dec 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!