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

CE_Wrap #2

Open
givename opened this issue Feb 7, 2022 · 0 comments
Open

CE_Wrap #2

givename opened this issue Feb 7, 2022 · 0 comments

Comments

@givename
Copy link

givename commented Feb 7, 2022

I don't think this function works correctly.
Maybe I misunderstood the essence of this function?

example:

CE_Wrap(10, 10, 360);  // expect 10  // receive 10
CE_Wrap(360, 10, 360); // expect 10  // receive 360
CE_Wrap(9, 10, 360);   // expect 359 // receive 369
CE_Wrap(361, 10, 360); // expect 11  // receive 1
CE_Wrap(360.0000078, 10, 360);      // expect 10.0000078  // receive 360.0000078
CE_Wrap(-201, -100, 100); // expect -1 // receive 99
CE_Wrap(301, -100, 100); // expect -99 // receive 1

I would like to offer my solution, which seems to achieve the desired behavior.

function CE_Wrap(_number, _min, _max) 
{
	var _length = (_max - _min);
	_number    -= _min;
	
	return (_number + ceil(-_number / _length) * _length + _min);
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant