// CS U213 Spring 2007
// Lecture 5: January 18, 2007

//Introduction to Methods
//-----------------------

//Lets try to define some basic shapes

/*
                 +-------+                
                 | Shape |                
                 +-------+                
                 +-------+                
                     |                    
                    / \                   
                    ---                   
                     |                    
           ---------------------          
           |                   |          
    +-------------+    +---------------+  
    | Square      |    | Circle        |  
    +-------------+    +---------------+  
 +--| CartPt nw   |    | CartPt center |-+
 |  | double size |    | double radius | | 
 |  +-------------+    +---------------+ |
 |                                       |
 +---------------------------------------+                                      
 |
 |  +--------+
 +->| CartPt |
    +--------+
    | int x  |
    | int y  |
    +--------+

*/

// represents a cartesian point
//class CartPt {
//  int x;
//  int y;
//
//  CartPt(int x, int y) {
//    this.x = x;
//    this.y = y;
//  }
//}

// represents a basic shape
interface Shape {
}

// represents a square
class Square implements Shape {
  CartPt nw;
  double size;

  Square(CartPt nw, double size) {
    this.nw = nw;
    this.size = size;
  }
}

// represents a circle
//class Circle implements Shape {
// CartPt center;
// double radius;
//
//  Circle(CartPt center, double radius) {
//    this.center = center;
//    this.radius = radius;
//  }
//}


//There is some useful information we would like to know about a circle or a square:
//  --what is its area?
//  --is it touching the top?
//  --is it within bounds?
//  --is a given point within it?
//
//In order to obtain such information we use methods!


//Lets try to compute the area of a circle

/*

;;In Scheme
;;---------

(define-struct mycircle (center radius))
;;A circle is make-circle(Posn Number) 

(define c1 (make-mycircle (make-posn 0 0) 1))
(define c2 (make-mycircle (make-posn 4 3) 10))

;;to compute the area of a circle
;;area: MyCircle -> Number
;;(define (area a-circle)
;; ...(mycircle-center a-circle)...
;; ...(mycircle-radius a-circle)...)
(define (area a-circle)
  (* (mycircle-radius a-circle)(mycircle-radius a-circle) pi))

;;Examples:
;;---------

(equal? (area c1) pi)


*/


//In Java
//-------


//We extend the Class Circle by adding a method!

// represents a circle
//class Circle implements Shape {
//  CartPt center;
//  double radius;
//
//  Circle(CartPt center, double radius) {
//    this.center = center;
//    this.radius = radius;
//  }

 //calculates the area of this circle
//  double area () {
//   /* ...this.center...
//      ...this.radius... */
//  return Math.PI*this.radius*this.radius;} 
//
//}

//Examples:
//---------
//
//class Examples {
// 
//  Examples () {}
//
//  Circle c1=new Circle(new CartPt(0,0),1);
//  double area=this.c1.area();
//
//}

//Lets try to define a function that computes if a circle contains a given point

/*

;;In Scheme
;;---------

(define-struct mycircle (center radius))
;;A circle is make-circle(Posn Number) 

(define c1 (make-mycircle (make-posn 0 0) 1))
(define c2 (make-mycircle (make-posn 4 3) 10))

;;does a circle contain a point?
;;contains: MyCircle Posn -> Boolean
;;(define (contains cir p)
;; ...(mycircle-center cir)...
;; ...(mycircle-radius cir)...
;; ...(posn-x p)...
;; ...(posn-y p)...)
(define (contains cir p)
  (> (mycircle-radius cir)
     (sqrt 
       (+
         (* (-(posn-x (mycircle-center cir)) (posn-x p))(-(posn-x (mycircle-center cir)) (posn-x p)))
         (* (-(posn-y (mycircle-center cir)) (posn-y p))(-(posn-y (mycircle-center cir)) (posn-y p)))))))

;;Examples:
;;---------

(equal? (contains c1 (make-posn 0 0)) #t) 
(equal? (contains c2 (make-posn 4 16)) #f)


*/


//In Java
//-------


//We extend the Class Circle by adding a method!
//But we need to be careful: 
//We need to calculate the distance between the center of the circle and the given point
//However, our method should not change when the representation of CartPt changes!
//So the Class CartPt should deal with the calculation of the distance between to CartPts. 
//Thus we also extend the Class CartPt by adding a method! 

// represents a cartesian point
class CartPt {
  int x;
  int y;

  CartPt(int x, int y) {
    this.x = x;
    this.y = y;
  }

  //compute the distance between this point and a given point
  double distTo(CartPt that){
   /* ...this.x...
      ...this.y...
      ...that.x...
      ...that.y... */
  return Math.sqrt((this.x-that.x)*(this.x-that.x)
                  +(this.y-that.y)*(this.y-that.y)); 
 }

}

// represents a circle
class Circle implements Shape {
  CartPt center;
  double radius;

  Circle(CartPt center, double radius) {
    this.center = center;
    this.radius = radius;
  }

 //calculates the area of this circle 
  double area () {
   /* ...this.center...
      ...this.radius... */
  return Math.PI*this.radius*this.radius;} 

 //does this circle contain the given point?
 boolean contains(CartPt pt) {
   /* ...this.center...
      ...this.radius... 
      ...this.center.disTo(Cart Pt)*/
  return this.center.distTo(pt)<this.radius;
 }

}

//Examples:
//---------

class Examples {

  Examples () {}

  Circle c1=new Circle(new CartPt(0,0),1);
  Circle c2=new Circle(new CartPt(4,3),10);
  CartPt p1=new CartPt(0,0);
  CartPt p2=new CartPt(4,16);
  double area1=this.c1.area();
  double area2=this.c2.area();
  boolean isInc1=this.c1.contains(this.p1);
  boolean isInc2=this.c2.contains(this.p2);

}