Familiarize yourself with the class example where we constructed nautical code flag "P."

Code flag "I" is a black circle in a yellow field (see example below). Write a MATLAB function to create and plot an I-flag. The function should have two inputs: the width of the flag (in pixels, e.g, 500) and the aspect ratio. The aspect ratio is the ratio of width-to-height. Your program should verify it is >=1. Your function will not have any return arguments, but will generate an image of the flag.

Your function should first figure out the number or rows and columns that will be in the image array. Use MATLAB rounding functions such as ceil or round. Preallocate the array and set all elements to 0.

Calculate the radius of the black circle, which should be about 1/3 of the height of the flag.

Figure out the [x,y] position of the center of the flag and store it in an array.

Next, loop over all elements in the flag array. For each element make a vector containing the element's [x,y] position. Use the distance formula to figure out how far the current element is from the center. You should be able to do this using element-by-element array operations--in other words, without referring to individual elements of the position or center vectors. You may find the MATLAB sqrt function useful. Also the sum function, which adds up the elements of a vector. If the current flag array element is within radius of the center, color it black. Otherwise color the element yellow.

Make a color map array with two colors: yellow (1,1,0) and black (0,0,0). Use imshow to display your flag. If you don't have imshow installed, you can instead use image: pass image the uint8 version of your flag array; then pass your color map array to the MATLAB colormap function.

Turn in the assignment by uploading the m-file to canvas. In addition, in the figure window, use "file->save as..." to save the image as a jpeg or tiff image. Also upload this file. To get full credit, your function must be appropriately commented and you must turn in both files.

egrhomework.PNG

110.7 KB

Get Higher Grades Now

Tutors Online