<commit_before>package org.anddev.andengine.util;

import java.util.Random;

import android.util.FloatMath;

/**
 * @author <NAME>
 * @since 20:42:15 - 17.12.2009
 */
public class MathUtils {
	// ===========================================================
	// Constants
	// ===========================================================

	public static Random RANDOM = new Random(System.nanoTime());

	public static final float PI = (float) Math.PI;
	public static final float DEG_TO_RAD = PI / 180.0f;
	public static final float RAD_TO_DEG = 180.0f / PI;

	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================

	public static final float radToDeg(final float pRad) {
		return RAD_TO_DEG * pRad;
	}

	public static final float degToRad(final float pDegree) {
		return DEG_TO_RAD * pDegree;
	}

	public static final float random(final float pMin, final float pMax) {
		return pMin + RANDOM.nextFloat() * (pMax - pMin);
	}

	public static final int random(final int pMin, final int pMax) {
		return pMin + RANDOM.nextInt(pMax - pMin + 1);
	}

	public static final boolean isPowerOfTwo(final int n) {
		return ((n != 0) && (n & (n - 1)) == 0);
	}

	public static final int nextPowerOfTwo(final int n) {
		int k = n;

		if (k == 0) {
			return 1;
		}

		k--;

		for (int i = 1; i < 32; i <<= 1) {
			k = k | k >> i;
		}

		return k + 1;
	}

	public static final int sum(final int[] pValues) {
		int sum = 0;
		for(int i = pValues.length - 1; i >= 0; i--) {
			sum += pValues[i];
		}

		return sum;
	}

	public static final void arraySumInternal(final int[] pValues) {
		final int valueCount = pValues.length;
		for(int i = 1; i < valueCount; i++) {
			pValues[i] = pValues[i-1] + pValues[i];
		}
	}

	public static final void arraySumInternal(final long[] pValues) {
		final int valueCount = pValues.length;
		for(int i = 1; i < valueCount; i++) {
			pValues[i] = pValues[i-1] + pValues[i];
		}
	}

	public static final void arraySumInternal(final long[] pValues, final long pFactor) {
		pValues[0] = pValues[0] * pFactor;
		final int valueCount = pValues.length;
		for(int i = 1; i < valueCount; i++) {
			pValues[i] = pValues[i-1] + pValues[i] * pFactor;
		}
	}

	public static final float arraySum(final float[] pValues) {
		float sum = 0;
		final int valueCount = pValues.length;
		for(int i = 0; i < valueCount; i++) {
			sum += pValues[i];
		}
		return sum;
	}

	public static final float arrayAverage(final float[] pValues) {
		return arraySum(pValues) / pValues.length;
	}

	@Deprecated
	public static float[] rotateAroundCenter(final float pX, final float pY, final float pRotation, final float pRotationCenterX, final float pRotationCenterY, final float[] pReuse) {
		final float RotationRad = MathUtils.degToRad(pRotation);
		pReuse[0] = pRotationCenterX + (FloatMath.cos(RotationRad) * (pX - pRotationCenterX) - FloatMath.sin(RotationRad) * (pY - pRotationCenterY));
		pReuse[1] = pRotationCenterY + (FloatMath.sin(RotationRad) * (pX - pRotationCenterX) + FloatMath.cos(RotationRad) * (pY - pRotationCenterY));
		return pReuse;
	}

