Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.

Get a Professional Answer

Via email, text message, or notification as you wait on our site. Ask follow up questions if you need to.

100% Satisfaction Guarantee

Rate the answer you receive.

Ask Scott Your Own Question

Scott, MIT Graduate

Category: Homework

Satisfied Customers: 3040

Experience: MIT Graduate (Math, Programming, Science, and Music)

3546829

Type Your Homework Question Here...

Scott is online now

Write a function that draws a straight line in the image

Customer Question

Write a function that draws a straight line in the image represented by the object of class Image (from the module 'images').The function signature should be:drawLine(image, x0, y0, x1, y1, color)After calling this function, a line from point at coordinate (x0,y0) to the point at coordinate (x1,y1) should be drawn in the specified color (the 'color' parameter is a tuple of 3 RGB components). Since the objects of class Image allow manipulation only at the single pixel level, you will have to individually add all the pixels that comprise the required line segment. You may need a bit of elementary math for that: a line is described in general by y = a*x + b. In our case it is convenient to re-parametrize it as: y = y0 + a*(x-x0). You can easily see that when x=x0, the corresponding value of y becomes y0, so such line automatically passes through one of the end points, (x0,y0). In order for the line to pass through the second end point, we need to have at x=x1: a*(x1-x0)+y0 = y1, hence a = (y1-y0) / (x1-x0) (note that this algorithm will break for strictly vertical lines when x0=x1, it's OK). Now the line is fully parametrized, all that's left for you is to draw it. You should iterate over the values of x, compute the corresponding values of y and set pixels of the specified color at each such (x,y).Verify that your function works: create a new empty image using the constructor of class Image() with width/height arguments. Draw a line or two of your choosing into that image and visualize the result (by using draw() method of an image object). Note that your lines will look good only if they close to horizontal ones.

After going through my resources, I do not have what is necessary to complete at this time.I am opting out and opening up to the other professionals and messaging a couple who might be able to help. Someone should be with you shortly.