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.
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.
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); } } }
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++; }
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager