有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java不绕轨道旋转椭圆

我有一个自定义椭圆,它有一个旋转方法:

 import com.badlogic.gdx.math.Vector2;

public class BetterEllipse {
    float centerx,centery,width,height;
    int pointAmount;
    //float vector array
    Vector2[] border;
    BetterEllipse(float cneterX,float centerY,float width_, float height_)
    {
        centerx=cneterX; centery=centerY;
        width=width_; height=height_;
        pointAmount=360;
        border=new Vector2[pointAmount];
        for(int n=0;n<pointAmount;n++)
        {
            double x=cneterX +(width_/2*Math.cos(n));
            double y=centerY+  (height_/2*Math.sin(n));
            border[n]=new Vector2((float)x,(float)y);

        }
    }
    /**angle > 0 - rotates counter-clock, angle < 0 - rotate clock*/
    public void rotate(float angle)
    {
        for(Vector2 point:border)
        {
            float newX=(float) ((point.x)*Math.cos(angle)-(point.y)*Math.sin(angle));
            float newY=(float) ((point.x)*Math.sin(angle)+(point.y)*Math.cos(angle));
            //after
            point.x=newX;
            point.y=newY;   
        }


    }

}

椭圆会旋转,但也会围绕点0,0旋转。我想我必须把centerx,centery放在rotate()中,但不知道该怎么做


共 (1) 个答案

  1. # 1 楼答案

    减法后旋转

    center = 1/2 * (centerx + centery)

    然后再次将其添加到结果中