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

2D Java Discussion :

Dessiner Courbe en zigzag


Sujet :

2D Java

  1. #1
    Membre habitué
    Homme Profil pro
    Ingénieur Informatique et Réseaux
    Inscrit en
    Avril 2011
    Messages
    232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur Informatique et Réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 232
    Points : 182
    Points
    182
    Par défaut Dessiner Courbe en zigzag
    Bonjour,

    J'ai du mal à créer un algorithme permettant de calculer les points d'une courbe en zigzag (voir pièce jointe). J'utilise QuadCurve2D pour dessiner mes autres droites et j'arrive pas dessiner celle là.

    Quelqu'un à une idée?

    Merci.
    Images attachées Images attachées  

  2. #2
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Janvier 2010
    Messages
    312
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Janvier 2010
    Messages : 312
    Points : 533
    Points
    533
    Par défaut
    Bonjour,

    Avec Quadric2D je ne sais pas mais je me suis amusé à le faire avec Path2D

    j'ai fait un truc qui est loin d'être complet il manque plein de tests notamment pour déterminer l'angle de la rotation. mon exemple ne fonctionne que pour un angle < PI.


    le principe :
    les coordonnées des 2 points (x1,y1 et x2,y2) définissent un segment à tracer

    A partir du segment à tracer, tu determines l'angle de ce segment par rapport à Ox

    ensuite tu 'translate' le repère jusque x1,y1

    tu 'rotate' le repère de l'angle trouvé

    tu traces la fonction y=amplitude*cos(omega*x) ou x varie entre 0 (origine du nouveau repere cad x1,y1) et norme du segment ( sqrt(x2-x1)^2+(y2-y1)^2) )

    ou ampltude et omega sont 2 paramètre que l'on peut définir.



    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
     
    public class Quadric extends JFrame implements ActionListener {
     
    	MonPanel  panel;
    	JPanel menu = new JPanel();
    	double x1,x2,y1,y2,amplitude,periode,omega;
     
    	public Quadric() {
     
    		super();
    		this.setResizable (false);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		x1=30;
    		x2=200;
    		y1=50;
    		y2=200;
    		amplitude = 15;
    		periode = 50;
    		omega = 2*Math.PI/periode;
    		panel = new MonPanel(x1,y1,x2,y2,amplitude);
    		add(panel);
    		pack();
     
     
    		setVisible(true);
    	}
     
    	public static void main(String[] args) {
    		Quadric tl = new Quadric();
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
     
    	}
     
     
     
     
    	public class MonPanel extends JPanel {
     
    		public MonPanel(double x1,double y1,double x2,double y2,double amplitude) {
    			setPreferredSize(new Dimension(600,600));	
    		}
     
    		public void paintComponent(Graphics g) {
    			Graphics2D g2 = (Graphics2D)g.create();
    			AffineTransform af = g2.getTransform();
    			g2.translate(x1,y1);
    			g2.rotate(  Math.acos(  (x2-x1)/Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2))  )  );
    			Path2D.Double li = new Path2D.Double();
    			li.moveTo(0,0);
    			double x=0;
    			while (x<= Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2)) ) {
    				li.lineTo(x,amplitude*Math.sin(x*omega));
    				x=x+0.1;
    			}
    			g2.draw(li);
    			g2.setTransform(af);
    		}
    	}
    }

  3. #3
    Membre habitué
    Homme Profil pro
    Ingénieur Informatique et Réseaux
    Inscrit en
    Avril 2011
    Messages
    232
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur Informatique et Réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 232
    Points : 182
    Points
    182
    Par défaut
    En fait j'ai fait autrement:

    Je calcul le vecteur directeur et le vecteur orthonormal entre 2 points.
    De là je calcul un point à une distance "interval" du point d'origine sur la droite ente les 2 points, puis calcul la pointes du premier zigzag à une distance "lineWidth" grâce au vecteur orthonormal.

    Ça sera plus parlant avec le code:
    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
     
                Point tailPoint = new Point(0,0);
                Point headPoint = new Point(100,100);
                float interval = 5.0f;
                float lineWidth = 5.0f;
     
                float[] vecLine = new float[2]; // vecteur parallèle à la droite
                float[] vecLeft = new float[2]; // vecteur perpendiculaire à la droite
                float fLength; // longueur de la droite
    //Calcul des informations
                fLength = (float) Math.sqrt((tailPoint.x - headPoint.x) * (tailPoint.x - headPoint.x) + (tailPoint.y - headPoint.y) * (tailPoint.y - headPoint.y));
                vecLine[ 0] = (float) (headPoint.x - tailPoint.x) / fLength;
                vecLine[ 1] = (float) (headPoint.y - tailPoint.y) / fLength;
                vecLeft[ 0] = -vecLine[ 1];
                vecLeft[ 1] = vecLine[ 0];
    //Calcul des points du crénelage
                Point lastPoint = tailPoint;
                int i = 0;
                float length = 0;
                while (length < fLength - lineWidth) {
                    float bX = (tailPoint.x + (headPoint.x - tailPoint.x) * interval / fLength);
                    float bY = (tailPoint.y + (headPoint.y- tailPoint.y) * interval / fLength);
                    int pointX;
                    int pointY;
                    if ((i % 2) == 0) {
                        pointX = (int) (bX + lineWidth * vecLeft[0]);
                        pointY = (int) (bY + lineWidth * vecLeft[1]);
                    } else {
                        pointX = (int) (bX - lineWidth * vecLeft[0]);
                        pointY = (int) (bY - lineWidth * vecLeft[1]);
                    }
                    g2D.drawLine(lastPoint.x, lastPoint.y, pointX, pointY);
                    length = (float) Math.sqrt((tailPoint.x - bX) * (tailPoint.x - bX) + (tailPoint.y - bY) * (tailPoint.y - bY));
                    lastPoint = new Point(pointX, pointY);
                    interval = interval + lineWidth;
                    i++;
                }

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

Discussions similaires

  1. dessiner courbe C
    Par Ontolingua dans le forum Débuter
    Réponses: 1
    Dernier message: 04/04/2008, 07h20
  2. Composant graphique pour dessiner courbes
    Par stephane.julien dans le forum C#
    Réponses: 11
    Dernier message: 02/11/2007, 16h26
  3. Réponses: 1
    Dernier message: 29/03/2006, 20h43
  4. [C#] Dessiner un graphique en courbes
    Par FoxDeltaSierra dans le forum ASP.NET
    Réponses: 11
    Dernier message: 28/07/2005, 16h16
  5. Dessiner des courbes
    Par LE NEINDRE dans le forum Balisage (X)HTML et validation W3C
    Réponses: 5
    Dernier message: 23/06/2005, 10h29

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