csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.3 KiB
93 lines
2.3 KiB
// -----------------------------------------------------------------------
|
|
// <copyright file="Vector.cs" company="Steven Kirk">
|
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
// </copyright>
|
|
// -----------------------------------------------------------------------
|
|
|
|
namespace Perspex
|
|
{
|
|
using System;
|
|
using System.Globalization;
|
|
|
|
/// <summary>
|
|
/// Defines a vector.
|
|
/// </summary>
|
|
public struct Vector
|
|
{
|
|
/// <summary>
|
|
/// The X vector.
|
|
/// </summary>
|
|
private double x;
|
|
|
|
/// <summary>
|
|
/// The Y vector.
|
|
/// </summary>
|
|
private double y;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Vector"/> structure.
|
|
/// </summary>
|
|
/// <param name="x">The X vector.</param>
|
|
/// <param name="y">The Y vector.</param>
|
|
public Vector(double x, double y)
|
|
{
|
|
this.x = x;
|
|
this.y = y;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the X vector.
|
|
/// </summary>
|
|
public double X
|
|
{
|
|
get { return this.x; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the Y vector.
|
|
/// </summary>
|
|
public double Y
|
|
{
|
|
get { return this.y; }
|
|
}
|
|
|
|
public static Vector operator -(Vector a)
|
|
{
|
|
return new Vector(-a.x, -a.y);
|
|
}
|
|
|
|
public static Vector operator +(Vector a, Vector b)
|
|
{
|
|
return new Vector(a.x + b.x, a.y + b.y);
|
|
}
|
|
|
|
public static Vector operator -(Vector a, Vector b)
|
|
{
|
|
return new Vector(a.x - b.x, a.y - b.y);
|
|
}
|
|
|
|
public static explicit operator Point(Vector a)
|
|
{
|
|
return new Point(a.x, a.y);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the string representation of the point.
|
|
/// </summary>
|
|
/// <returns>The string representation of the point.</returns>
|
|
public override string ToString()
|
|
{
|
|
return string.Format(CultureInfo.InvariantCulture, "{0}, {1}", this.x, this.y);
|
|
}
|
|
|
|
public Vector WithX(double x)
|
|
{
|
|
return new Vector(x, this.y);
|
|
}
|
|
|
|
public Vector WithY(double y)
|
|
{
|
|
return new Vector(this.x, y);
|
|
}
|
|
}
|
|
}
|
|
|