MODULE: MathsPlus v1.0 - Abs, AbsInt, Ceil, Floor, Max, MaxInt, Min, MinInt...

Started by monkey0506, Wed 27/01/2016 19:02:48

Previous topic - Next topic

monkey0506

DOWNLOAD

The MathsPlus module adds some additional Maths functions, including:

* float Maths.Abs(float)
* float Maths.Ceil(float)
* float Maths.Floor(float)
* float Maths.Max(float, float)
* float Maths.Min(float, float)
* float Maths.Round(float)
* int Maths.AbsInt(int)
* int Maths.MaxInt(int, int)
* int Maths.MinInt(int, int)

Requires AGS 3.4.0.6 or higher

Sslaxx

Stuart "Sslaxx" Moore.

Gurok

I think we want to eventually have some kind of polymorphism or type coercion for AGS proper. i.e. A Maths.Abs() that would support both floats and ints. For now, I think this is a good approach.

This library might also work as a good study reference or just time-tested implementations of common methods. I am not sure what your intention was though, Monkey. Was this just to fill gaps in the existing library or to be more of a supplemental library with slightly less general use things?

Whatever the case, there's Clamp, which is common for graphics stuff:

Code: ags

// Clamp a number to a specified range
float Maths::Clamp(float value, float minimum, float maximum);
int Maths::ClampInt(int value, int minimum, int maximum);


If you are taking further suggestions, here are some not-so-common ones I use:

Code: ags

// Find the distance between two points (a^2 + b^2 = c^2)
float Maths::DistanceBetween(float x1, float y1, float x2, float y2);
int Maths::DistanceBetweenInt(int x1, int y1, int x2, int y2);

// Find whether two ranges intersect (startA <= stopB and stopA >= startB)
// I put this in a method because it's annoying to have to work from the proof each time to get the right conditions
bool Maths::Intersects(float startA, float stopA, float startB, float stopB);
bool Maths::IntersectsInt(int startA, int stopA, int startB, int stopB);

// Return true if a given probability succeeds
// This is probably the best candidate for a library, as it's a standard form
bool Maths::Chance(float p); // p = 0.5 is a coin flip
// I actually use something like this because it's easy to read as "Chance(1, 20) => chance 1 in 20" and the default is a percentage
bool Maths::Chance(int numerator, int denominator = 100);


If this is a shared library, for something like Chance, it might be better to see which one people prefer.
[img]http://7d4iqnx.gif;rWRLUuw.gi

Crimson Wizard

I question the decision to give "basic" names to float variants, and specific names to integer ones.
I know this may sound naive, but I think AGSers use integers more often than floats, even in calculations; therefore it might be more natural for them to have simplier function names for integer variants. Float variants could be distinguished by F prefix or postfix (MaxF, MinF, AbsF, -- or -- FMax, FMin, FAbs, etc).

monkey0506

Quote from: Crimson Wizard on Thu 11/02/2016 12:05:43I question the decision to give "basic" names to float variants, and specific names to integer ones.

Your rationale about the general use case might be accurate, but I picked the names I did to better mirror the existing methods, which all operate on floats.

I didn't have any particular purpose behind the module except filling in gaps for common functions that I particularly needed. These specific ones are probably general enough to warrant a built-in implementation, but I would rate it as very low priority (although, the implementations themselves are trivial, so...).

vga256

Apologies for the necropost - since monkey0506's link is long dead, just wanted to share an archived link to the module:
Download Here

SMF spam blocked by CleanTalk