using System.Collections; using System.Collections.Generic; using UnityEngine; public class CourbesFermees : MonoBehaviour { // Liste des points composant la courbe private List ListePoints = new List(); // Donnees i.e. points cliqués public GameObject Donnees; // Coordonnees des points composant le polygone de controle private List PolygoneControleX = new List(); private List PolygoneControleY = new List(); // degres des polynomes par morceaux public int degres = 5; // nombre d'itération de subdivision public int nombreIteration = 5; ////////////////////////////////////////////////////////////////////////// // fonction : subdivise // // semantique : réalise nombreIteration subdivision pour des polys de // // degres degres // // params : - List X : abscisses des point de controle // // - List Y : odronnees des point de controle // // sortie : // // - (List, List) : points de la courbe // ////////////////////////////////////////////////////////////////////////// (List, List) subdivise(List X, List Y) { List Xold = new List(X); List Yold = new List(Y); List XResult = new List(); List YResult = new List(); for (int d = 1; d <= degres; d++) { XResult = new List(); YResult = new List(); for (int i = 0; i < Xold.Count; i++) { if (d != degres) { XResult.Add(Xold[i]); YResult.Add(Yold[i]); } XResult.Add((Xold[(i + 1) % Xold.Count] + Xold[i]) / 2); YResult.Add((Yold[(i + 1) % Yold.Count] + Yold[i]) / 2); } Xold = new List(XResult); Yold = new List(YResult); } return (Xold, Yold); } ////////////////////////////////////////////////////////////////////////// //////////////////////////// NE PAS TOUCHER ////////////////////////////// ////////////////////////////////////////////////////////////////////////// void Update() { if (Input.GetKeyDown(KeyCode.Return)) { var ListePointsCliques = GameObject.Find("Donnees").GetComponent(); if (ListePointsCliques.X.Count > 0) { for (int i = 0; i < ListePointsCliques.X.Count; ++i) { PolygoneControleX.Add(ListePointsCliques.X[i]); PolygoneControleY.Add(ListePointsCliques.Y[i]); } List Xres = new List(); List Yres = new List(); (Xres, Yres) = subdivise(ListePointsCliques.X, ListePointsCliques.Y); for (int i = 0; i < Xres.Count; ++i) { ListePoints.Add(new Vector3(Xres[i], -4.0f, Yres[i])); } } } } void OnDrawGizmosSelected() { Gizmos.color = Color.red; for (int i = 0; i < PolygoneControleX.Count - 1; ++i) { Gizmos.DrawLine(new Vector3(PolygoneControleX[i], -4.0f, PolygoneControleY[i]), new Vector3(PolygoneControleX[i + 1], -4.0f, PolygoneControleY[i + 1])); } if (PolygoneControleX.Count > 0) { Gizmos.DrawLine(new Vector3(PolygoneControleX[PolygoneControleX.Count - 1], -4.0f, PolygoneControleY[PolygoneControleY.Count - 1]), new Vector3(PolygoneControleX[0], -4.0f, PolygoneControleY[0])); } Gizmos.color = Color.blue; for (int i = 0; i < ListePoints.Count - 1; ++i) { Gizmos.DrawLine(ListePoints[i], ListePoints[i + 1]); } if (ListePoints.Count > 0) { Gizmos.DrawLine(ListePoints[ListePoints.Count - 1], ListePoints[0]); } } }