ToolBX.Mathemancy
1.0.4-beta1
See the version list below for details.
dotnet add package ToolBX.Mathemancy --version 1.0.4-beta1
NuGet\Install-Package ToolBX.Mathemancy -Version 1.0.4-beta1
<PackageReference Include="ToolBX.Mathemancy" Version="1.0.4-beta1" />
paket add ToolBX.Mathemancy --version 1.0.4-beta1
#r "nuget: ToolBX.Mathemancy, 1.0.4-beta1"
// Install ToolBX.Mathemancy as a Cake Addin #addin nuget:?package=ToolBX.Mathemancy&version=1.0.4-beta1&prerelease // Install ToolBX.Mathemancy as a Cake Tool #tool nuget:?package=ToolBX.Mathemancy&version=1.0.4-beta1&prerelease
Mathemancy
A bundle for generic geometry types such as Vector2<T>, Size<T> and Rectangle<T>
Have you ever had the pleasure of working with a game engine that had types such as RectangleFloat or RectangleInt? I have! And I find it kind of annoying. If only there was a way to have a generic Rectangle…
There is!
Have you ever wanted Size and/or Vector types? We have that too!
This library is still small but will (probably) get bigger with time.
Most (all of it, really) of the logic for generic numeric operators was adapted to .NET 6 from Marc Gravell and Jon Skeet's https://jonskeet.uk/csharp/miscutil/
Variable names
a, b, c, m, x and y may be perfectly acceptable as variable names in mathematical theories and pieces of paper but it has no place in programming as it only serves to make code less readable.
This library recognizes that not everyone may have a strong mathematical background and makes use of longer variable names to accomodate a wider range of developers.
Mathemancy thus follows the ToolBX philosophy that programming should never be a guessing game and code should speak for itself.
Linear interpolation
You can also use the Mathematics class to interpolate between values. Most game engines already have these included (Lerp) but they are often misused by hobbyists because those engines seem to like to keep their code and documentation obfuscated for some reason.
public void DoSomeStuff()
{
//Returns a value between 50 and 125 when the current progress is 40% (80)
var currentValue = Mathematics.Interpolate(50, 125, 0.4f);
}
You'll usually want to use linear interpolation in a repeating method such as a game loop for instance.
private float _progress;
private float _speed;
private float _elapsedTime;
private float _position;
//In this example, an object's _position would gradually move from 50 towards 125
public void Update(float deltaTime)
{
if (_progress < 1)
{
_elapsedTime += deltaTime;
_progress = _elapsedTime * deltaTime * _speed;
_position = Mathematics.Interpolate(50, 125, _progress);
}
}
Conversion
Numbers
Want to convert that Vector<float> into a Vector<int> but don't want to do this?
var newPosition = new Vector<int>((int)position.X, (int)position.Y);
You're in luck because you can just do this :
var newPosition = position.ToInt();
var newSize = size.ToDouble();
var newRectangle = rectangle.ToULong();
These extension methods are available for Vector2, Vector3, Rectangle and Size.
Mathemancy.Randomness
We have two services to generate random numbers. One of them is a more predictable pseudo random number generator while the other is a more cryptographically-secure generator.
While more predictable, the pseudo generator is a lot faster so I recommend using it if performance is an issue or if you need to generate a lot of random numbers often.
Both services share the same base interface so they are used in the exact same way and offer the same capabilities.
Disclaimer : These services provide common use cases for generating random numbers. If you need it to do some other weird things then it's probably not for you 😦
Getting started
If you already use [AutoInject] then you only need to inject IPseudoRandomNumberGenerator and/or ISecureRandomNumberGenerator to a class in order to use it.
If not, you can use the following method when injecting services :
services.AddRandomness();
If you don't use dependency injection at all then you can just instantiate the classes directly and use them.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net6.0
- ToolBX.AutoInject (>= 1.0.1.1-beta)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on ToolBX.Mathemancy:
Package | Downloads |
---|---|
ToolBX.NetAbstractions
Abstractions for .NET base types such as File and Directory to provide easier means to mock low-level operations. |
|
ToolBX.Collections.Grid
An observable, dynamic two-dimensional array. |
|
ToolBX.Spritebound
Spritesheet and animation management for sprite-based projects. |
|
ToolBX.AwesomeMarkup
Extracts any kind of markup information from a string of characters. |
|
ToolBX.Spritebound.Web
Spritesheet management for websites. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.0.0 | 1,698 | 9/26/2024 |
3.0.0-beta1 | 209 | 9/23/2024 |
2.2.1 | 1,807 | 1/12/2024 |
2.2.0 | 131 | 1/12/2024 |
2.2.0-beta4 | 119 | 1/10/2024 |
2.2.0-beta3 | 345 | 1/7/2024 |
2.2.0-beta2 | 188 | 12/13/2023 |
2.2.0-beta1 | 121 | 12/12/2023 |
2.0.3 | 170 | 9/23/2023 |
2.0.3-beta3 | 124 | 9/22/2023 |
2.0.3-beta2 | 109 | 9/17/2023 |
2.0.2 | 450 | 6/19/2023 |
2.0.1 | 761 | 4/25/2023 |
2.0.0 | 490 | 11/15/2022 |
2.0.0-beta1 | 575 | 9/22/2022 |
1.1.2 | 1,490 | 9/8/2022 |
1.1.1 | 1,551 | 7/18/2022 |
1.1.0 | 445 | 7/17/2022 |
1.0.7 | 441 | 7/17/2022 |
1.0.6 | 921 | 7/5/2022 |
1.0.5 | 538 | 7/4/2022 |
1.0.4 | 433 | 7/4/2022 |
1.0.4-beta3 | 273 | 6/5/2022 |
1.0.4-beta2 | 163 | 5/22/2022 |
1.0.4-beta1 | 145 | 5/21/2022 |
1.0.3 | 1,713 | 4/24/2022 |
1.0.3-beta1 | 153 | 5/21/2022 |
1.0.1 | 765 | 2/11/2022 |
1.0.0 | 277 | 12/22/2021 |