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

API standards et tierces Java Discussion :

Symétrie de formes


Sujet :

API standards et tierces Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Avatar de seiryujay
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    950
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 950
    Par défaut Symétrie de formes
    Bonjour,

    Je suis en train de manipuler la classe AffineTransform afin de pourvoir déformer certaines figures.
    J'aimerais maintenant pouvoir faire une symétrie par rapport à un axe précis (pas forcément les axes de mon repère, mais plutôt une droite que je fournirai en paramètre)

    J'ai vu une constante dans AffineTransform qui s'appelle TYPE_FLIP, mais je ne sais pas où elle peut être utilisée...

    Quelqu'un saurait-il comment faire ou quelles classes utiliser?
    Merci

  2. #2
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 901
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 901
    Billets dans le blog
    54
    Par défaut
    On peut facilement faire des symétries simple (x, y, origine) avec la transformation scale (voir code).

    Pour pouvoir la généraliser avec n'importe quelle droite, il doit falloir utiliser une symétrie X et la concatener à une transformation rotate (l'angle d'entre cette droite et l'axe de abscisses) et probablement aussi une transformation translate* (l'origine de la rotation doit être sur cette droite).

    *Utiliser tout simplement la transformation rotate qui prend un centre de rotation.

    Pour pouvoir la généraliser avec n'importe quelle point, c'est encore plus simple : il faut utiliser la symétrie O et la concaténer à une transformation translate.

    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
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
     
    package test;
     
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
     
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2005</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */
    public final class RenderPanel extends JPanel {
      public RenderPanel() {
        super();
        setPreferredSize(new Dimension(600, 600));
      }
     
      /** {@inheritDoc}
       */
      @Override protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.setColor(Color.BLACK);
        g2d.drawLine(0, 300, 600, 300);
        g2d.drawLine(300, 0, 300, 600);
        g2d.drawString("(0,0)", 300, 300);
        g2d.drawString("X", 575, 300);
        g2d.drawString("Y", 300, 575);
        Rectangle rect = new Rectangle(100, 100, 75, 50);
        Ellipse2D ellipse = new Ellipse2D.Float(150, 200, 50, 75);
        g2d.translate(300, 300);
        graphics.setColor(Color.MAGENTA);
        graphics.drawLine(0, 0, 100, 0);
        graphics.drawLine(90, -10, 100, 0);
        graphics.drawLine(90, 10, 100, 0);
        graphics.drawLine(0, 0, 0, 100);
        graphics.drawLine( -10, 90, 0, 100);
        graphics.drawLine(10, 90, 0, 100);
        g2d.setColor(Color.BLUE);
        g2d.draw(rect);
        g2d.draw(ellipse);
        // Symétrie par rapport à X.
        AffineTransform symmetryX = AffineTransform.getScaleInstance(1, -1);
        g2d.setColor(Color.RED);
        g2d.draw(symmetryX.createTransformedShape(rect));
        g2d.draw(symmetryX.createTransformedShape(ellipse));
        // Symétrie par rapport à Y.
        AffineTransform symmetryY = AffineTransform.getScaleInstance( -1, 1);
        g2d.setColor(Color.GREEN);
        g2d.draw(symmetryY.createTransformedShape(rect));
        g2d.draw(symmetryY.createTransformedShape(ellipse));
        // Symétrie par rapport à l'origine.
        AffineTransform symmetryO = AffineTransform.getScaleInstance( -1, -1);
        g2d.setColor(Color.CYAN);
        g2d.draw(symmetryO.createTransformedShape(rect));
        g2d.draw(symmetryO.createTransformedShape(ellipse));
        graphics.translate( -300, -300);
      }
     
      public static void main(String[] args) {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new JScrollPane(new RenderPanel()));
        frame.pack();
        frame.setVisible(true);
      }
     
    }
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  3. #3
    Membre éclairé
    Avatar de seiryujay
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    950
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 950
    Par défaut
    Et comment tu ferais une symétrie axiale par rapport à n'importe quelle droite?
    Parce que j'ai du mal à voir comment y arriver...

  4. #4
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 901
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 901
    Billets dans le blog
    54
    Par défaut
    - Trouver l'angle entre cette droite et l'axe des X (cf cours de terminale ou de 1ère, c'est loin tout ca, bref les vecteurs directeurs et les nombres complexes*).
    - Créer une transformation rotate avec cet angle.
    - Créer une transformation translate avec le 1er points de la droite.
    - Créer la transformation scale symétrie X
    - Faire translation.concatenate(rotation.concatenate(symmetryX)) (à moins que ce ne soit symmetryX.concatenate(rotation.concatenate(translation))), à vérifier).

    Tu notteras que dans l'exemple c'est ce qui se passe :

    - le dessin de la symétrie X = rotation de 0° autour de 300x300 ( = translation à 300x300 + rotation de 0° autour de 0x0) + scale symétrie X.
    - le dessin de la symétrie Y = rotation de 90° autour de 300x300 ( = translation à 300x300 + rotation de 90° autour de 0x0) + scale symétrie X.

    Idem pour la symétrie centrale :

    - le dessin de la symétrie centrale = translation à 300x300 + scale symétrie O.

    * Sauf grosse bourde jusqu'à présent ce code marche pour moi :
    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
    25
    26
    27
    28
    29
    30
    31
    32
     
      /** Gets the angle between the given vector and the horizontal axis.
       * @param x1 The X coordinate of the first point.
       * @param y1 The Y coordinate of the first point.
       * @param x2 The X coordinate of the first point.
       * @param y2 The Y coordinate of the first point.
       * @return A value within the <CODE>[0, 2*PI[</CODE> range.
       * @exception ValueNotDefinedException If the vector's length is zero, the angle is not defined.
       * @since 1.1
       */
      public static double calculateAngle(double x1, double y1, double x2, double y2) throws ValueNotDefinedException {
        double a = (x2 - x1);
        double b = (y2 - y1);
        double length = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
        if (length == 0) {
          throw new ValueNotDefinedException();
        }
        double p = length; //Math.sqrt(a*a+b*b);
        double theta = Math.asin(Math.abs(b) / p);
        if (b < 0) {
          theta = -theta;
        }
        if (a < 0) {
          theta = Math.PI - theta;
        }
        // Rescale into [0, 2*PI[.
        theta %= 2 * Math.PI;
        if (theta < 0) {
          theta += 2 * Math.PI;
        }
        return theta;
      }
    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

  5. #5
    Rédacteur/Modérateur

    Avatar de bouye
    Homme Profil pro
    Information Technologies Specialist (Scientific Computing)
    Inscrit en
    Août 2005
    Messages
    6 901
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Nouvelle-Calédonie

    Informations professionnelles :
    Activité : Information Technologies Specialist (Scientific Computing)
    Secteur : Agroalimentaire - Agriculture

    Informations forums :
    Inscription : Août 2005
    Messages : 6 901
    Billets dans le blog
    54
    Par défaut
    Argh mais quel couillon je fais, j'avais oublie la transformation inverse pour revenir au repere d'origine (pff et dire que je m'arrachais mes derniers cheveux depuis hier soir) :

    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
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
     
    package test;
     
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
     
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2005</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */
    public final class RenderPanel
        extends JPanel {
      public RenderPanel() {
        super();
        setPreferredSize(new Dimension(1000, 1000));
      }
     
      /** Gets the angle between the given vector and the horizontal axis.
       * @param x1 The X coordinate of the first point.
       * @param y1 The Y coordinate of the first point.
       * @param x2 The X coordinate of the first point.
       * @param y2 The Y coordinate of the first point.
       * @return A value within the <CODE>[0, 2*PI[</CODE> range.
       * @exception IllegalArgumentException If the vector's length is zero, the angle is not defined.
       * @since 1.1
       */
      public static double calculateAngle(double x1, double y1, double x2, double y2)
          throws IllegalArgumentException {
        double a = (x2 - x1);
        double b = (y2 - y1);
        double length = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
        if (length == 0) {
          throw new IllegalArgumentException();
        }
        double p = length; //Math.sqrt(a*a+b*b);
        double theta = Math.asin(Math.abs(b) / p);
        if (b < 0) {
          theta = -theta;
        }
        if (a < 0) {
          theta = Math.PI - theta;
        }
        // Rescale into [0, 2*PI[.
        theta %= 2 * Math.PI;
        if (theta < 0) {
          theta += 2 * Math.PI;
        }
        return theta;
      }
     
     
      /** {@inheritDoc}
       */
      @Override protected void paintComponent(Graphics graphics) {
        super.paintComponent(graphics);
        Graphics2D g2d = (Graphics2D) graphics;
        g2d.scale(0.5, 0.5);
        g2d.translate(1000, 1000);
     
     
        g2d.setColor(Color.BLACK);
        g2d.drawLine( -1000, 0, 1000, 0);
        g2d.drawLine(0, -1000, 0, 1000);
        g2d.drawString("X", 575, 0);
        g2d.drawString("Y", 0, 575);
        graphics.setColor(Color.MAGENTA);
        g2d.drawString("(0,0)", 5, 10);
        graphics.drawLine(0, 0, 100, 0);
        graphics.drawLine(90, -10, 100, 0);
        graphics.drawLine(90, 10, 100, 0);
        graphics.drawLine(0, 0, 0, 100);
        graphics.drawLine( -10, 90, 0, 100);
        graphics.drawLine(10, 90, 0, 100);
        // Definition du segment de l'axe de symmetrie ici.
        /*
             int x1 = 100;
             int y1 = 400;
             int x2 = 500;
             int y2 = 400;
         */
        /*
              int x1 = 400;
              int y1 = 100;
              int x2 = 400;
              int y2 = 500;
         */
        int x1 = 400;
        int y1 = 100;
        int x2 = 100;
        int y2 = 400;
        graphics.setColor(Color.PINK);
        g2d.drawLine(x1, y1, x2, y2);
     
        Rectangle rect = new Rectangle(100, 100, 75, 50);
        Ellipse2D ellipse = new Ellipse2D.Float(150, 200, 50, 75);
        g2d.setColor(Color.BLUE);
        g2d.draw(rect);
        g2d.draw(ellipse);
        // Symétrie par rapport à X.
        AffineTransform symmetryX = AffineTransform.getScaleInstance(1, -1);
        g2d.setColor(Color.RED);
        g2d.draw(symmetryX.createTransformedShape(rect));
        g2d.draw(symmetryX.createTransformedShape(ellipse));
        // Symétrie par rapport à Y.
        AffineTransform symmetryY = AffineTransform.getScaleInstance( -1, 1);
        g2d.setColor(Color.GREEN);
        g2d.draw(symmetryY.createTransformedShape(rect));
        g2d.draw(symmetryY.createTransformedShape(ellipse));
        // Symétrie par rapport à l'origine.
        AffineTransform symmetryO = AffineTransform.getScaleInstance( -1, -1);
        g2d.setColor(Color.CYAN);
        g2d.draw(symmetryO.createTransformedShape(rect));
        g2d.draw(symmetryO.createTransformedShape(ellipse));
        double angle = calculateAngle(x1, y1, x2, y2);
        try {
          AffineTransform axisChange = AffineTransform.getTranslateInstance(x1, y1);
          axisChange.concatenate(AffineTransform.getRotateInstance(angle));
          AffineTransform axisChangeInv = axisChange.createInverse();
          Shape rectDest = axisChangeInv.createTransformedShape(rect);
          Shape ellipseDest = axisChangeInv.createTransformedShape(ellipse);
          /*
          g2d.setColor(Color.YELLOW);
          g2d.draw(rectDest);
          g2d.draw(ellipseDest);
          */
          //
          rectDest = symmetryX.createTransformedShape(rectDest);
          ellipseDest = symmetryX.createTransformedShape(ellipseDest);
          /*
          g2d.setColor(Color.ORANGE);
          g2d.draw(rectDest);
          g2d.draw(ellipseDest);
          */
          //
          rectDest = axisChange.createTransformedShape(rectDest);
          ellipseDest = axisChange.createTransformedShape(ellipseDest);
          g2d.setColor(Color.RED);
          g2d.draw(rectDest);
          g2d.draw(ellipseDest);
          //
        }
        // On peut pas creer la transformation inverse.
        catch (Exception e) {
        }
     
     
        g2d.translate( -1000, -1000);
        g2d.scale( -0.5, -0.5);
      }
     
      public static void main(String[] args) {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new JScrollPane(new RenderPanel()));
        frame.pack();
        frame.setVisible(true);
      }
    }
    Bon maintenant il s'agit voir s'il est possible de concatener les transformations entre elles (et dans quel ordre) pour eviter de produire des formes intermediaires.

    Ah oui et puisque je suis dans les mea-cupla... Symmetry O oui ca marche avec translate + scale + translate back (surtout ne pas oublier le retour au repere d'origine) mais c'est quand meme bien plus simple et rapide en faisant tout simplement un rotate de PI autour du centre de symmetrie (moi bete, moi stupide).

    Merci de penser au tag quand une réponse a été apportée à votre question. Aucune réponse ne sera donnée à des messages privés portant sur des questions d'ordre technique. Les forums sont là pour que vous y postiez publiquement vos problèmes.

    suivez mon blog sur Développez.

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning. ~ Rich Cook

Discussions similaires

  1. Réponses: 87
    Dernier message: 06/07/2011, 15h33
  2. Héritage entre Forms
    Par BarBal dans le forum Composants VCL
    Réponses: 7
    Dernier message: 29/08/2002, 17h44
  3. [Kylix] SIGSEGV 11 - creation form
    Par pram dans le forum EDI
    Réponses: 1
    Dernier message: 29/08/2002, 15h24
  4. [FORMS] Chemin des icones (intégré FAQ 150 QR)
    Par jerome62 dans le forum Forms
    Réponses: 2
    Dernier message: 30/07/2002, 08h32
  5. Form principale non visible au demarrage!!!!
    Par toufou dans le forum Composants VCL
    Réponses: 2
    Dernier message: 20/07/2002, 21h49

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