HMathUtils.h 4 KB
//
//  HMathUtils.h
//  SteveMaggieCpp
//
//  Created by Katarzyna Kalinowska-Górska on 10.05.2017.
//
//

#ifndef HMathUtils_h
#define HMathUtils_h

#include <stdio.h>
#include <numeric>
#include <deque>
#include <random>
#include "cocos2d.h"

class HMathUtils {

    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<class T>
        static T getRandomElement(std::vector<T>& arr){
            auto size = arr.size();
            if(size > 0){
                int randomIndex = HMathUtils::getRandomInt(0, (int)size-1);
                return arr[randomIndex];
            }
            throw "Empty array!";
        }
    
        template<class T>
        static void shuffleArray(std::vector<T>& arr)
        {
            int currentIndex = (int)arr.size(), randomIndex;
            T temporaryValue;

            // While there remain elements to shuffle...
            while (0 != currentIndex) {

                // Pick a remaining element...
                randomIndex = HMathUtils::getRandomInt(0, currentIndex-1);
                currentIndex -= 1;

                // And swap it with the current element.
                temporaryValue = arr[currentIndex];
                arr[currentIndex] = arr[randomIndex];
                arr[randomIndex] = temporaryValue;
            }
        }
    
        template<class T>
        static void multiplyArray(std::vector<T>& arr, T byValue)
        {
            for(int i = 0; i < arr.size(); ++i){
                arr[i] *= byValue;
            }
        }
    
    template<class T>
    static void multiplyPointArray(std::vector<cocos2d::Point>& arr, T byValue)
        {
            for(int i = 0; i < arr.size(); ++i){
                arr[i].x *= byValue;
                arr[i].y *= byValue;
            }
        }
    
    template<class T>
    static void multiplyRectPositionsArray(std::vector<cocos2d::Rect>& 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<class T>
    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<class T>
    static void multiplyPoint(cocos2d::Point& point, T byValue)
    {
        point.x *= byValue;
        point.y *= byValue;
    }
    
    //NOT WORKING, WHY? Container size is always 0.
//        template<class T, class Container>
//        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<float>& 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 /* HMathUtils_h */