Comment traduire un objet graphique d'un point à un autre sans le modifier?
Question
Je suis en train de nombreuses heures pour traduire un chemin mathématique (une onde de péché) d'une position de coordonnées à l'autre .. mais soit je suis stupide ou quelque chose va mal.
I tryed cette
// Translate
for (int i=0; i < 300; i++) {
coordinatesX[i] = coordinatesX[i] + ( 80 - coordinatesX[i]);
coordinatesY[i] = coordinatesY[i] + (100 - coordinatesY[i]);
}
Où 80 et 100 sont la nouvelle position de coordonnées à laquelle je veux traduire mon objet. Je tryed cette
// Translate
for (int i=0; i < 300; i++) {
coordinatesX[i] = coordinatesX[i] + 80;
coordinatesY[i] = coordinatesY[i] + 100;
}
Mais tout semble fonctionner partiellement ou le travail que si l'onde a un certain angle? Je pense que je manque un peu de mathématiques à l'école. Je programmation en Java en utilisant la AndEngine. Peut être il y a des raccourcis à cette fonction elementar.
Le code complet:
// define newpath
float[] coordinatesX = new float[300];
float[] coordinatesY = new float[300];
// wave
for (int i=0; i<300; i++){
coordinatesX[i] = i;
coordinatesY[i] = (float)(20 * (Math.sin((-0.10 * coordinatesX[i]))));
System.out.println(coordinatesX[i]);
System.out.println(coordinatesY[i]);
//coordinatesX[i] = coordinatesX[i] + centerX;
//coordinatesY[i] = coordinatesX[i]+centerY;
}
// ROtate
for (int i=0; i<300; i++){
coordinatesX[i] = ((coordinatesX[i] * (float)Math.cos(-10)) - (coordinatesY[i] * (float)Math.sin(-10))) + coordinatesX[i];
coordinatesY[i] = (coordinatesX[i] * (float)Math.sin(-10)) + (coordinatesY[i] * (float)Math.cos(-10)) + coordinatesY[i];
}
// Translate
for (int i=0; i < 300; i++) {
coordinatesX[i] = coordinatesX[i]+ (200);
coordinatesY[i] = coordinatesY[i] + (300);
}
La solution
Voici un exemple de la façon de traduire une onde sinusoïdale. L'adapter en fonction de vos besoins.
package math;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
/**
* Curve translation example
* User: Michael
* Date: 1/10/12
* Time: 7:56 PM
*/
public class TranslationExample {
public static void main(String[] args) {
double length = 1.0;
int numPoints = 20;
List<Point2D.Double> curve = getCurve(length, numPoints);
System.out.println("before translation");
for (Point2D.Double point : curve) {
System.out.printf("%10.6f %10.6f\n", point.getX(), point.getY());
}
Point2D.Double delta = new Point2D.Double(3.0, 4.0);
curve = translateCurve(curve, delta);
System.out.println("after translation");
for (Point2D.Double point : curve) {
System.out.printf("%10.6f %10.6f\n", point.getX(), point.getY());
}
}
private static List<Point2D.Double> translateCurve(List<Point2D.Double> curve, Point2D.Double delta) {
List<Point2D.Double> translated = new ArrayList<Point2D.Double>();
for (Point2D.Double point : curve) {
translated.add(new Point2D.Double(point.getX()+delta.getX(), point.getY()+delta.getY()));
}
return translated;
}
private static List<Point2D.Double> getCurve(double length, int numPoints) {
List<Point2D.Double> points = new ArrayList<Point2D.Double>();
if ((length > 0.0) && (numPoints > 0)) {
double dx = length / numPoints;
double x = 0.0;
for (int i = 0; i < (numPoints - 1); ++i) {
points.add(new Point2D.Double(x, Math.sin(2.0*Math.PI*x/length)));
x += dx;
}
points.add(new Point2D.Double(length, Math.sin(2.0*Math.PI)));
}
return points;
}
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow