IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Multiple rotation de text en GDI


Sujet :

C#

  1. #1
    Membre à l'essai
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    18
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2019
    Messages : 18
    Points : 18
    Points
    18
    Par défaut Multiple rotation de text en GDI
    Bonsoir,

    J'ai besoin de dessiner 3 string (à 120°) autour d'un cercle. De plus, ces string tournent autour de celui-ci tout en restant horizontales pour être lisible facilement.
    J'ai créé une fonction qui positionne une string et je comptais répéter l'opération 3 fois. Mais ça ne fonctionne pas comme prévu je n'arrive pas à intégrer le décalage de 120° , les 2 dernières tournent autour de la première car les rotations s'additionnent.
    Voici ma fonction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
            private void DrawText(Graphics myGraphics,int AngularPosition,string StrTmp, Font FntVerin,float VerticalTranslation)
            {
                myGraphics.TranslateTransform(0, -VerticalTranslation);
                myGraphics.RotateTransform(-Rotation);
                SizeF stringSize = new SizeF();
                int stringWidth = 100;
     
                StringFormat newStringFormat = new StringFormat();
                newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
     
                stringSize = myGraphics.MeasureString(StrTmp, FntVerin, stringWidth, newStringFormat);
     
                Point PtCircle = new Point();
                PtCircle.X = (int)(Math.Cos(Rotation / 360.0 * Math.PI));
                PtCircle.Y = (int)(Math.Sin(Rotation / 360.0 * Math.PI));
                float DeltaX = 0.0f;
                if (Rotation < 90) { DeltaX = (float)(Math.Sin(Rotation / 360.0 * Math.PI) + (stringSize.Height / 2)); }
                else if (Rotation < 180) { DeltaX = (float)(Math.Sin((180.0 - Rotation) / 360.0 * Math.PI) + (stringSize.Height / 2)); }
                else if (Rotation < 270) { DeltaX = (float)(Math.Sin((Rotation - 180.0) / 360.0 * Math.PI) - (stringSize.Height / 2)); }
                else if (Rotation < 360) { DeltaX = (float)(Math.Sin((180.0 - (Rotation - 180.0)) / 360.0 * Math.PI) - (stringSize.Height / 2)); }
                myGraphics.DrawString(StrTmp, FntVerin, Brushes.Blue, PtCircle.X - stringSize.Height / 2 + DeltaX, PtCircle.Y - stringSize.Width / 2);
     
                myGraphics.TranslateTransform(0, VerticalTranslation);
            }
    Merci pour votre aide

  2. #2
    Membre actif
    Homme Profil pro
    libre
    Inscrit en
    Juin 2019
    Messages
    205
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : libre

    Informations forums :
    Inscription : Juin 2019
    Messages : 205
    Points : 292
    Points
    292
    Par défaut
    La rotation n'est pas compliquée a réaliser voir l'exemple suivant le point ptCenter c'est auquel on pivote et ptStart est le point initial du texte .



    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    void DrawText(Graphics myGraphics,string StrTmp,int Ang, Font FntVerin,Point ptCenter, Point ptStart)
    {
     
                SizeF stringSize = new SizeF();
     
                StringFormat newStringFormat = new StringFormat();
                stringSize = myGraphics.MeasureString(StrTmp, FntVerin);
     
                float s=(float)Math.Sin(Ang / 180.0 * Math.PI);
                float c=(float)Math.Cos(Ang / 180.0 * Math.PI);
                Point pt= new Point(ptStart.X-ptCenter.X, ptStart.Y-ptCenter.Y);
                PointF PtOut =new PointF();
                PtOut.X = pt.X * c - pt.Y * s + ptCenter.X;
                PtOut.Y = pt.X * s + pt.Y * c + ptCenter.Y;
     
                PointF txtPos = new PointF();
                txtPos.X = PtOut.X - stringSize.Width / 2;
                txtPos.Y = PtOut.Y - stringSize.Height / 2;
                myGraphics.DrawString(StrTmp, FntVerin, Brushes.Blue,txtPos);
                myGraphics.DrawLine(Pens.Red, ptCenter,PtOut);	
    }

  3. #3
    Membre à l'essai
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2019
    Messages
    18
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Février 2019
    Messages : 18
    Points : 18
    Points
    18
    Par défaut
    Merci quand même mais avec ton code mon problème reste le même, si je l'appelle 2 fois la deuxième fois l'angle est cumulé avec la première fois.
    Dans mon code j'ai aussi une translation pour que le texte suive le bord du cercle pendant la rotation en tournant en sens inverse pour pouvoir rester horizontal.
    J'ai résolu mon problème, j'avais oublié de faire une rotation inverse pour annuler une précédente rotation ce qui fait qu'elle se rajoutait à chaque passage dans la boucle.

  4. #4
    Membre actif
    Homme Profil pro
    libre
    Inscrit en
    Juin 2019
    Messages
    205
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : libre

    Informations forums :
    Inscription : Juin 2019
    Messages : 205
    Points : 292
    Points
    292
    Par défaut
    Votre code n'était pas claire et manque d'infos et dépendant d'autres paramètres externes sur son fonctionnement donc j'ai donné un réponse basique sur la rotation des points.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Afficher texte en GDI+
    Par ChicoLau dans le forum C++Builder
    Réponses: 0
    Dernier message: 21/08/2007, 15h29
  2. [Direct3D] Rotation de texte
    Par tnarol dans le forum DirectX
    Réponses: 4
    Dernier message: 26/01/2007, 18h13
  3. Tcanvas Rotation de texte et impression
    Par kilog dans le forum Delphi
    Réponses: 2
    Dernier message: 21/08/2006, 10h42
  4. [TLabel] rotation du text dans un label
    Par Bourak dans le forum Delphi
    Réponses: 1
    Dernier message: 16/05/2006, 18h08
  5. Rotation de texte et impression
    Par rizom dans le forum Langage
    Réponses: 6
    Dernier message: 16/11/2004, 20h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo