Please solve using MATLAB
5. Confetti
In this question, a confetti figure as below must be plotted. All coordinates in this question can be assumed to be in the range [0, 1] and [0, 1].
0.2
0.4
i) Create a function getMinDistance with two input variables:
- A matrix xs of size [N x 2], indicating the coordinates of N points (x- and y-coordinates in the first and second column respectively).
- A vector x of size [1 x 2], indicating the coordinates of a single point.
And one output variable:
- The distance between point x and the nearest point in matrix xs.
Note: If you do not manage to solve this question, you may assign the result of rand(1,1) to the output variable in order to proceed with the following question.
ii) Create a function confetti with two input variables:
- A radius r of all confetti particles.
- The number of confetti particles N.
And one output variable:
- A matrix of size [N x 2] with the coordinates of N confetti particles.
Generate random positions for the particles, but make sure that each confetti particle has a distance of at least 2r to all other particles.
Hint: Use a while loop to generate one particle at a time, and use the getMinDistance function to check the minimum distance between each new particle and all particles in the set.
Note: If you do not manage to solve this question, you may assign the result of rand(N,2) to the output variable in order to proceed with the following question.
iii) Evaluate the confetti function and plot all particles such as in the figure above. Each particle can be plotted as a line with:
- x = e + r*cos(theta)
- y = e + r*sin(theta)
with (e, e) being the center coordinates of each particle, r being the radius, and theta being a vector with linearly spaced values in the range from 0 to 2*pi.
In the figure above, the output of confetti is plotted with r = 0.02 and N = 300.