// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; using System.Globalization; using System.Xml.Linq; namespace Avalonia { /// /// Defines a vector. /// public struct Vector { /// /// The X vector. /// private readonly double _x; /// /// The Y vector. /// private readonly double _y; /// /// Initializes a new instance of the structure. /// /// The X vector. /// The Y vector. public Vector(double x, double y) { _x = x; _y = y; } /// /// Gets the X vector. /// public double X => _x; /// /// Gets the Y vector. /// public double Y => _y; /// /// Converts the to a . /// /// The vector. public static explicit operator Point(Vector a) { return new Point(a._x, a._y); } /// /// Calculates the dot product of two vectors /// /// First vector /// Second vector /// The dot product public static double operator *(Vector a, Vector b) { return a.X*b.X + a.Y*b.Y; } /// /// Scales a vector. /// /// The vector /// The scaling factor. /// The scaled vector. public static Vector operator *(Vector vector, double scale) { return new Vector(vector._x * scale, vector._y * scale); } /// /// Scales a vector. /// /// The vector /// The divisor. /// The scaled vector. public static Vector operator /(Vector vector, double scale) { return new Vector(vector._x / scale, vector._y / scale); } /// /// Length of the vector /// public double Length => Math.Sqrt(X*X + Y*Y); /// /// Negates a vector. /// /// The vector. /// The negated vector. public static Vector operator -(Vector a) { return new Vector(-a._x, -a._y); } /// /// Adds two vectors. /// /// The first vector. /// The second vector. /// A vector that is the result of the addition. public static Vector operator +(Vector a, Vector b) { return new Vector(a._x + b._x, a._y + b._y); } /// /// Subtracts two vectors. /// /// The first vector. /// The second vector. /// A vector that is the result of the subtraction. public static Vector operator -(Vector a, Vector b) { return new Vector(a._x - b._x, a._y - b._y); } /// /// Returns the string representation of the point. /// /// The string representation of the point. public override string ToString() { return string.Format(CultureInfo.InvariantCulture, "{0}, {1}", _x, _y); } /// /// Returns a new vector with the specified X coordinate. /// /// The X coordinate. /// The new vector. public Vector WithX(double x) { return new Vector(x, _y); } /// /// Returns a new vector with the specified Y coordinate. /// /// The Y coordinate. /// The new vector. public Vector WithY(double y) { return new Vector(_x, y); } } }