Nullable value based vector : nullable « Data Type « C# / CSharp Tutorial

Home
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.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Data Type » nullable 
2.58.12.Nullable value based vector
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


    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)
        {
            Vector v1 = new Vector(12);
            Vector v2 = new Vector(13);
            Console.WriteLine("{0} + {1} = {2}", v1, v2, v1 + v2);
            Console.WriteLine("{0} - {1} = {2}", v1, v2, v1 - v2);
        }
    }
2.58.nullable
2.58.1.Box and unbox for nullable value
2.58.2.Boxing And Unboxing for nullable type
2.58.3.Age Calculation with nullable death date
2.58.4.Convert nullable value to string
2.58.5.Does Nullable value has value
2.58.6.Function for getting Nullable double value
2.58.7.Get hash code for nullable value
2.58.8.Get value or default value for nullable value
2.58.9.Nullable Class Members
2.58.10.Nullable Try Parse
2.58.11.Nullable integer Demo
2.58.12.Nullable value based vector
2.58.13.Partial Comparer for nullable value
2.58.14.Provide default value for nullable value
2.58.15.Reference Compare for nullable value
2.58.16.Using the Nullable Modifier
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.