CS 100: Section Assignment S5
Solutions
import java.io.*; public class S5 { public static void main(String args[]) { TokenReader in = new TokenReader(System.in); // Problem 1. Note the use of the "extra" boolean variables to // hold important boolean subresults. Disk D1 = new Disk(1,2,3); Disk D2 = new Disk(5,0,1); Disk D3 = new Disk(-5,1,2); // Part (a) boolean D1Big = D1.contain(D2) && D1.contain(D3); boolean D2Big = D2.contain(D1) && D2.contain(D3); boolean D3Big = D3.contain(D1) && D3.contain(D2); boolean B1 = D1Big || D2Big || D3Big; // Part (b) boolean D1andD2_Apart = D1.sep(D2) > 0; boolean D2andD3_Apart = D2.sep(D3) > 0; boolean D1andD3_Apart = D1.sep(D3) > 0; boolean B2 = D1andD2_Apart && D2andD3_Apart && D1andD3_Apart; // Problem 2. Lets see if the following disk sits in a single quadrant: Disk D = new Disk(1,4,3); System.out.print("\nD: "); D.println(); if (D.inQuadrant()) {System.out.println("D is in a quadrant.");} else {System.out.println("D is not in a quadrant.");} // Problem 3. System.out.println("\n\nThese disks intersect the unit disk with center (0,0):\n"); Disk U = new Disk(0,0,1); // Unit disk at the origin. Disk rD; // Will reference the current randomly generated disk. int count=0; // Number of disks that intersect U. while (count<5) { rD = Disk.random(-10,10,-10,10,3); // Generate a new random disk. if (U.sep(rD) == 0) { // Have generated an intersecting disk. count++; rD.println(); } } // Problem 4 Disk F1 = new Disk(-3,2,1); double a = F1.get_xc(); double b = F1.get_yc(); double rho = F1.get_r(); Disk F2 = new Disk(-a,-b,rho); System.out.print("\nF1: "); F1.println(); System.out.print("F2: "); F2.println(); System.out.println("\nThe separation between F1 and F2 = " + F1.sep(F2)); // in.waitUntilEnter(); } } /* Sample Output: D: xc = 1.0 yc = 4.0 r = 3.0 D is not in a quadrant. These disks intersect the unit disk with center (0,0): xc = -2.225729417210511 yc = 0.2307854683707955 r = 2.133714168544869 xc = 2.3505891864903194 yc = -1.5187660085422454 r = 2.8770267346545095 xc = -1.4227595571417062 yc = 0.06258182091714737 r = 0.7235474442701275 xc = -1.4968708397021242 yc = 2.8514172777021862 r = 2.5179259208687395 xc = 2.081521428190973 yc = 1.6302856894069766 r = 1.975274184109415 F1: xc = -3.0 yc = 2.0 r = 1.0 F2: xc = 3.0 yc = -2.0 r = 1.0 The separation between F1 and F2 = 5.211102550927978 */
public class Disk // An instance of this class is a disk with center (xc,yc) and radius r. { private double xc; private double yc; private double r; // Constructor public Disk(double xcVal, double ycVal, double rVal) { xc = xcVal; yc = ycVal; r = rVal; } // Yields the center-to-center distance between this disk and q. public double dist(Disk q) { double delx = xc-q.xc; double dely = yc-q.yc; double d = Math.sqrt(delx*delx + dely*dely); return d; } // Yields the area of this disk. public double area() { return Math.PI*r*r; } // Pretty prints the parameters of this disk. public void println() { System.out.println("xc = " + xc + " yc = " + yc + " r = " + r); } // Yields the minimum distance between a point in this disk and a point in q. public double sep(Disk q) { double c2c; // The center-to-center distance c2c = Math.sqrt((xc-q.xc)*(xc-q.xc)+(yc-q.yc)*(yc-q.yc)); // Equivalent to c2c = this.dist(q); // and also to c2c = dist(q); if (c2c<r+q.r) {return 0;} else {return c2c - r - q.r;} } // Yields true if every point in q is in this disk. public boolean contain(Disk q) { double d = this.dist(q); return (d+q.r<=r); } // Yields a disk whose center is randomly selected from the set // {(x,y) | x1<=x<x2, y1<=y<y2} and whose radius is randomly selected from // the interval [0,rMax). public static Disk random(double x1, double x2, double y1, double y2, double rMax) { double a = x1 + Math.random()*(x2-x1); double b = y1 + Math.random()*(y2-y1); double rho = Math.random()*rMax; Disk D = new Disk(a,b,rho); return D; } // Resets the center of this disk to (x0,y0). public void move(double x0, double y0) { xc = x0; yc = y0; } // Resets the radius of this disk to r0. public void scale(double r0) { r = r0; } // Yields the x-coordinate of this disk's center. public double get_xc() { return xc; } // Yields the y-coordinate of this disk's center. public double get_yc() { return yc; } // Yields the radius of this disk. public double get_r() { return r; } // Yields true if this disk does not intersect the x or y axis. public boolean inQuadrant() { boolean NoTouch_yaxis = Math.abs(xc) > r; boolean NoTouch_xaxis = Math.abs(yc) > r; return (NoTouch_yaxis && NoTouch_xaxis); } }