After Operations on a List of Points L1 Point55 L 3 x 4 y Point 1 x 2 y Point 9

# After operations on a list of points l1 point55 l 3 x

This preview shows page 8 - 20 out of 32 pages.

After
Operations on a List of Points >>> L[1] = Point(5,5) L: 3 x 4 y Point 1 x 2 y Point 9 x 3 y Point Before
Operations on a List of Points >>> L[1] = Point(5,5) L: 3 x 4 y Point 5 x 5 y Point 9 x 3 y Point After
Printing a List of Points def printCloud(A): """ Prints the points in A PreC : A is a list of points. """ for a in A: print a Synonym for the loop: for k in range(len(A)): print A[k]
We Now Showcase the Use of Lists of Objects Example 1. A Disk Intersection Problem Example 2. A Census Data Problem
A Disk Intersection Problem
A Class for Representing Disks def class Disk(object): """ Attributes: center: Point, the center of the disk radius: float, the radius of the disk """ def __init__(self,P,r): """ Creates a Disk object with center P and radius r PreC: P is a Point,r is a pos float """ self.center = P self.radius = r Note that an attribute can be an object. The center attribute is a Point
When Does a Pair of Disks Intersect? Answer: When the distance between their centers is less than the sum of their radii.
The Method Intersects def Intersects(self,other): """ Returns True if self and other intersect and False otherwise. PreC: self and other are Disk objects """ # The center-to-center distance: c1 = self.center c2 = other.center d = c1.Dist(c2) # The sum of the two radii radiusSum = self.radius + other.radius TheyIntersect = (radiusSum >= d ) return TheyIntersect
An Intersection Problem We have a 10-by-10 target for k in range(100): Generate a random disk D Display D if it does not touch any of the previously displayed disks Assume all the disks have radius 1 and all inside the target.
A Critical Function def outsideAll(D0,L): """ Returns True if D0 doesn't intersect any of the disks in L PreC: D0 is a Disk and L is a list of Disks """ for D in L: if D.Intersects(D0): return False return True
Using outsideAll

#### You've reached the end of your free preview.

Want to read all 32 pages?

• Fall '19
• Orange County, California, PREC, San Bernardino County, California, Riverside County, California