	@Deprecated
	public static float[] scaleAroundCenter(final float pX, final float pY, final float pScaleX, final float pScaleY, final float pCenterCenterX, final float pCenterCenterY, final float[] pReuse) {
		pReuse[0] = pCenterCenterX + (pX - pCenterCenterX) * pScaleX;
		pReuse[1] = pCenterCenterY + (pY - pCenterCenterY) * pScaleY;
		
		return pReuse;
	}
	
	
	public static float[] rotateAroundCenter(final float[] pVertices, final float pRotation, final float pRotationCenterX, final float pRotationCenterY) {
		final float rotationRad = MathUtils.degToRad(pRotation);
		final float sinRotationRad = FloatMath.sin(rotationRad);
		final float cosRotationInRad = FloatMath.cos(rotationRad);
		
		for(int i = pVertices.length - 2; i >= 0; i -= 2) {
			float pX = pVertices[i];
			float pY = pVertices[i + 1];
			pVertices[i] = pRotationCenterX + (cosRotationInRad * (pX - pRotationCenterX) - sinRotationRad * (pY - pRotationCenterY));
			pVertices[i + 1] = pRotationCenterY + (sinRotationRad * (pX - pRotationCenterX) + cosRotationInRad * (pY - pRotationCenterY));	
		}
		return pVertices;
	}
	
	public static float[] scaleAroundCenter(final float[] pVertices, final float pScaleX, final float pScaleY, final float pScaleCenterX, final float pScaleCenterY) {
		for(int i = pVertices.length - 2; i >= 0; i -= 2) {
			pVertices[i] = pScaleCenterX + (pVertices[i] - pScaleCenterX) * pScaleX;
			pVertices[i + 1] = pScaleCenterY + (pVertices[i + 1] - pScaleCenterY) * pScaleY;
		}
		
		return pVertices;
	}
	
	public static float[] rotateAndScaleAroundCenter(final float[] pVertices, final float pRotation, final float pRotationCenterX, final float pRotationCenterY, final float pScaleX, final float pScaleY, final float pScaleCenterX, final float pScaleCenterY) {
		rotateAroundCenter(pVertices, pRotation, pRotationCenterX, pRotationCenterY);
		return scaleAroundCenter(pVertices, pScaleX, pScaleY, pScaleCenterX, pScaleCenterY);
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}<commit_msg>Optimized methods in MathUtils used by CollisionDetection through early exit conditions.<commit_after>package org.anddev.andengine.util;

import java.util.Random;

import android.util.FloatMath;

/**
 * @author <NAME>
 * @since 20:42:15 - 17.12.2009
 */
public class MathUtils {
	// ===========================================================
	// Constants
	// ===========================================================

	public static Random RANDOM = new Random(System.nanoTime());

	public static final float PI = (float) Math.PI;
	public static final float DEG_TO_RAD = PI / 180.0f;
	public static final float RAD_TO_DEG = 180.0f / PI;

	// ===========================================================
	// Fields
	// ===========================================================

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	// ===========================================================
	// Methods
	// ===========================================================

	public static final float radToDeg(final float pRad) {
		return RAD_TO_DEG * pRad;
	}

	public static final float degToRad(final float pDegree) {
		return DEG_TO_RAD * pDegree;
	}

	public static final float random(final float pMin, final float pMax) {
		return pMin + RANDOM.nextFloat() * (pMax - pMin);
	}

	public static final int random(final int pMin, final int pMax) {
		return pMin + RANDOM.nextInt(pMax - pMin + 1);
	}

	public static final boolean isPowerOfTwo(final int n) {
		return ((n != 0) && (n & (n - 1)) == 0);
	}

	public static final int nextPowerOfTwo(final int n) {
		int k = n;

		if (k == 0) {
			return 1;
		}

		k--;

		for (int i = 1; i < 32; i <<= 1) {
			k = k | k >> i;
		}

		return k + 1;
	}

	public static final int sum(final int[] pValues) {
		int sum = 0;
		for(int i = pValues.length - 1; i >= 0; i--) {
			sum += pValues[i];
		}

		return sum;
	}

	public static final void arraySumInternal(final int[] pValues) {
		final int valueCount = pValues.length;
		for(int i = 1; i < valueCount; i++) {
			pValues[i] = pValues[i-1] + pValues[i];
		}
	}

	public static final void arraySumInternal(final long[] pValues) {
		final int valueCount = pValues.length;
		for(int i = 1; i < valueCount; i++) {
			pValues[i] = pValues[i-1] + pValues[i];
		}
	}

