Browse Source

Merge branch 'master' into feature/menu-scroller

pull/4097/head
danwalmsley 6 years ago
committed by GitHub
parent
commit
a17086c93e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/Avalonia.Visuals/Size.cs
  2. 52
      src/Avalonia.Visuals/Vector.cs

13
src/Avalonia.Visuals/Size.cs

@ -189,7 +189,7 @@ namespace Avalonia
} }
/// <summary> /// <summary>
/// Returns a boolean indicating whether the size is equal to the other given size. /// Returns a boolean indicating whether the size is equal to the other given size (bitwise).
/// </summary> /// </summary>
/// <param name="other">The other size to test equality against.</param> /// <param name="other">The other size to test equality against.</param>
/// <returns>True if this size is equal to other; False otherwise.</returns> /// <returns>True if this size is equal to other; False otherwise.</returns>
@ -201,6 +201,17 @@ namespace Avalonia
// ReSharper enable CompareOfFloatsByEqualityOperator // ReSharper enable CompareOfFloatsByEqualityOperator
} }
/// <summary>
/// Returns a boolean indicating whether the size is equal to the other given size (numerically).
/// </summary>
/// <param name="other">The other size to test equality against.</param>
/// <returns>True if this size is equal to other; False otherwise.</returns>
public bool NearlyEquals(Size other)
{
return MathUtilities.AreClose(_width, other._width) &&
MathUtilities.AreClose(_height, other._height);
}
/// <summary> /// <summary>
/// Checks for equality between a size and an object. /// Checks for equality between a size and an object.
/// </summary> /// </summary>

52
src/Avalonia.Visuals/Vector.cs

