// // AniMathUtils.h // SteveMaggieCpp // // Created by Katarzyna Kalinowska-Górska on 10.05.2017. // // #ifndef AniMathUtils_h #define AniMathUtils_h #include #include #include #include #include "cocos2d.h" class AniMathUtils { public: // Returns a random number between min (inclusive) and max (exclusive) static double getRandom(double min, double max); // Returns a random integer between min (included) and max (excluded) static int getRandomInt(int min, int max); static cocos2d::Color4F getRandomOpaqueColor(); static int signum(float expression){ if(expression < 0){ return -1; } else if(expression > 0){ return 1; } return 0; }; template static T getRandomElement(std::vector& arr){ auto size = arr.size(); if(size > 0){ int randomIndex = AniMathUtils::getRandomInt(0, (int)size-1); return arr[randomIndex]; } throw "Empty array!"; } template static void shuffleArray(std::vector& arr) { int currentIndex = (int)arr.size(), randomIndex; T temporaryValue; // While there remain elements to shuffle... while (0 != currentIndex) { // Pick a remaining element... randomIndex = AniMathUtils::getRandomInt(0, currentIndex-1); currentIndex -= 1; // And swap it with the current element. temporaryValue = arr[currentIndex]; arr[currentIndex] = arr[randomIndex]; arr[randomIndex] = temporaryValue; } } template static void multiplyArray(std::vector& arr, T byValue) { for(int i = 0; i < arr.size(); ++i){ arr[i] *= byValue; } } template static void multiplyPointArray(std::vector& arr, T byValue) { for(int i = 0; i < arr.size(); ++i){ arr[i].x *= byValue; arr[i].y *= byValue; } } template static void multiplyRectPositionsArray(std::vector& arr, T byValue) { for(int i = 0; i < arr.size(); ++i){ arr[i].origin.x *= byValue; arr[i].origin.y *= byValue; arr[i].size.width *= byValue; arr[i].size.height *= byValue; } } template static void multiplyRectPosition(cocos2d::Rect& rect, T byValue) { rect.origin.x *= byValue; rect.origin.y *= byValue; rect.size.width *= byValue; rect.size.height *= byValue; } template static void multiplyPoint(cocos2d::Point& point, T byValue) { point.x *= byValue; point.y *= byValue; } //NOT WORKING, WHY? Container size is always 0. // template // static void averageValue(const Container& valueArray, T& averageValue){ // if(valueArray.size() == 0){ // averageValue = 0; // } else { // averageValue = std::accumulate(valueArray.begin(), valueArray.end(), 0) / valueArray.size(); // } // } //temp static void averageValue(const std::deque& valueArray, float& averageValue){ if(valueArray.size() == 0){ averageValue = 0; } else { float size = valueArray.size(); averageValue = std::accumulate(valueArray.begin(), valueArray.end(), 0) / size; } } // static void drawRoundedCorneredRect(cocos2d::DrawNode*, cocos2d::Rect rect, float cornerRadius, cocos2d::Color4F&); private: static std::mt19937 _getRNG(); }; #endif /* AniMathUtils_h */