Python: Use object-oriented programming and Numpy techniques to create simple graphics with bullseyes

Online tutoring services

Need help with this question or any other academic project? Click on the button below to to hire an expert cheap.

Create a class Canvas, and implement two methods. The __init__() method should take two arguments other than self, background and n. The background is expected to be a 1d Numpy array of length 3, representing an RGB color. For example, black = np.array([0,0,0]) and purple = np.array([0.5, 0.5, 0]). At this stage, the __init__ method should create an instance variable self.im, a Numpy array of shape (n, n, 3). This array should be constructed so that self.im[i,j] == background for each value of i and j. The show() method should take no arguments (except for self), and simply display self.im using the plt.imshow() function. Modifying your class above (that is, not copy/pasting code), implement a method called add_disk(centroid, radius, color), which draws a colored disk with specified radius, centered at centroid, of the specified color. centroid may be assumed to be a tuple, list, or Numpy array of the form (x,y), where x gives the horizontal coordinate of the disk's center and y gives the vertical coordinate. All points within distance radius of the centroid should be filled in with color. Modify your code from Part B (still no copy/paste), write a method called add_bullseye(centroid, radius, color1, color2, bandwidth). This method should create a bullseye pattern consisting of concentric circles with alternating colors. Each circle should have thickness equal to bandwidth, and the radius of the entire pattern should be equal to radius. Write a further demonstration of the correct functioning of your code by creating a new Canvas with at least three bullseyes drawn on it. You should demonstrate: At least three (3) different centroids. At least four (4) different colors. At least three (3) different values of the bandwidth parameter.

Get Help With a similar task to - Python: Use object-oriented programming and Numpy techniques to create simple graphics with bullseyes

Login to view and/or buy answers.. or post an answer
Additional Instructions:

