如何将图形对象从一个点转换到另一点而不修改它?

发布于 2024-12-26 02:39:26 字数 1614 浏览 0 评论 0原文

我花了很多时间尝试将数学路径(正弦波)从一个坐标位置转换到另一个坐标位置..但要么我很愚蠢,要么出了问题。

我尝试了

    // Translate 
    for (int i=0; i < 300; i++) {
        coordinatesX[i] = coordinatesX[i] + ( 80 - coordinatesX[i]);
        coordinatesY[i] = coordinatesY[i] + (100 - coordinatesY[i]);
    }

其中 80 和 100 是我想要将对象平移到的新坐标位置。 我尝试了这个

    // Translate 
    for (int i=0; i < 300; i++) {
        coordinatesX[i] = coordinatesX[i] + 80;
        coordinatesY[i] = coordinatesY[i] + 100;
    }

,但似乎一切都部分有效,或者只有当波有一定角度时才有效? 我想我在学校错过了一些数学知识。我正在使用 AndEngine 进行 Java 编程。这个基本功能可能有一些快捷方式。

整个代码:

    // 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);
    }

I'm trying for many hours to translate a mathematical path (a sin wave) from one coordinate position to another.. but either i'm stupid or something goes wrong.

I tryed this

    // Translate 
    for (int i=0; i < 300; i++) {
        coordinatesX[i] = coordinatesX[i] + ( 80 - coordinatesX[i]);
        coordinatesY[i] = coordinatesY[i] + (100 - coordinatesY[i]);
    }

Where 80 and 100 are the new coordinate position to which i wanna to translate my object.
I tryed this

    // Translate 
    for (int i=0; i < 300; i++) {
        coordinatesX[i] = coordinatesX[i] + 80;
        coordinatesY[i] = coordinatesY[i] + 100;
    }

But all seems to partially work or work only if the wave has some angle ?
I think I was missing some mathematics at school. I'm programming in Java using the AndEngine . May be there are some shortcuts to this elementar function.

The whole code:

    // 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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠栀 2025-01-02 02:39:26

这是如何平移正弦波的示例。根据您的需要进行调整。

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;
    }
}

Here's an example of how to translate a sine wave. Adapt it according to your needs.

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文