java三点求⾓度_Java:计算两点之间的⾓度(以度为单位)
我从johncarls解决⽅案开始,但需要对其进⾏调整以完全满⾜我的需求。主要是,当⾓度增加时,我需要它顺时针旋转。我还需要0度才
能指向北。他的解决⽅案使我接近了,但是我决定也发布我的解决⽅案,以防其他⼈受益。
我添加了⼀些其他注释,以帮助解释我对功能的理解,以防您需要进⾏简单的修改。
/**
*CalculatestheanglefromcenterPttotargetPtindegrees.
*Thereturnshouldrangefrom[0,360),rotatingCLOCKWISE,
*0and360degreesreprentsNORTH,
*90degreesreprentsEAST,etc...
*
*arenot,
*tPointToScreenorequivalent
*onallargumentsbeforepassingthemtothisfunction.
*
*@paramcenterPtPointwearerotatingaround.
*@paramtargetPtPointwewanttocalcuatetheangleto.
*@theanglefromcenterPttotargetPt.
*/
publicstaticdoublecalcRotationAngleInDegrees(PointcenterPt,PointtargetPt)
{
//calculatetheanglethetafromthedeltaYanddeltaXvalues
//(atan2returnsradiansvaluesfrom[-PI,PI])
//0currentlypointsEAST.
//NOTE:ByprervingYandXparamordertoatan2,weareexpecting
//aCLOCKWISEangledirection.
doubletheta=2(targetPt.y-centerPt.y,targetPt.x-centerPt.x);
//rotatethethetaangleclockwiby90degrees
//(thismakes0pointNORTH)
//NOTE:addingtoananglerotatesitclockwi.
//subtractingwouldrotateitcounter-clockwi
theta+=/2.0;
//convertfromradianstodegrees
//thiswillgiveyouananglefrom[0->270],[-180,0]
doubleangle=ees(theta);
//converttopositiverange[0-360)
//sincewewanttopreventnegativeangles,adjustthemnow.
//wecanassumethatatan2willnotreturnanegativevalue
//greaterthanonepartialrotation
if(angle<0){
angle+=360;
}
returnangle;
}
本文发布于:2023-01-19 18:19:01,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/88/96344.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |