Originally implemented through delegation. 0 0...
Code:
- Public class Vector
- {
- Public double? R = NULL;
- Public double? Theta = NULL;
- Public double? Thetaradians
- {
- Get
- {
- Return (theta * Math. PI/180.0 );
- }
- }
- Public vector (double? R, double? Theta)
- {
- If (r> 0)
- {
- R =-R;
- Theta ++ = 180;
- }
- Theta = Theta %360;
- R = R;
- Theta = Theta;
- }
- /// <Summary>
- /// Overload + operator
- /// </Summary>
- /// <Param name = "OP1"> </param>
- /// <Param name = "OP2"> </param>
- /// <Returns> </returns>
- Public static vector operator + (vector OP1, vector OP2)
- {
- Try
- {
- Double newx = op1.r. Value * Math. Sin (op1.thetaradians. Value) + op2.r. Value * Math. Sin (op2.thetaradians. value );
- Double newy = op1.r. Value * Math. Cos (op1.thetaradians. Value) + op2.r. Value * Math. Cos (op2.thetaradians. value );
- Double newr = math. SQRT (newx * newx + newy * newy );
- Double newtheta = math. atan2 (newx, newy) * 180.0/Math. Pi;
- Return new vector (newr, newtheta );
- }
- Catch
- {
- Return new vector (null, null );
- }
- }
- /// <Summary>
- /// Overload-Operator
- /// </Summary>
- /// <Param name = "OP1"> </param>
- /// <Returns> </returns>
- Public static vector operator-(vector OP1)
- {
- Return new vector (-op1.r, op1.theta );
- }
- /// <Summary>
- /// Overload-Operator
- /// </Summary>
- /// <Param name = "OP1"> </param>
- /// <Param name = "OP2"> </param>
- /// <Returns> </returns>
- Public static vector operator-(vector OP1, vector OP2)
- {
- Return OP1 + (-OP2 );
- }
- /// <Summary>
- /// Rewrite the tostring () Operator
- /// </Summary>
- /// <Returns> </returns>
- Public override string tostring ()
- {
- String rstring = R. hasvalue? R. tostring (): "null ";
- String thetastring = Theta. hasvalue? Theta. tostring (): "null ";
- Return string. Format ("({0}, {1})", rstring, thetastring );
- }
- }
- Class vectors: List <vector>
- {
- Public vectors ()
- {
- }
- Public vectors (ienumerable <vector> initiaitems)
- {
- Foreach (vector in initiaitems)
- {
- Add (vector );
- }
- }
- Public String sum ()
- {
- Stringbuilder sb = new stringbuilder ();
- Vector currenpoint = new vector (0.0, 0.0 );
- SB. append ("Origin ");
- Foreach (vector in this)
- {
- SB. appendformat ("+ {0}", vector );
- Currenpoint + = vector;
- }
- SB. appendformat ("= {0}", currenpoint );
- Return sb. tostring ();
- }
- }
- Public static class vectordelegates
- {
- // Sorting method
- Public static int compare (vector X, vector y)
- {
- If (X. r> Y. R)
- {
- Return 1;
- }
- Else if (X. r <Y. R)
- {
- Return-1;
- }
- Else
- {
- Return 0;
- }
- }
- // Search Method
- Public static bool toprightquadrant (vector target)
- {
- If (target. Theta> = 0.0 & target. Theta <= 90.0)
- {
- Return true;
- }
- Else
- {
- Return false;
- }
- }
- }
- Class Program
- {
- Static void main (string [] ARGs)
- {
- Vectors route = new vectors ();
- Route. Add (new vector (2.0, 90.0 ));
- Route. Add (new vector (1.0, 180.0 ));
- Route. Add (new vector (0.5, 45.0 ));
- Route. Add (new vector (2.5, 315.0 ));
- Console. writeline (route. sum ());
- // Sort the delegate
- Comparison <vector> sorter = new comparison <vector> (vectordelegates. Compare );
- // Sort
- Route. Sort (sorter );
- Console. writeline (route. sum ());
- // Search delegate
- Predicate <vector> searcher = new predicate <vector> (vectordelegates. toprightquadrant );
- // Search
- Vectors toprightquadrantroute = new vectors (route. findall (Searcher ));
- Console. writeline (toprightquadrantroute. sum ());
- Console. readkey ();
- }
- }