Exercises in this lecture   Go to the notes, in which this exercise belongs -- Keyboard shortcut: 'u'   Alphabetic index   Course home   

Exercise solution:
Public data representation


The solution below is programmed with C# properties

// A versatile version with Rotation and internal methods
// for rectangular and polar coordinates. 

using System;

public class Point {

  public enum PointRepresentation {Polar, Rectangular}

  // public double x, y;   // previous public data repr.
  private double r, a;     // new, private data repr: radius, angle

  public Point(double x, double y){
     r = RadiusGivenXy(x,y);
     a = AngleGivenXy(x,y);
  }

  public Point(double par1, double par2, PointRepresentation pr){
   if (pr == PointRepresentation.Polar){
     r = par1; a = par2;
   } 
   else {
     r = RadiusGivenXy(par1,par2);
     a = AngleGivenXy(par1,par2);
   }
  }

  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;}
  }


  public void Move(double dx, double dy){
    double x, y;
    x = XGivenRadiusAngle(r,a);   y = XGivenRadiusAngle(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);
  }

  
}