Three dimensional Vector with IFormattable : IFormattable « Class Interface « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » IFormattableScreenshots 
Three dimensional Vector with IFormattable
 
using System;
using System.Collections;
using System.Text;



class MainEntryPoint {
    static void Main(string[] args) {
        Vector Vect1 = new Vector(1.02.05.0);
        foreach (double Component in Vect1) {
            foreach (double Next in Vect1)
                Console.Write("  " + Next);
            Console.WriteLine(Component);
        }
        Console.ReadLine();
    }
}
struct Vector : IFormattable {
    public double x, y, z;

    public IEnumerator GetEnumerator() {
        return new VectorEnumerator(this);
    }

    public Vector(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public string ToString(string format, IFormatProvider formatProvider) {
        if (format == null)
            return ToString();
        string formatUpper = format.ToUpper();
        switch (formatUpper) {
            case "N":
                return "|| " + Norm().ToString() " ||";
            case "VE":
                return String.Format("( {0:E}, {1:E}, {2:E} )", x, y, z);
            case "IJK":
                StringBuilder sb = new StringBuilder(x.ToString()30);
                sb.Append(" i + ");
                sb.Append(y.ToString());
                sb.Append(" j + ");
                sb.Append(z.ToString());
                sb.Append(" k");
                return sb.ToString();
            default:
                return ToString();
        }
    }

    public Vector(Vector rhs) {
        x = rhs.x;
        y = rhs.y;
        z = rhs.z;
    }

    public override string ToString() {
        return "( " + x + " , " + y + " , " + z + " )";
    }

    public double this[uint i] {
        get {
            switch (i) {
                case 0:
                    return x;
                case 1:
                    return y;
                case 2:
                    return z;
                default:
                    throw new IndexOutOfRangeException(
                       "Attempt to retrieve Vector element" + i);
            }
        }
        set {
            switch (i) {
                case 0:
                    x = value;
                    break;
                case 1:
                    y = value;
                    break;
                case 2:
                    z = value;
                    break;
                default:
                    throw new IndexOutOfRangeException(
                       "Attempt to set Vector element" + i);
            }
        }
    }

    private const double Epsilon = 0.0000001;

    public static bool operator ==(Vector lhs, Vector rhs) {
        if (System.Math.Abs(lhs.x - rhs.x< Epsilon &&
           System.Math.Abs(lhs.y - rhs.y< Epsilon &&
           System.Math.Abs(lhs.z - rhs.z< Epsilon)
            return true;
        else
            return false;
    }

    public static bool operator !=(Vector lhs, Vector rhs) {
        return !(lhs == rhs);
    }

    public static Vector operator +(Vector lhs, Vector rhs) {
        Vector Result = new Vector(lhs);
        Result.x += rhs.x;
        Result.y += rhs.y;
        Result.z += rhs.z;
        return Result;
    }

    public static Vector operator *(double lhs, Vector rhs) {
        return new Vector(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
    }


    public static Vector operator *(Vector lhs, double rhs) {
        return rhs * lhs;
    }

    public static double operator *(Vector lhs, Vector rhs) {
        return lhs.x * rhs.x + lhs.y + rhs.y + lhs.z * rhs.z;
    }

    public double Norm() {
        return x * x + y * y + z * z;
    }

    private class VectorEnumerator : IEnumerator {
        Vector theVector;
        int location;

        public VectorEnumerator(Vector theVector) {
            this.theVector = theVector;
            location = -1;
        }

        public bool MoveNext() {
            ++location;
            return (location > 2false true;
        }

        public object Current {
            get {
                if (location < || location > 2)
                    throw new InvalidOperationException(
                       "The enumerator is either before the first element or " +
                       "after the last element of the Vector");
                return theVector[(uint)location];
            }
        }

        public void Reset() {
            location = -1;
        }
    }
}

 
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.