Vector extends List : List « Data Structure « C# / CSharp Tutorial

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / C Sharp
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » Data Structure » List 
11. 34. 5. Vector extends List
using System;
using System.Collections.Generic;
using System.Text;

public static class VectorDelegates {
    public static int Compare(Vector x, Vector y) {
        if (x.R > y.R) {
            return 1;
        else if (x.R < y.R) {
            return -1;
        }
        return 0;
    }

    public static bool TopRightQuadrant(Vector target) {
        if (target.Theta >= 0.0 && target.Theta <= 90.0) {
            return true;
        else {
            return false;
        }
    }
}

public class Vectors : List<Vector> {
    public Vectors() {
    }

    public Vectors(IEnumerable<Vector> initialItems) {
        foreach (Vector vector in initialItems) {
            Add(vector);
        }
    }

    public string Sum() {
        StringBuilder sb = new StringBuilder();
        Vector currentPoint = new Vector(0.00.0);
        sb.Append("origin");
        foreach (Vector vector in this) {
            sb.AppendFormat(" + {0}", vector);
            currentPoint += vector;
        }
        sb.AppendFormat(" = {0}", currentPoint);
        return sb.ToString();
    }
}

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

    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, newY180.0 / Math.PI;

            return new Vector(newR, newTheta);
        catch {
            return new Vector(null, null);
        }
    }

    public static Vector operator -(Vector op1) {
        return new Vector(-op1.R, op1.Theta);
    }

    public static Vector operator -(Vector op1, Vector op2) {
        return op1 + (-op2);
    }

    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 Program {
    static void Main(string[] args) {
        Vectors route = new Vectors();
        route.Add(new Vector(2.090.0));
        route.Add(new Vector(1.0180.0));
        route.Add(new Vector(0.545.0));
        route.Add(new Vector(2.5315.0));

        Console.WriteLine(route.Sum());

        Comparison<Vector> sorter = new Comparison<Vector>(VectorDelegates.Compare);
        route.Sort(sorter);
        Console.WriteLine(route.Sum());

        Predicate<Vector> searcher = new Predicate<Vector>(VectorDelegates.TopRightQuadrant);
        Vectors topRightQuadrantRoute = new Vectors(route.FindAll(searcher));
        Console.WriteLine(topRightQuadrantRoute.Sum());
    }
}
11. 34. List
11. 34. 1. Obtaining a read-only copy of a list
11. 34. 2. Using the Action delegate
11. 34. 3. Converting a list from list of string to list of int
11. 34. 4. Converting a list: user defined converting function
11. 34. 5. Vector extends List
w___ww___.__j___av___a___2__s__._com___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.