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
Image of page 8
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
Image of page 9
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
Image of page 10
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]
Image of page 11
We Now Showcase the Use of Lists of Objects Example 1. A Disk Intersection Problem Example 2. A Census Data Problem
Image of page 12
A Disk Intersection Problem
Image of page 13
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
Image of page 14
When Does a Pair of Disks Intersect? Answer: When the distance between their centers is less than the sum of their radii.
Image of page 15
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
Image of page 16
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.
Image of page 17
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
Image of page 18
Using outsideAll
Image of page 19
Image of page 20

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

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture