Drag and Drop d'un polygone

Résolu
tintin3788 Messages postés 4 Date d'inscription jeudi 21 février 2008 Statut Membre Dernière intervention 6 août 2009 - 5 août 2009 à 16:20
tintin3788 Messages postés 4 Date d'inscription jeudi 21 février 2008 Statut Membre Dernière intervention 6 août 2009 - 6 août 2009 à 08:55
Je me permet de poster car je suis face a un mur!

Voila je fais une rotation d'un rectangle d'un angle donné autour de son centre de gravité, lorsque celui ci se trouve a x=0 Il n'y a aucun problème, mais lorsque x augmente, ma coordonnée sur y augmente aussi.
Ainsi l'écart entre la figure d'origine et la transformée s'agrandit au fur et a mesure que x augmente. Je n'ai pas le problème si la coordonnée sur Y augmente.

Voila typiquement ce qui m'arrive.



Je ne vois pas l'erreur, peut être du aux arrondis, mais pourquoi n'aurai-je pas le même souci sur Y?

public void draw(Graphics g) {

Color c = g.getColor();
g.setColor(color);

Point p1 = new Point(rect.x, rect.y);
Point p2 = new Point(rect.x + rect.width, rect.y);
Point p3 = new Point(rect.x + rect.width, rect.y + rect.height);
Point p4 = new Point(rect.x, rect.y + rect.height);

Point p12 = new Point();
Point p22 = new Point();
Point p32 = new Point();
Point p42 = new Point();

p12.x = (int) ((p1.x - (rect.x + rect.width / 2)) * Math.cos(angle)
- (p1.y - (rect.y + rect.height / 2)) * Math.sin(angle) + (rect.x + rect.width / 2));
p12.y = (int) ((p1.y - (rect.y + rect.height / 2)) * Math.cos(angle)
+ (p1.x - (rect.x + rect.width / 2) * Math.sin(angle)) + (rect.y + rect.height / 2));

p22.x = (int) ((p2.x - (rect.x + rect.width / 2)) * Math.cos(angle)
- (p2.y - (rect.y + rect.height / 2)) * Math.sin(angle) + (rect.x + rect.width / 2));
p22.y = (int) ((p2.y - (rect.y + rect.height / 2)) * Math.cos(angle)
+ (p2.x - (rect.x + rect.width / 2) * Math.sin(angle)) + (rect.y + rect.height / 2));

p32.x = (int) ((p3.x - (rect.x + rect.width / 2)) * Math.cos(angle)
- (p3.y - (rect.y + rect.height / 2)) * Math.sin(angle) + (rect.x + rect.width / 2));
p32.y = (int) ((p3.y - (rect.y + rect.height / 2)) * Math.cos(angle)
+ (p3.x - (rect.x + rect.width / 2) * Math.sin(angle)) + (rect.y + rect.height / 2));

p42.x = (int) ((p4.x - (rect.x + rect.width / 2)) * Math.cos(angle)
- (p4.y - (rect.y + rect.height / 2)) * Math.sin(angle) + (rect.x + rect.width / 2));
p42.y = (int) ((p4.y - (rect.y + rect.height / 2)) * Math.cos(angle)
+ (p4.x - (rect.x + rect.width / 2) * Math.sin(angle)) + (rect.y + rect.height / 2));

Polygon poly = new Polygon();
Polygon poly2 = new Polygon();

poly.addPoint(p12.x, p12.y);
poly.addPoint(p22.x, p22.y);
poly.addPoint(p32.x, p32.y);
poly.addPoint(p42.x, p42.y);

poly2.addPoint(p1.x, p1.y);
poly2.addPoint(p2.x, p2.y);
poly2.addPoint(p3.x, p3.y);
poly2.addPoint(p4.x, p4.y);


g.fillPolygon(poly2);
g.fillPolygon(poly);
g.setColor(c);


}

2 réponses

uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 9
5 août 2009 à 21:01
g.fillPolygon(poly2);
((Graphics2D) g).rotate(angle, rect.x + rect.width / 2, rect.y + rect.height / 2);
g.setColor(Color.GRAY);
g.fillPolygon(poly2);
//g.fillPolygon(poly);
3
tintin3788 Messages postés 4 Date d'inscription jeudi 21 février 2008 Statut Membre Dernière intervention 6 août 2009
6 août 2009 à 08:55
AH oui suis-je bête! Merci beaucoup pour cette rapide réponse!
0
Rejoignez-nous