Select Git revision
Utils.cs 2.91 KiB
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Utils
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public static Vector3 randomDirection() {
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
int r;
for(int i = 0; i<10; i++) {
r = random(0,3);
if (r == 0) x += 1.0f;
else if (r == 1) y += 1.0f;
else z += 1.0f;
}
if(random(1,3) > 1) x = -x;
if(random(1,3) > 1) y = -y;
if(random(1,3) > 1) z = -z;
return normalizedVector(x,y,z);
}
public static Vector3 normalizedVector(float x, float y, float z) {
return (new Vector3(x,y,z)).normalized;
}
public static Vector3 meanVector(List<Vector3> vectors) {
Vector3 res = Vector3.zero;
for(int i = 0; i<vectors.Count; i++) {
res += vectors[i];
}
res = res/vectors.Count;
return res.normalized;
}
public static Vector3 randomVector(float minX, float maxX, float minY, float maxY, float minZ, float maxZ) {
return new Vector3(random(minX,maxX),
random(minY,maxY),
random(minZ,maxZ));
}
public static float normVector(Vector3 v) {
return (float) Math.Sqrt(Math.Pow(v.x,2) + Math.Pow(v.y,2) + Math.Pow(v.z,2));
}
public static float normVector(float minX, float maxX, float minY, float maxY, float minZ, float maxZ) {
return (float) Math.Sqrt(Math.Pow(maxX-minX,2) + Math.Pow(maxY-minY,2) + Math.Pow(maxZ-minZ,2));
}
public static Vector3 noiseVector(Vector3 v, int nbBruit, float bruit ) {
int coin1; int coin2; float x = v.x; float y = v.y; float z = v.z;
for(int i=0;i<nbBruit;i++) {
coin1 = random(1,3);
coin2 = random(1,4);
if(coin2 == 1) {
if(coin1 == 1) x+=bruit;
else x-= bruit;
}
else if(coin2 == 2) {
if(coin1 == 1) y+=bruit;
else y-= bruit;
}
else {
if(coin1 == 1) z+=bruit;
else z-= bruit;
}
}
return normalizedVector(x,y,z);
}
public static float abs(float n) {return (float) Math.Sqrt(Math.Pow(n,2));}
public static float random(float min, float max) {return UnityEngine.Random.Range(min,max);}
public static int random(int min, int max) {return UnityEngine.Random.Range(min,max);}
public static float randomPosOrNeg(float n) { return n * ((float) Math.Pow(-1,random(0,2))); }
public static int randomPosOrNeg(int n) { return n * ((int) Math.Pow(-1,random(0,2))); }
}