	public static final void arraySumInternal(final long[] pValues, final long pFactor) {
		pValues[0] = pValues[0] * pFactor;
		final int valueCount = pValues.length;
		for(int i = 1; i < valueCount; i++) {
			pValues[i] = pValues[i-1] + pValues[i] * pFactor;
		}
	}

	public static final float arraySum(final float[] pValues) {
		float sum = 0;
		final int valueCount = pValues.length;
		for(int i = 0; i < valueCount; i++) {
			sum += pValues[i];
		}
		return sum;
	}

	public static final float arrayAverage(final float[] pValues) {
		return arraySum(pValues) / pValues.length;
	}

	@Deprecated
	public static float[] rotateAroundCenter(final float pX, final float pY, final float pRotation, final float pRotationCenterX, final float pRotationCenterY, final float[] pReuse) {
		final float RotationRad = MathUtils.degToRad(pRotation);
		pReuse[0] = pRotationCenterX + (FloatMath.cos(RotationRad) * (pX - pRotationCenterX) - FloatMath.sin(RotationRad) * (pY - pRotationCenterY));
		pReuse[1] = pRotationCenterY + (FloatMath.sin(RotationRad) * (pX - pRotationCenterX) + FloatMath.cos(RotationRad) * (pY - pRotationCenterY));
		return pReuse;
	}

	@Deprecated
	public static float[] scaleAroundCenter(final float pX, final float pY, final float pScaleX, final float pScaleY, final float pCenterCenterX, final float pCenterCenterY, final float[] pReuse) {
		pReuse[0] = pCenterCenterX + (pX - pCenterCenterX) * pScaleX;
		pReuse[1] = pCenterCenterY + (pY - pCenterCenterY) * pScaleY;
		
		return pReuse;
	}
	
	
	public static float[] rotateAroundCenter(final float[] pVertices, final float pRotation, final float pRotationCenterX, final float pRotationCenterY) {
		if(pRotation != 0) {
			final float rotationRad = MathUtils.degToRad(pRotation);
			final float sinRotationRad = FloatMath.sin(rotationRad);
			final float cosRotationInRad = FloatMath.cos(rotationRad);
			
			for(int i = pVertices.length - 2; i >= 0; i -= 2) {
				float pX = pVertices[i];
				float pY = pVertices[i + 1];
				pVertices[i] = pRotationCenterX + (cosRotationInRad * (pX - pRotationCenterX) - sinRotationRad * (pY - pRotationCenterY));
				pVertices[i + 1] = pRotationCenterY + (sinRotationRad * (pX - pRotationCenterX) + cosRotationInRad * (pY - pRotationCenterY));	
			}
		}
		return pVertices;
	}
	
	public static float[] scaleAroundCenter(final float[] pVertices, final float pScaleX, final float pScaleY, final float pScaleCenterX, final float pScaleCenterY) {
		if(pScaleX != 0 || pScaleY != 0) {
			for(int i = pVertices.length - 2; i >= 0; i -= 2) {
				pVertices[i] = pScaleCenterX + (pVertices[i] - pScaleCenterX) * pScaleX;
				pVertices[i + 1] = pScaleCenterY + (pVertices[i + 1] - pScaleCenterY) * pScaleY;
			}
		}
		
		return pVertices;
	}
	
	public static float[] rotateAndScaleAroundCenter(final float[] pVertices, final float pRotation, final float pRotationCenterX, final float pRotationCenterY, final float pScaleX, final float pScaleY, final float pScaleCenterX, final float pScaleCenterY) {
		rotateAroundCenter(pVertices, pRotation, pRotationCenterX, pRotationCenterY);
		return scaleAroundCenter(pVertices, pScaleX, pScaleY, pScaleCenterX, pScaleCenterY);
	}

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
{'fraction_non_alphanumeric': 0.2438509001774998, 'fraction_numerical': 0.010396416194742625, 'mean_word_length': 4.4375, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5002431', 'n_tokens_mistral': 4099, 'n_tokens_neox': 3615, 'n_words': 996}