Java 如何使用360度旋转在2D中获得正确的方向
我试图得到一个物体旋转过快或最短的最近方向 基本上,这个想法是给物体一个期望的方向,并且必须以尽可能短的方式朝这个方向旋转 我有以下代码:Java 如何使用360度旋转在2D中获得正确的方向,java,math,Java,Math,我试图得到一个物体旋转过快或最短的最近方向 基本上,这个想法是给物体一个期望的方向,并且必须以尽可能短的方式朝这个方向旋转 我有以下代码: rotationChangeAmount = (this.getDirection(getRotation(), mDestinationRotation)) ? -rotationChangeAmount : rotationChangeAmount; 使用以下功能: private boolean getDirection(double targetR
rotationChangeAmount = (this.getDirection(getRotation(), mDestinationRotation)) ? -rotationChangeAmount : rotationChangeAmount;
使用以下功能:
private boolean getDirection(double targetRotation, double value)
{
//Max 100 max (360)
//Target 60
//Value 30
//Value - Max > Target - Max
if(Math.abs(targetRotation - 360.0) > Math.abs(value - 360.0))
{
return false;
}
return true;
}
从逻辑上考虑,如果值和目标旋转之间的距离大于180,则顺时针旋转,如果小于180,则逆时针旋转,假设目标<原始 假设我最初有270个,我想要180个。abs(270-180)是90,所以我将逆时针旋转,我们粗略地看一下就知道这是正确的 如果我一开始有270,我想要45,那么(270-45)=225,所以我会顺时针旋转135度(我们也知道这是正确的) 若我的初始值是45,我想要300,那个么abs(45-300)=255,所以我会逆时针旋转105度(因为初始值小于目标值) 最后,如果我一开始有45,我想要90,那么abs(45-90)=45,我将顺时针旋转45度 因此,要在此逻辑基础上构建您的功能:
private double getChange(double target, double original){
if (target < original){
if (Math.abs(original - target) > 180)
return Math.abs((360 - original) + target);
else
return (-1 * Math.abs(target - original);
}
else{
if (Math.abs(target - original) > 180)
return Math.abs( (360 - target) + original);
else
return (-1 * Math.abs(original - target);
}
}
private double getChange(双重目标,双重原始){
如果(目标<原始){
如果(数学绝对值(原始-目标)>180)
返回Math.abs((360-原始)+目标);
其他的
返回(-1*Math.abs(目标-原始);
}
否则{
如果(数学abs(目标-原始)>180)
返回Math.abs((360-目标)+原始);
其他的
返回(-1*Math.abs(原始-目标);
}
}
从逻辑上考虑,如果值与目标旋转之间的距离大于180,则顺时针旋转,如果小于180,则逆时针旋转,假设目标<原始旋转
假设我一开始有270,我想要180。abs(270-180)是90,所以我会逆时针旋转,我们粗略地看一下就知道这是正确的
如果我一开始有270,我想要45,那么(270-45)=225,所以我会顺时针旋转135度(我们也知道这是正确的)
若我的初始值是45,我想要300,那个么abs(45-300)=255,所以我会逆时针旋转105度(因为初始值小于目标值)
最后,如果我一开始有45,我想要90,那么abs(45-90)=45,我将顺时针旋转45度
因此,要在此逻辑基础上构建您的功能:
private double getChange(double target, double original){
if (target < original){
if (Math.abs(original - target) > 180)
return Math.abs((360 - original) + target);
else
return (-1 * Math.abs(target - original);
}
else{
if (Math.abs(target - original) > 180)
return Math.abs( (360 - target) + original);
else
return (-1 * Math.abs(original - target);
}
}
private double getChange(双重目标,双重原始){
如果(目标<原始){
如果(数学绝对值(原始-目标)>180)
返回Math.abs((360-原始)+目标);
其他的
返回(-1*Math.abs(目标-原始);
}
否则{
如果(数学abs(目标-原始)>180)
返回Math.abs((360-目标)+原始);
其他的
返回(-1*Math.abs(原始-目标);
}
}
通过TDD完成:
package rotation;
import static org.junit.Assert.*;
import org.junit.Test;
public class RotationTest {
public enum Direction { RIGHT, LEFT, EITHER }
public Direction getDirection(double current, double target) {
validate(current);
validate(target);
double alwaysLarger = current < target ? (current + 360) : current;
double gap = alwaysLarger - target;
return gap > 180
? Direction.RIGHT
: (gap == 180 ? Direction.EITHER : Direction.LEFT);
}
private void validate(double degrees) {
if (degrees < 0 || degrees > 360) {
throw new IllegalStateException();
}
}
@Test
public void test() {
assertEquals(Direction.LEFT, getDirection(90, 1));
assertEquals(Direction.LEFT, getDirection(90, 359));
assertEquals(Direction.LEFT, getDirection(90, 271));
assertEquals(Direction.EITHER, getDirection(90, 270));
assertEquals(Direction.RIGHT, getDirection(90, 269));
assertEquals(Direction.RIGHT, getDirection(90, 180));
}
}
包装旋转;
导入静态org.junit.Assert.*;
导入org.junit.Test;
公共类轮换测试{
公共枚举方向{右、左或}
公共方向(双流、双目标){
验证(当前);
验证(目标);
双AlwaysRanger=当前<目标?(当前+360):当前;
双间隙=alwaysLarger-目标;
返回间隙>180
方向,对
:(间隙==180?方向。任意一个:方向。左);
}
私有void验证(双度){
如果(度<0 | |度>360){
抛出新的非法状态异常();
}
}
@试验
公开无效测试(){
assertEquals(Direction.LEFT,getDirection(90,1));
assertEquals(Direction.LEFT,getDirection(90359));
assertEquals(Direction.LEFT,getDirection(90271));
assertEquals(Direction.other,getDirection(90270));
assertEquals(Direction.RIGHT,getDirection(90269));
assertEquals(Direction.RIGHT,getDirection(90180));
}
}
通过TDD完成:
package rotation;
import static org.junit.Assert.*;
import org.junit.Test;
public class RotationTest {
public enum Direction { RIGHT, LEFT, EITHER }
public Direction getDirection(double current, double target) {
validate(current);
validate(target);
double alwaysLarger = current < target ? (current + 360) : current;
double gap = alwaysLarger - target;
return gap > 180
? Direction.RIGHT
: (gap == 180 ? Direction.EITHER : Direction.LEFT);
}
private void validate(double degrees) {
if (degrees < 0 || degrees > 360) {
throw new IllegalStateException();
}
}
@Test
public void test() {
assertEquals(Direction.LEFT, getDirection(90, 1));
assertEquals(Direction.LEFT, getDirection(90, 359));
assertEquals(Direction.LEFT, getDirection(90, 271));
assertEquals(Direction.EITHER, getDirection(90, 270));
assertEquals(Direction.RIGHT, getDirection(90, 269));
assertEquals(Direction.RIGHT, getDirection(90, 180));
}
}
包装旋转;
导入静态org.junit.Assert.*;
导入org.junit.Test;
公共类轮换测试{
公共枚举方向{右、左或}
公共方向(双流、双目标){
验证(当前);
验证(目标);
双AlwaysRanger=当前<目标?(当前+360):当前;
双间隙=alwaysLarger-目标;
返回间隙>180
方向,对
:(间隙==180?方向。任意一个:方向。左);
}
私有void验证(双度){
如果(度<0 | |度>360){
抛出新的非法状态异常();
}
}
@试验
公开无效测试(){
assertEquals(Direction.LEFT,getDirection(90,1));
assertEquals(Direction.LEFT,getDirection(90359));
assertEquals(Direction.LEFT,getDirection(90271));
assertEquals(Direction.other,getDirection(90270));
assertEquals(Direction.RIGHT,getDirection(90269));
assertEquals(Direction.RIGHT,getDirection(90180));
}
}
最近的旋转方向是什么?最近的旋转方向是什么?