Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

** Write a program very similar to makePoly.py, and call it makePath.py, with a function pathHere. The only outward difference between polyHere and pathHere is

** Write a program very similar to makePoly.py, and call it makePath.py, with a function pathHere. The only outward difference between polyHere and pathHere is that while the first creates a closed polygon, and returns it, and the new one creates a polygonal path, without the final point being automatically connected to the first point, and a list of the lines in the path is returned. Internally the functions are quite different. The change simplifies some things: no need to undraw anything in the main loop - just draw the latest segment each time going from the previous point to the just clicked point. There is a complication however, you do need deal specially with the first point. It has no previous point to connect to. I suggest you handle this before the main loop, and draw the point so it is a visible guide for the next point. After your main loop is finished undraw this initial point. (The place on the screen will still be visible if an initial segment is drawn. If no more points were added, the screen is left blank, which is the way it should be.) You also need to remember the previous point each time through the main loop.

In your main program, test the makePath function several times. Use the list of lines returned to loop and change the color in one path and the width of the lines in another path. A portion of a sample image is shown below after all this is done.

makePoly.py:

from graphics import *

def isBetween(x, end1, end2): '''Return True if x is between the ends or equal to either. The ends do not need to be in increasing order.''' return end1 <= x <= end2 or end2 <= x <= end1

def isInside(point, rect): '''Return True if the point is inside the Rectangle rect.''' pt1 = rect.getP1() pt2 = rect.getP2() return isBetween(point.getX(), pt1.getX(), pt2.getX()) and \ isBetween(point.getY(), pt1.getY(), pt2.getY())

def polyHere(rect, win): ''' Draw a polygon interactively in Rectangle rect, in GraphWin win. Collect mouse clicks inside rect into the vertices of a Polygon, and always draw the Polygon created so far. When a click goes outside rect, stop and return the final polygon. The Polygon ends up drawn. The method draws and undraws rect. ''' rect.setOutline("red") rect.draw(win) vertices = list() pt = win.getMouse() while isInside(pt, rect): vertices.append(pt) poly = Polygon(vertices) poly.draw(win) pt = win.getMouse() poly.undraw() poly.draw(win) rect.undraw() return poly

def main(): win = GraphWin('Drawing Polygons', 400, 400) win.yUp() instructions = Text(Point(win.getWidth()/2, 30), "Click vertices inside the red rectangle."+ " Click outside the rectangle to stop.") instructions.draw(win)

rect1 = Rectangle(Point(5, 55), Point(200, 120)) poly1 = polyHere(rect1, win) poly1.setFill('green') rect2 = Rectangle(Point(210, 50), Point(350, 350)) poly2 = polyHere(rect2, win) poly2.setOutline('orange')

win.promptClose(instructions)

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions