Lecture overview -- Keyboard shortcut: 'u'  Previous page: Privacy Leaks - Programs for exercise -- Keyboard shortcut: 'p'  Next page: Bank Account classes -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'       Help page about these notes  Course home    Extra Stuff - slide 17 : 25

Polygon Stuff
using System;

public class Polygon {

  private Point[] corners;
  
  public Polygon(params Point[] corners){
    this.corners = new Point[corners.Length];

    // Copy corners into instance variable:
    for(int i = 0; i < corners.Length; i++)
      this.corners[i] = new Point(corners[i]);
  }

  private Line[] Edges(){
    Line[] res = new Line[corners.Length];
    int Lgt = corners.Length;
    for (int i = 0; i < Lgt - 1; i++)
       res[i] = new Line(corners[i], corners[i+1]);
    res[Lgt-1] = new Line(corners[Lgt-1], corners[0]);
    return res;
  }

  public virtual double Circumference(){
    double res = 0;
    foreach(Line ln in this.Edges())
      res += ln.Length();
    return res; 
  }

  public virtual int Rank{
    get {
      return corners.Length;}
  }

}

internal class Line{

 private Point p1, p2;
 
 public Line(Point p1, Point p2){
   this.p1 = new Point(p1);
   this.p2 = new Point(p2);
 }

 public double Length(){
   return Math.Sqrt(
            Square(p1.X - p2.X) + 
            Square(p1.Y - p2.Y));
 }

 private static double Square(double a){
   return a * a;
 }
}    
// A versatile version with Rotation and internal methods
// for rectangular and polar coordinates. 

using System;

public class Point {

  private double r, a;            //  Data repr: radius, angle

  private Point(double r, double a){
     this.r = r; this.a = a;
  }

  // Copy constructor.
  public Point(Point p){
     this.r = p.r;
     this.a = p.a;
  }

  public static Point MakePolarPoint(double r, double a){
    return new Point(r, a);
  }

  public static Point MakeRectangularPoint(double x, double y){
    return new Point(RadiusGivenXy(x,y), AngleGivenXy(x,y));
  }

  public double X {
    get {return XGivenRadiusAngle(r,a);}
  }

  public double Y {
    get {return YGivenRadiusAngle(r,a);}
  }

  public double Radius {
    get {return r;}
  }

  public double Angle{
    get {return a;}
  }

  // The midpoint between p and this point
  public Point MidPoint(Point p){
    return MakeRectangularPoint(this.X + (p.X - this.X)/2,
                                this.Y + (p.Y - this.Y)/2);
  }

  // Return a new point which corresponds to this point 
  // rotated angle around p
  public Point RotateAroundPoint(Point p, double angle){
    Point pointToRotate = MakeRectangularPoint(this.X, this.Y);
    pointToRotate.Move(-p.X, -p.Y);
    pointToRotate.Rotate(angle);
    pointToRotate.Move(p.X, p.Y);
    return pointToRotate;
  }

  public void Move(double dx, double dy){
    double x, y;
    x = XGivenRadiusAngle(r,a);   y = YGivenRadiusAngle(r,a); 
    r = RadiusGivenXy(x+dx, y+dy);
    a = AngleGivenXy(x+dx, y+dy);
  }

  public void Rotate(double angle){
    a += angle;
  }

  public override string ToString(){
    return  "(" + XGivenRadiusAngle(r,a) + "," + YGivenRadiusAngle(r,a) + ")";
  }

  private static double RadiusGivenXy(double x, double y){
    return Math.Sqrt(x * x + y * y);
  }

  private static double AngleGivenXy(double x, double y){
    return Math.Atan2(y,x);
  }

  private static double XGivenRadiusAngle(double r, double a){
    return r * Math.Cos(a);
  }

  private static double YGivenRadiusAngle(double r, double a){
    return r * Math.Sin(a);
  }

  
}