@ -2,7 +2,8 @@ using System;
using System.Globalization; using System.Globalization;
using Avalonia.Animation.Animators; using Avalonia.Animation.Animators;
using Avalonia.Utilities; using Avalonia.Utilities;
using JetBrains.Annotations;
#nullable enable
namespace Avalonia namespace Avalonia
{ {
@ -17,20 +18,20 @@ namespace Avalonia
} }
/// <summary> /// <summary>
/// The X vector. /// The X component.
/// </summary> /// </summary>
private readonly double _x; private readonly double _x;
/// <summary> /// <summary>
/// The Y vector. /// The Y component.
/// </summary> /// </summary>
private readonly double _y; private readonly double _y;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Vector"/> structure. /// Initializes a new instance of the <see cref="Vector"/> structure.
/// </summary> /// </summary>
/// <param name="x">The X vector.</param> /// <param name="x">The X component.</param>
/// <param name="y">The Y vector.</param> /// <param name="y">The Y component.</param>
public Vector(double x, double y) public Vector(double x, double y)
{ {
_x = x; _x = x;
@ -38,12 +39,12 @@ namespace Avalonia
} }
/// <summary> /// <summary>
/// Gets the X vector. /// Gets the X component.
/// </summary> /// </summary>
public double X => _x; public double X => _x;
/// <summary> /// <summary>
/// Gets the Y vector. /// Gets the Y component.
/// </summary> /// </summary>
public double Y => _y; public double Y => _y;
@ -57,18 +58,18 @@ namespace Avalonia
} }
/// <summary> /// <summary>
/// Calculates the dot product of two vectors /// Calculates the dot product of two vectors.
/// </summary> /// </summary>
/// <param name="a">First vector</param> /// <param name="a">First vector.</param>
/// <param name="b">Second vector</param> /// <param name="b">Second vector.</param>
/// <returns>The dot product</returns> /// <returns>The dot product.</returns>
public static double operator *(Vector a, Vector b) public static double operator *(Vector a, Vector b)
=> Dot(a, b); => Dot(a, b);
/// <summary> /// <summary>
/// Scales a vector. /// Scales a vector.
/// </summary> /// </summary>
/// <param name="vector">The vector</param> /// <param name="vector">The vector.</param>
/// <param name="scale">The scaling factor.</param> /// <param name="scale">The scaling factor.</param>
/// <returns>The scaled vector.</returns> /// <returns>The scaled vector.</returns>
public static Vector operator *(Vector vector, double scale) public static Vector operator *(Vector vector, double scale)
@ -77,7 +78,7 @@ namespace Avalonia
/// <summary> /// <summary>
/// Scales a vector. /// Scales a vector.
/// </summary> /// </summary>
/// <param name="vector">The vector</param> /// <param name="vector">The vector.</param>
/// <param name="scale">The divisor.</param> /// <param name="scale">The divisor.</param>
/// <returns>The scaled vector.</returns> /// <returns>The scaled vector.</returns>
public static Vector operator /(Vector vector, double scale) public static Vector operator /(Vector vector, double scale)
@ -100,12 +101,12 @@ namespace Avalonia
} }
/// <summary> /// <summary>
/// Length of the vector /// Length of the vector.
/// </summary> /// </summary>
public double Length => Math.Sqrt(SquaredLength); public double Length => Math.Sqrt(SquaredLength);
/// <summary> /// <summary>
/// Squared Length of the vector /// Squared Length of the vector.
/// </summary> /// </summary>
public double SquaredLength => _x * _x + _y * _y; public double SquaredLength => _x * _x + _y * _y;
@ -154,9 +155,8 @@ namespace Avalonia
/// <returns>True if vectors are nearly equal.</returns> /// <returns>True if vectors are nearly equal.</returns>
public bool NearlyEquals(Vector other) public bool NearlyEquals(Vector other)
{ {
const float tolerance = float.Epsilon; return MathUtilities.AreClose(_x, other._x) &&
MathUtilities.AreClose(_y, other._y);
return Math.Abs(_x - other._x) < tolerance && Math.Abs(_y - other._y) < tolerance;
} }
public override bool Equals(object obj) => obj is Vector other && Equals(other); public override bool Equals(object obj) => obj is Vector other && Equals(other);
@ -189,9 +189,9 @@ namespace Avalonia
} }
/// <summary> /// <summary>
/// Returns a new vector with the specified X coordinate. /// Returns a new vector with the specified X component.
/// </summary> /// </summary>
/// <param name="x">The X coordinate.</param> /// <param name="x">The X component.</param>
/// <returns>The new vector.</returns> /// <returns>The new vector.</returns>
public Vector WithX(double x) public Vector WithX(double x)
{ {
@ -199,9 +199,9 @@ namespace Avalonia
} }
/// <summary> /// <summary>
/// Returns a new vector with the specified Y coordinate. /// Returns a new vector with the specified Y component.
/// </summary> /// </summary>
/// <param name="y">The Y coordinate.</param> /// <param name="y">The Y component.</param>
/// <returns>The new vector.</returns> /// <returns>The new vector.</returns>
public Vector WithY(double y) public Vector WithY(double y)
{ {
@ -311,25 +311,25 @@ namespace Avalonia
=> new Vector(-vector._x, -vector._y); => new Vector(-vector._x, -vector._y);
/// <summary> /// <summary>
/// Returnes the vector (0.0, 0.0) /// Returns the vector (0.0, 0.0).
/// </summary> /// </summary>
public static Vector Zero public static Vector Zero
=> new Vector(0, 0); => new Vector(0, 0);
/// <summary> /// <summary>
/// Returnes the vector (1.0, 1.0) /// Returns the vector (1.0, 1.0).
/// </summary> /// </summary>
public static Vector One public static Vector One
=> new Vector(1, 1); => new Vector(1, 1);
/// <summary> /// <summary>
/// Returnes the vector (1.0, 0.0) /// Returns the vector (1.0, 0.0).
/// </summary> /// </summary>
public static Vector UnitX public static Vector UnitX
=> new Vector(1, 0); => new Vector(1, 0);
/// <summary> /// <summary>
/// Returnes the vector (0.0, 1.0) /// Returns the vector (0.0, 1.0).
/// </summary> /// </summary>
public static Vector UnitY public static Vector UnitY
=> new Vector(0, 1); => new Vector(0, 1);

Loading…
Cancel
Save