/* * Base des objets Bras et jambe */ using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Bonhomme02 { class BaseBJ : Peau { enum Articulation { Epaule, Coude, Poignet, Bassin = Epaule, Genou = Coude, Cheville = Poignet }; public Peau Haut, Millieux, Bas; double longueur; public BaseBJ() { } public BaseBJ(PictureBox Hebergeur, ref Peau parent, Peau bas, double longueur, double epaisseur, double angle) : this( Hebergeur, ref parent, longueur, epaisseur, angle) { this.Bas = bas; this.Bas.parent = this.Millieux; } public BaseBJ(PictureBox Hebergeur, ref Peau parent, double longueur, double epaisseur, double angle) : base(Hebergeur, ref parent, longueur / 2, epaisseur, angle) { this.parent = parent; this.longueur = longueur; this.Haut = this; this.Millieux = new Peau(Hebergeur, ref this.Haut, longueur / 2, epaisseur, angle); } public new void Afficher(IntPtr handle) { if(base.Visible) { Haut.Afficher(handle); Millieux.Afficher(handle); Bas.Afficher(handle); } } public new void Cacher(IntPtr handle) { Haut.Cacher(handle); Millieux.Cacher(handle); Bas.Cacher(handle); } public new void Afficher(Graphics gr) { if(base.Visible) { Haut.Afficher(gr); Millieux.Afficher(gr); Bas.Afficher(gr); } } public new void Cacher(Graphics gr) { Haut.Cacher(gr); Millieux.Cacher(gr); Bas.Cacher(gr); } public double TrouverAngleHaut(Point pt) { Point ptOrigine = parent.Coordonnee + parent.longueur; Complex vecteur = new Complex(pt.X- ptOrigine.X, pt.Y- ptOrigine.Y); return vecteur.Phase; } public double TrouverAngleMillieux(Point pt) { Point ptOrigine = Millieux.Coordonnee; Complex vecteur = new Complex(pt.X - ptOrigine.X, pt.Y - ptOrigine.Y); return vecteur.Phase; } public Point TouverPointMillieux(Point bas) { try { Point H = Haut.Coordonnee; //Point M = new Point() ; //Inconnue Point B = new Point(bas.X - H.X, bas.Y - H.Y); //On part du principe que H est 0;0 , on rapatrie donc B pour que se soit le cas Complex tt = new Complex(B.X, B.Y); double a = Math.Sqrt(4 * Math.Pow(Haut.longueur.ABS, 2) - Math.Pow(B.X, 2) - Math.Pow(B.Y, 2)); double b = Math.Sqrt(Math.Pow(B.X, 2) + Math.Pow(B.Y, 2)); double cooMX = (B.Y * a + B.X * b); cooMX /= 2 * b; double cooMY = -(B.X * a - B.Y * b); cooMY /= 2 * b; Complex t = new Complex(cooMX, cooMY); cooMX += H.X; cooMY += H.Y; int xx = (int)Math.Round(cooMX); int yy = (int)Math.Round(cooMY); if(Double.IsNaN(a) || Double.IsNaN(b)) { return Millieux.Coordonnee; } else return new Point(xx, yy); } catch(Exception e) { Console.WriteLine("Erreur Point millieux : " + e); return new Point(0, 0); } } public void BougerBas(int X, int Y) { Point nouveauPtBas = new Point(Bas.X + X, Bas.Y + Y); Point nouveauPtMillieux = TouverPointMillieux(nouveauPtBas); if(nouveauPtMillieux.X != 0 || nouveauPtMillieux.Y != 0) { Haut.Angle = TrouverAngleHaut(nouveauPtMillieux); Millieux.Angle = TrouverAngleMillieux(nouveauPtBas); } } public void BougerXHaut(int x, ref Bonhomme bonhomme) { Point oldPtBas = Bas.Coordonnee; Vecteur oldVecteurHautBas = new Vecteur(Bas.Coordonnee, Haut.Coordonnee); Vecteur newVecteurHautBas; Point newCooHaut = TrouverPointSurCerlceSelonX(Bas.Coordonnee, oldVecteurHautBas.ABS, Haut.Coordonnee.X + x); bonhomme.Coordonnee = new Point(newCooHaut.X, (int)Math.Round(newCooHaut.Y - bonhomme.longueur.ABS)); Point nouveauPtMillieux = TouverPointMillieux(oldPtBas); if (nouveauPtMillieux.X != 0 || nouveauPtMillieux.Y != 0) { Haut.Angle = TrouverAngleHaut(nouveauPtMillieux); Millieux.Angle = TrouverAngleMillieux(oldPtBas); } } public void SetAngle(double h, double m, double b) { Haut.Angle = h; Millieux.Angle = m; Bas.Angle = b; } public Point TrouverPointSurCerlceSelonX(Point centre, double rayon, int nouveauX) { double racine = Math.Sqrt(rayon * rayon - nouveauX * nouveauX + 2 * centre.X * nouveauX - centre.X * centre.X); double y = -(racine - centre.Y); return new Point(nouveauX, (int)Math.Round(y)); } } }