簡體   English   中英

如何計算兩個向量之間的角度?

[英]How to calculate the angle between two vectors?

我們正在嘗試獲取v和u之間的cos值,但結果卻遠遠大於1或小於0

哪里:

vx = in.nextInt(); // x speed of your pod
vy = in.nextInt(); // y speed of your pod

int ux = nextCheckPointIdX - x;
int uy = nextCheckPointIdY - y;

這是公式:

double cos = (vx*ux + vy*uy) / ( Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2)) + Math.sqrt(Math.pow(ux, 2) + Math.pow(uy, 2)) );

您在上一行中發現任何錯誤嗎?

分母有問題。

int num = (vx*ux + vy*uy);
double den = (Math.sqrt(Math.pow(vx, 2) + Math.pow(vy, 2)) * (Math.sqrt(Math.pow(ux, 2) + Math.pow(uy, 2))) );
double cos =  num / den;
System.out.println(cos);
System.out.println(Math.acos(cos));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM