Simple, Quality, Awesome Software

3D Vectors

Below is the complete code for the 3D Vectors Try It Out! problem.

The Vector Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vectors
{
    /// <summary>
    /// Represents a vector in 3D space, including an x, y,
    /// and z component. This also implements a variety of
    /// operators, to make it easy to do math with it.
    /// </summary>
    class Vector
    {
        /// <summary>
        /// Gets or sets the x component of the vector.
        /// </summary>
        public double X { get; set; }

        /// <summary>
        /// Gets or sets the y component of the vector.
        /// </summary>
        public double Y { get; set; }

        /// <summary>
        /// Gets or sets the z component of the vector.
        /// </summary>
        public double Z { get; set; }

        /// <summary>
        /// Creates a new vector with specific x, y, and z
        /// components.
        /// </summary>
        public Vector(double x, double y, double z)
        {
            X = x;
            Y = y;
            Z = z;
        }

        /// <summary>
        /// Adds two vectors together.
        /// </summary>
        public static Vector operator +(Vector v1, Vector v2)
        {
            return new Vector(
                v1.X + v2.X,
                v1.Y + v2.Y,
                v1.Z + v2.Z);
        }

        /// <summary>
        /// Subtracts a vector from another one.
        /// </summary>
        public static Vector operator -(Vector v1, Vector v2)
        {
            return new Vector(
                v1.X - v2.X,
                v1.Y - v2.Y,
                v1.Z - v2.Z);
        }

        /// <summary>
        /// Negates a vector, so that it is pointing in the opposite
        /// direction.
        /// </summary>
        public static Vector operator -(Vector v)
        {
            return new Vector(-v.X, -v.Y, -v.Z);
        }

        /// <summary>
        /// Multiplies a vector by a scalar.
        /// </summary>
        public static Vector operator *(Vector v, double scalar)
        {
            return new Vector(
                v.X * scalar,
                v.Y * scalar,
                v.Z * scalar);
        }

        /// <summary>
        /// Does division of a vector with a scalar.
        /// </summary>
        public static Vector operator /(Vector v, double scalar)
        {
            return new Vector(
                v.X / scalar,
                v.Y / scalar,
                v.Z / scalar);
        }

        /// <summary>
        /// Overridden to make it easy to print out vectors.
        /// This return the vector in the format <x, y, z>
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "<" + X + ", " + Y + ", " + Z + ">";
        }
    }
}

The Program Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vectors
{
    class Program
    {
        static void Main(string[] args)
        {
            Vector v1 = new Vector(1, 2, 3);
            Vector v2 = new Vector(3, 3, 3);
            Console.WriteLine(v1 + v2);
            Console.WriteLine(v1 - v2);

            Vector v = new Vector(2, 0, -4);
            Console.WriteLine(-v);

            Console.WriteLine(v1 * 4);
            Console.WriteLine(new Vector(2, 4, 6) / 2);

            Console.ReadKey();
        }
    }
}