This repository has been archived by the owner on Feb 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from SixLabors/MathF
Use MathF for .Net Core 2.0
- Loading branch information
Showing
9 changed files
with
268 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Six Labors and contributors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace SixLabors | ||
{ | ||
/// <summary> | ||
/// Provides common mathematical methods. | ||
/// </summary> | ||
internal static class MathFExtensions | ||
{ | ||
/// <summary> | ||
/// Converts a degree (360-periodic) angle to a radian (2*Pi-periodic) angle. | ||
/// </summary> | ||
/// <param name="degree">The angle in degrees.</param> | ||
/// <returns> | ||
/// The <see cref="float"/> representing the degree as radians. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static float DegreeToRadian(float degree) | ||
{ | ||
return degree * (MathF.PI / 180F); | ||
} | ||
|
||
/// <summary> | ||
/// Converts a radian (2*Pi-periodic) angle to a degree (360-periodic) angle. | ||
/// </summary> | ||
/// <param name="radian">The angle in radians.</param> | ||
/// <returns> | ||
/// The <see cref="float"/> representing the degree as radians. | ||
/// </returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static float RadianToDegree(float radian) | ||
{ | ||
return radian / (MathF.PI / 180F); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,49 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>Low level primitives for use across Six Labors projects..</Description> | ||
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix> | ||
<VersionPrefix Condition="$(packageversion) == ''">0.1.0-alpha2</VersionPrefix> | ||
<Authors>Six Labors</Authors> | ||
<TargetFramework>netstandard1.1</TargetFramework> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<AssemblyName>SixLabors.Core</AssemblyName> | ||
<PackageId>SixLabors.Core</PackageId> | ||
<PackageTags>rectangle;point;size,primitives</PackageTags> | ||
<PackageIconUrl>https://raw.githubusercontent.com/SixLabors/Home/master/logo.png</PackageIconUrl> | ||
<PackageProjectUrl>https://github.com/SixLabors/Core</PackageProjectUrl> | ||
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/SixLabors/Core</RepositoryUrl> | ||
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute> | ||
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute> | ||
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute> | ||
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute> | ||
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute> | ||
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute> | ||
<DebugType Condition="$(codecov) == 'true'">full</DebugType> | ||
<RootNamespace>SixLabors</RootNamespace> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<Description>Low level primitives for use across Six Labors projects..</Description> | ||
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix> | ||
<VersionPrefix Condition="$(packageversion) == ''">0.1.0-alpha2</VersionPrefix> | ||
<Authors>Six Labors</Authors> | ||
<TargetFrameworks>netstandard1.1;netcoreapp2.0;</TargetFrameworks> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<AssemblyName>SixLabors.Core</AssemblyName> | ||
<PackageId>SixLabors.Core</PackageId> | ||
<PackageTags>rectangle;point;size,primitives</PackageTags> | ||
<PackageIconUrl>https://raw.githubusercontent.com/SixLabors/Home/master/logo.png</PackageIconUrl> | ||
<PackageProjectUrl>https://github.com/SixLabors/Core</PackageProjectUrl> | ||
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/SixLabors/Core</RepositoryUrl> | ||
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute> | ||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> | ||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> | ||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> | ||
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute> | ||
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute> | ||
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute> | ||
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute> | ||
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute> | ||
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute> | ||
<DebugType Condition="$(codecov) == 'true'">full</DebugType> | ||
<RootNamespace>SixLabors</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<CodeAnalysisRuleSet>..\SixLabors.ruleset</CodeAnalysisRuleSet> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<CodeAnalysisRuleSet>..\SixLabors.ruleset</CodeAnalysisRuleSet> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<AdditionalFiles Include="..\..\stylecop.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004"> | ||
<PrivateAssets>All</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<AdditionalFiles Include="..\..\stylecop.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004"> | ||
<PrivateAssets>All</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.1'"> | ||
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" /> | ||
</ItemGroup> | ||
</Project> |
59 changes: 59 additions & 0 deletions
59
tests/SixLabors.Core.Tests/Helpers/FloatRoundingComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Six Labors and contributors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Numerics; | ||
|
||
namespace SixLabors.Tests.Helpers | ||
{ | ||
/// <summary> | ||
/// Allows the comparison of single-precision floating point values by precision. | ||
/// </summary> | ||
public struct FloatRoundingComparer : IEqualityComparer<float>, IEqualityComparer<Vector4> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="FloatRoundingComparer"/> struct. | ||
/// </summary> | ||
/// <param name="precision">The number of decimal places (valid values: 0-7)</param> | ||
public FloatRoundingComparer(int precision) | ||
{ | ||
Guard.MustBeBetweenOrEqualTo(precision, 0, 7, nameof(precision)); | ||
this.Precision = precision; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the number of decimal places (valid values: 0-7) | ||
/// </summary> | ||
public int Precision { get; } | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(float x, float y) | ||
{ | ||
float xp = (float)Math.Round(x, this.Precision, MidpointRounding.AwayFromZero); | ||
float yp = (float)Math.Round(y, this.Precision, MidpointRounding.AwayFromZero); | ||
|
||
return Comparer<float>.Default.Compare(xp, yp) == 0; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Equals(Vector4 x, Vector4 y) | ||
{ | ||
return this.Equals(x.X, y.X) && this.Equals(x.Y, y.Y) && this.Equals(x.Z, y.Z) && this.Equals(x.W, y.W); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public int GetHashCode(float obj) | ||
{ | ||
unchecked | ||
{ | ||
int hashCode = obj.GetHashCode(); | ||
hashCode = (hashCode * 397) ^ this.Precision.GetHashCode(); | ||
return hashCode; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public int GetHashCode(Vector4 obj) => HashHelpers.Combine(obj.GetHashCode(), this.Precision.GetHashCode()); | ||
} | ||
} |
Oops, something went wrong.