6/8/2021 Homework_P2 localhost:8889/nbconvert/html/Downloads/Homework_P2.ipynb?download=false 1/6 Homework, Problem 2 (45 points) Introduction In this problem, you'll use object-oriented programming and Numpy techniques to create simple graphics with bullseyes, like this one: Example output from this problem. A Note on Loops There is precisely one place in this entire problem in which a loop (such as a for -loop, a while - loop, or a list comprehension) is completely appropriate. This place is Part D. I'll tell you when we get there. With that one exception, you should avoid the use of loops whenever possible. Solutions that use loops elsewhere will receive partial credit. Concision and Style Remember, concision and style are two of the criteria on which we assess your solutions. You should aim to write code that is as short, simple, and readable as possible. My own solution for this problem requires 21 lines excluding comments. Longer solutions are ok, as long as they don't perform redundant computation and appropriately make use of Numpy operations. Comments and Docstrings Comments and docstrings are not required in any part of this problem. However, they may help us give you partial credit, so they are recommended unless you're feeling very confident. 6/8/2021 Homework_P2 localhost:8889/nbconvert/html/Downloads/Homework_P2.ipynb?download=false 2/6 Input Checking It is not necessary to perform any input checking in this problem -- you can assume that the user will supply inputs with the correct data types, shapes, etc. Image Appearance It's ok for your images to look a little different from mine. For example, your image might appear slightly jagged or blocky, depending on the size of your background Canvas . That's ok! As long as your image clearly demonstrates correct code, you'll receive full credit. Part A You don't have to write any code here, just run the block below. You did it! Part B (10 points) Create a class Canvas , and implement two methods. The __init__() method should take two arguments other than self , background and n . The background is expected to be a 1d Numpy array of length 3, representing an RGB color. For example, black = np.array([0,0,0]) and purple = np.array([0.5, 0.5, 0]) . At this stage, the __init__ method should create an instance variable self.im , a Numpy array of shape (n, n, 3) . This array should be constructed so that self.im[i,j] == background for each value of i and j . The show() method should take no arguments (except for self ), and simply display self.im using the plt.imshow() function. For example, the code purple = np.array([0.5, 0.0, 0.5]) C = Canvas(purple, 2001) # 2001 x 2001 pixels C.show() should display a purple square, like this one: In [ ]: # run this to get started import numpy as np from matplotlib import pyplot as plt 6/8/2021 Homework_P2 localhost:8889/nbconvert/html/Downloads/Homework_P2.ipynb?download=false 3/6 Example output. Make sure to run the test block after your class definition in order to demonstrate that your code works. Notes You can use ax.axis("off") to remove the axis ticks and borders, although that's not required for this problem. For me, the easiest way to create self.im was to create an array of appropriate dimensions using np.zeros() , and then populate it using array broadcasting. This problem can be solved using a loop for partial credit. Part C (15 points) Modifying your class above (that is, not copy/pasting code), implement a method called add_disk(centroid, radius, color) , which draws a colored disk with specified radius , centered at centroid , of the specified color. centroid may be assumed to be a tuple, list, or Numpy array of the form (x,y) , where x gives the horizontal coordinate of the disk's center and y gives the vertical coordinate. All points within distance radius of the centroid should be filled in with color . For example, the code In [ ]: # define your Canvas class here # solutions to Parts B, C, and D should all be in this cell In [ ]: # test code: run but do not modify purple = np.array([0.5, 0, 0.5]) C = Canvas(purple, 2001) # 2001 x 2001 pixels C.show() 6/8/2021 Homework_P2 localhost:8889/nbconvert/html/Downloads/Homework_P2.ipynb?download=false 4/6 purple = np.array([.5, 0, .5]) white = np.array([1, 1, 1]) C = Canvas(background = purple, n = 2001) C.add_disk((1001, 1001), 500, white) C.show() should produce the following image: Example output. Run the test code supplied below to demonstrate that your code is working. Math Note Recall that the (open) disk of radius with centroid is the set of all points satisfying the formula Programming Notes The function np.meshgrid is a useful way to represent the horizontal and vertical coordinates. If you take this approach, you should ensure that this function is called only once even if your user calls self.add_disk() multiple times. This problem can be solved using a loop for partial credit. If you do take the loop-based approach for partial credit, you may need to reduce n , the size of the Canvas , in the examples below, as your code might be slower. r (x0, y0) (x, y) (x − x0) 2 + (y − y0) 2 < r2 . In [ ]: # test code: run but do not modify purple = np.array([.5, 0, .5]) white = np.array([1, 1, 1]) C = Canvas(background = purple, n = 2001) C.add_disk((1001, 1001), 500, white) C.show() 6/8/2021 Homework_P2 localhost:8889/nbconvert/html/Downloads/Homework_P2.ipynb?download=false 5/6 Part D (15 points) Modify your code from Part B (still no copy/paste), write a method called add_bullseye(centroid, radius, color1, color2, bandwidth) . This method should create a bullseye pattern consisting of concentric circles with alternating colors. Each circle should have thickness equal to bandwidth , and the radius of the entire pattern should be equal to radius . For example: purple = np.array([0.5, 0.0, 0.5]) white = np.array([1.0, 1.0, 1.0]) grey = np.array([0.2, 0.2, 0.2]) C = Canvas(background = grey, n = 2001) C.add_bullseye((1001, 1001), 500, purple, white, bandwidth = 50) C.show() Example output. In this example, each of the bands is 50 pixels thick, and the entire pattern has radius 500. Loops In this part, it would be appropriate to write one for - or while -loop. Hint You might wish to create a new cell and run the following code -- it could help you catch on to the right idea. purple = np.array([0.5, 0.0, 0.5]) white = np.array([1.0, 1.0, 1.0]) grey = np.array([0.2, 0.2, 0.2]) C = Canvas(background = grey, n = 2001) C.add_disk((1001, 1001), 500, purple) 6/8/2021 Homework_P2 localhost:8889/nbconvert/html/Downloads/Homework_P2.ipynb?download=false 6/6 C.add_disk((1001, 1001), 450, white) C.show() Part E (5 points) Write a further demonstration of the correct functioning of your code by creating a new Canvas with at least three bullseyes drawn on it. You should demonstrate: At least three (3) different centroids. At least four (4) different colors. At least three (3) different values of the bandwidth parameter. You're also welcome to vary the radius parameter, but this isn't required. You're encouraged to be creative! Coordinate your colors, let your bullseyes partially intersect, etc. etc. But if you're not really feeling your artistic mojo today, it's ok to base your solution on the example shown at the very beginning of this problem, which satisfies all of the above criteria. I've predefined the colors I used for your convenience. Provided that you've solved up to Part D correctly, no further modifications to your Canvas class are required. In [ ]: # test code: run but do not modify purple = np.array([0.5, 0.0, 0.5]) white = np.array([1.0, 1.0, 1.0]) grey = np.array([0.2, 0.2, 0.2]) C = Canvas(background = grey, n = 2001) C.add_bullseye((1001, 1001), 500, purple, white, bandwidth = 50) C.show() In [ ]: # predefined colors -- feel free to add your faves! purple = np.array([.5, 0, .5]) white = np.array([1, 1, 1]) green = np.array([0, 1, 0]) blue = np.array([0, 0, 1]) orange = np.array([1, .4, 0]) yellow = np.array([1, 1, 0]) grey = np.array([.2,.2,.2]) # write your demonstration here # don't forget to show the image!

Related Questions

Similar orders to Python: Use object-oriented programming and Numpy techniques to create simple graphics with bullseyes
17
Views
0
Answers
Financial Analysis project for ETF Betas
This project is for my Financial Analysis class and is required to be done via Jupyter Notebooks. When the assignment is completed I need to submit the Jupyter Notebook document and use the data on their to present via a powerpoint. The powerpoint I can do...
47
Views
0
Answers
Python programming with ordinary differential equations
You need to know math at university level to understand the ordinary differential equations. The full task description is in the provided file. I need a python code with useful comments and the required figures. Then I will write the report myself....
48
Views
0
Answers
Develop a Python script to simulate key models of robot navigation using object oriented programming.
Develop a Python script to simulate key models of robot navigation using object oriented programming. Then, work through some written problems of robot navigation based on your studies of simulation and computer vision...
35
Views
0
Answers
Finance Program with two loops
Pull data from a .csv. Randomly select two given a parameter. Find the moving average that provides the highest return then save that as the best return along with the best moving average (from 0 to 100 in increments of 5). Run this 100 times to find the b...