Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Rectangle
- * <Description>
- * Creates a Rectangle object
- * <Usage>
- * javac Rectangle.java StadDraw.java
- *
- * API:
- * class Rectangle
- * public Rectangle(double x0, double y0, double w, double h)
- * public double area()
- * public double perimeter()
- * public boolean intersects(Rectangle b)
- * public void show(Rectangle b)
- *
- * <References>
- * http://introcs.cs.princeton.edu/java/stdlib/
- * UAB CIS201L-2014 Summer Dr. Sloan
- * @author
- * Dan Latham
- * @version 0.0.1
- *
- */
- public class Rectangle
- {
- private final double x;
- private final double y;
- private final double width;
- private final double height;
- public Rectangle(double x0, double y0, double w, double h)
- {
- x = x0;
- y = y0;
- width = w;
- height = h;
- }
- public double area()
- {
- return (double) width * height;
- }
- public double perimeter()
- {
- return (double) 2*width + 2*height;
- }
- public boolean intersects(Rectangle b)
- {
- //check if x and y points are the same
- if (x == b.x && y == b.y)
- {
- if (width == b.width || height == b.height) return true;
- else if (width > b.width && height < b.height) return true;
- else if (width < b.width && height > b.height) return true;
- }
- //check if rightside of rect.a.x goes past rect.b.x or at rect.b.x
- else if (x <= b.x) if (x+(width/2) >= b.x-(width/2)) return true;
- //opposite
- else if (x > b.x) if (b.x+(width/2) >= x-(width/2)) return true;
- //check if top of a.y goes past b.y
- else if (y <= b.y) if (y+(height/2) >= b.y-(height/2)) return true;
- //opposite
- else if (y > b.y) if (b.y+(height/2) >= y-(height/2)) return true;
- return false; //base
- }
- public void show(Rectangle b)
- {
- StdDraw.setPenRadius();
- StdDraw.setPenColor(StdDraw.BOOK_BLUE);
- StdDraw.rectangle(b.x,b.y,b.width/2,b.height/2);
- StdDraw.show(1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment