TP-modelisation-geometrique/TP2/Assets/Scripts/CalculHodographe.cs
2023-06-21 20:37:15 +02:00

160 lines
6.4 KiB
C#
Executable file

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CalculHodographe : MonoBehaviour
{
// Nombre de subdivision dans l'algo de DCJ
public int NombreDeSubdivision = 3;
// Liste des points composant la courbe de l'hodographe
private List<Vector3> ListePoints = new List<Vector3>();
// Donnees i.e. points cliqués
public GameObject Donnees;
public GameObject particle;
//////////////////////////////////////////////////////////////////////////
// fonction : DeCasteljauSub //
// semantique : renvoie la liste des points composant la courbe //
// approximante selon un nombre de subdivision données //
// params : - List<float> X : abscisses des point de controle //
// - List<float> Y : odronnees des point de controle //
// - int nombreDeSubdivision : nombre de subdivision //
// sortie : //
// - (List<float>, List<float>) : liste des abscisses et liste //
// des ordonnées des points composant la courbe //
//////////////////////////////////////////////////////////////////////////
(List<float>, List<float>) DeCasteljauSub(List<float> X, List<float> Y, int nombreDeSubdivision)
{
if (nombreDeSubdivision == 0)
{ // condition de terminaison
return (X, Y);
}
else
{ // récurrence
List<float> x_gauche = new List<float>();
List<float> y_gauche = new List<float>();
List<float> x_droite = new List<float>();
List<float> y_droite = new List<float>();
// copie de X et Y pour la subdivision
int n = X.Count;
List<float> nevX = new List<float>(X);
List<float> nevY = new List<float>(Y);
// ajout des premiers points
x_gauche.Add(X[0]);
y_gauche.Add(Y[0]);
for (int i = n - 1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
nevX[j] = 0.5f * nevX[j] + 0.5f * nevX[j + 1];
nevY[j] = 0.5f * nevY[j] + 0.5f * nevY[j + 1];
}
// ajouts de la subdiv de gauche
x_gauche.Add(nevX[0]);
y_gauche.Add(nevY[0]);
}
// ajout de la subdiv de droite
for (int i = 0; i < n; i++)
{
x_droite.Add(nevX[i]);
y_droite.Add(nevY[i]);
}
// déclaration des nouvelles listes de points
List<float> x_new_gauche = new List<float>();
List<float> y_new_gauche = new List<float>();
List<float> x_new_droite = new List<float>();
List<float> y_new_droite = new List<float>();
// appels récurrents
(x_new_gauche, y_new_gauche) = DeCasteljauSub(x_gauche, y_gauche, nombreDeSubdivision - 1);
(x_new_droite, y_new_droite) = DeCasteljauSub(x_droite, y_droite, nombreDeSubdivision - 1);
// on enlève le point en commun dans une des deux listes
x_new_droite.RemoveAt(0);
y_new_droite.RemoveAt(0);
return (x_new_gauche.Concat(x_new_droite).ToList(), y_new_gauche.Concat(y_new_droite).ToList());
}
}
//////////////////////////////////////////////////////////////////////////
// fonction : Hodographe //
// semantique : renvoie la liste des vecteurs vitesses entre les paires //
// consécutives de points de controle //
// approximante selon un nombre de subdivision données //
// params : - List<float> X : abscisses des point de controle //
// - List<float> Y : odronnees des point de controle //
// - float Cx : offset d'affichage en x //
// - float Cy : offset d'affichage en y //
// sortie : //
// - (List<float>, List<float>) : listes composantes des //
// vecteurs vitesses sous la forme (Xs,Ys) //
//////////////////////////////////////////////////////////////////////////
(List<float>, List<float>) Hodographe(List<float> X, List<float> Y, float Cx = 1.5f, float Cy = 0.0f)
{
List<float> XHodo = new List<float>();
List<float> YHodo = new List<float>();
for (int i = 0; i < X.Count - 1; i++)
{
XHodo.Add(X[i + 1] - X[i] + Cx);
YHodo.Add(Y[i + 1] - Y[i] + Cy);
}
return DeCasteljauSub(XHodo, YHodo, 3);
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////// NE PAS TOUCHER //////////////////////////////
//////////////////////////////////////////////////////////////////////////
void Start()
{
Instantiate(particle, new Vector3(1.5f, -4.0f, 0.0f), Quaternion.identity);
}
void Update()
{
// if (Input.GetKeyDown(KeyCode.Return))
// {
ListePoints.Clear();
var ListePointsCliques = GameObject.Find("Donnees").GetComponent<Points>();
if (ListePointsCliques.X.Count > 1)
{
List<float> XSubdivision = new List<float>();
List<float> YSubdivision = new List<float>();
List<float> dX = new List<float>();
List<float> dY = new List<float>();
(dX, dY) = Hodographe(ListePointsCliques.X, ListePointsCliques.Y);
(XSubdivision, YSubdivision) = DeCasteljauSub(dX, dY, NombreDeSubdivision);
for (int i = 0; i < XSubdivision.Count; ++i)
{
ListePoints.Add(new Vector3(XSubdivision[i], -4.0f, YSubdivision[i]));
}
}
// }
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
for (int i = 0; i < ListePoints.Count - 1; ++i)
{
Gizmos.DrawLine(ListePoints[i], ListePoints[i + 1]);
}
}
}