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