Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #182 from vpenades/main
Browse files Browse the repository at this point in the history
Added System.Numerics.Vectors constructors and operators
  • Loading branch information
jonlipsky committed Oct 2, 2021
2 parents 4ce7c8b + 302d411 commit a8feeb0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Microsoft.Maui.Graphics/AffineTransform.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Numerics;

namespace Microsoft.Maui.Graphics
{
Expand Down Expand Up @@ -52,6 +53,16 @@ public AffineTransform(float[] matrix)
}
}

public AffineTransform(in Matrix3x2 matrix)
{
_m00 = matrix.M11;
_m10 = matrix.M12;
_m01 = matrix.M21;
_m11 = matrix.M22;
_m02 = matrix.M31;
_m12 = matrix.M32;
}

public void SetMatrix(float m00, float m10, float m01, float m11, float m02, float m12)
{
_m00 = m00;
Expand All @@ -62,6 +73,16 @@ public void SetMatrix(float m00, float m10, float m01, float m11, float m02, flo
_m12 = m12;
}

public void SetMatrix(in Matrix3x2 matrix)
{
_m00 = matrix.M11;
_m10 = matrix.M12;
_m01 = matrix.M21;
_m11 = matrix.M22;
_m02 = matrix.M31;
_m12 = matrix.M32;
}

public float ScaleX => _m00;

public float ScaleY => _m11;
Expand Down Expand Up @@ -102,6 +123,16 @@ public void SetTransform(float m00, float m10, float m01, float m11, float m02,
_m12 = m12;
}

public void SetTransform(in Matrix3x2 matrix)
{
_m00 = matrix.M11;
_m10 = matrix.M12;
_m01 = matrix.M21;
_m11 = matrix.M22;
_m02 = matrix.M31;
_m12 = matrix.M32;
}

public void SetTransform(AffineTransform t)
{
SetTransform(t._m00, t._m10, t._m01, t._m11, t._m02, t._m12);
Expand Down Expand Up @@ -361,6 +392,13 @@ public bool OnlyScale()
return !HasRotate() && !HasTranslate();
}

public static implicit operator AffineTransform(Matrix3x2 matrix) => new AffineTransform(matrix);

public static explicit operator Matrix3x2(AffineTransform matrix)
{
return new Matrix3x2(matrix._m00, matrix._m10, matrix._m01, matrix._m11, matrix._m02, matrix._m12);
}

public bool IsIdentity => _m00 == 1.0f && _m11 == 1.0f && _m10 == 0.0f && _m01 == 0.0f && _m02 == 0.0f && _m12 == 0.0f;
}
}
11 changes: 11 additions & 0 deletions src/Microsoft.Maui.Graphics/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Numerics;

namespace Microsoft.Maui.Graphics
{
Expand Down Expand Up @@ -41,6 +42,14 @@ public Color(float red, float green, float blue, float alpha)
Alpha = alpha.Clamp(0, 1);
}

public Color(Vector4 color)
{
Red = color.X.Clamp(0, 1);
Green = color.Y.Clamp(0, 1);
Blue = color.Z.Clamp(0, 1);
Alpha = color.W.Clamp(0, 1);
}

public override string ToString()
{
return $"[Color: Red={Red}, Green={Green}, Blue={Blue}, Alpha={Alpha}]";
Expand Down Expand Up @@ -910,5 +919,7 @@ static double ParseColorValue(string elem, int maxValue, bool acceptPercent)
static double ParseOpacity(string elem)
=> double.Parse(elem, NumberStyles.Number, CultureInfo.InvariantCulture).Clamp(0, 1);

public static implicit operator Color(Vector4 color) => new Color(color);

}
}
9 changes: 9 additions & 0 deletions src/Microsoft.Maui.Graphics/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Numerics;

namespace Microsoft.Maui.Graphics
{
Expand Down Expand Up @@ -38,6 +39,12 @@ public Point(SizeF sz) : this()
Y = sz.Height;
}

public Point(Vector2 v)
{
X = v.X;
Y = v.Y;
}

public override bool Equals(object o)
{
if (!(o is Point))
Expand Down Expand Up @@ -113,6 +120,8 @@ public void Deconstruct(out double x, out double y)

public static implicit operator PointF(Point p) => new PointF((float)p.X, (float)p.Y);

public static implicit operator Point(Vector2 v) => new Point(v);

public static bool TryParse(string value, out Point point)
{
if (!string.IsNullOrEmpty(value))
Expand Down
11 changes: 11 additions & 0 deletions src/Microsoft.Maui.Graphics/PointF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Numerics;

namespace Microsoft.Maui.Graphics
{
Expand Down Expand Up @@ -32,6 +33,12 @@ public PointF(SizeF sz) : this()
Y = sz.Height;
}

public PointF(Vector2 v)
{
X = v.X;
Y = v.Y;
}

public override bool Equals(object o)
{
if (!(o is PointF))
Expand Down Expand Up @@ -106,6 +113,10 @@ public void Deconstruct(out float x, out float y)
}
public static implicit operator Point(PointF p) => new Point(p.X, p.Y);

public static implicit operator PointF(Vector2 v) => new PointF(v);

public static explicit operator Vector2(PointF p) => new Vector2(p.X, p.Y);

public static bool TryParse(string value, out PointF pointF)
{
if (!string.IsNullOrEmpty(value))
Expand Down
11 changes: 11 additions & 0 deletions src/Microsoft.Maui.Graphics/Size.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Numerics;

namespace Microsoft.Maui.Graphics
{
Expand Down Expand Up @@ -32,6 +33,16 @@ public Size(double width, double height)
_height = height;
}

public Size(Vector2 vector)
{
if (float.IsNaN(vector.X))
throw new ArgumentException("NaN is not a valid value for X");
if (float.IsNaN(vector.Y))
throw new ArgumentException("NaN is not a valid value for Y");
_width = vector.X;
_height = vector.Y;
}

public bool IsZero => _width == 0 && _height == 0;

[DefaultValue(0d)]
Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.Maui.Graphics/SizeF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Numerics;

namespace Microsoft.Maui.Graphics
{
Expand Down Expand Up @@ -32,6 +33,16 @@ public SizeF(float width, float height)
_height = height;
}

public SizeF(Vector2 vector)
{
if (float.IsNaN(vector.X))
throw new ArgumentException("NaN is not a valid value for X");
if (float.IsNaN(vector.Y))
throw new ArgumentException("NaN is not a valid value for Y");
_width = vector.X;
_height = vector.Y;
}

public bool IsZero => _width == 0 && _height == 0;

[DefaultValue(0d)]
Expand Down Expand Up @@ -88,6 +99,11 @@ public float Height
return new PointF(size.Width, size.Height);
}

public static explicit operator Vector2(SizeF size)
{
return new Vector2(size.Width, size.Height);
}

public bool Equals(SizeF other)
{
return _width.Equals(other._width) && _height.Equals(other._height);
Expand Down

0 comments on commit a8feeb0

Please sign in to comment.