2.5 The Polygon ADTAs indicated earlier, there are many different types of containers. Most containers are designed for general purpose use, such as a list or dictionary. But a container can also be designed for a specific limited use. For example, suppose we need a container to store and manage a collection of vertices that define a geometric polygon. A polygon container would store and manage a collection of vertices in a specific order, which define the polygon. The operations needed to use and manage the collection can be limited to those needed to find, add, or remove vertices. In this section, we define and implement a complex abstract data type to represent a polygon. The Polygon ADT
The following simple program illustrates the basic use of the Polygon ADT. A three sided polygon is initially constructed with two additional vertices added, one is inserted and the other appended. The number of vertices is obtained and a search is performed for a given vertex. from polygon import Polygon poly = Polygon(0, 12, 13, 20, 27, 10) poly.insertVertex(3, 20, 0) poly.appendVertex(5, 0) print("The polygon contains", len(poly), "vertices.") if poly.findVertex(5, 20) >= 0 : print("The polygon has vertex (%d, %d)", (5, 20)) else : print("The polygon does not contain vertex (%d, %d)", (5, 20))
|