简体   繁体   English

找到相对于x轴的反射角

[英]find angle of reflection relative to x-axis

I am working on a project that displays a laser beam and it's path through reflections. 我正在做一个显示激光束及其通过反射路径的项目。 When an entity is hit by a laser it calls the entities alterLaser(Laser r) method. 当实体被激光击中时,它将调用实体alterLaser(Laser r)方法。 In order to create a reflecting ray I must call Laser.createNew(x,y,angle counter clockwise of (+x)). 为了创建反射射线,我必须调用Laser.createNew(x,y,(+ x)​​逆时针旋转的角度)。 My problem is that I can find the angle of reflection easily but I don't know how to find that angle relative to the x-axis. 我的问题是我可以轻松找到反射角度,但是我不知道如何找到相对于x轴的角度。

I first tried finding the acute angle in between the two vectors laser and mirror but I do not know if there is a direct relationship between that and the x-axis. 我首先尝试在激光和反射镜这两个向量之间找到锐角,但是我不知道它和x轴之间是否存在直接关系。 After searching the internet I found this formula: 搜索互联网后,我发现以下公式:

r = i + 2s - 180 r =我+ 2秒-180

where r is the angle the reflected ray makes with the X-axis. 其中r是反射射线与X轴的夹角。 i is the angle the initial ray makes with the x-axis and s is the angle the reflected ray makes with the x-axis; i是初始射线与x轴的夹角,s是反射射线与x轴的夹角;

I found this formula wasn't working, but in the cases I tried it was giving theta in a different quadrant than the intended quadrant. 我发现此公式无效,但是在我尝试过的情况下,它在与预期象限不同的象限中给出theta。 But that poses the new problem of finding which quadrant it is in. 但这提出了新的问题,即要找到它所在的象限。

here is a look at my current code: 这是我当前的代码:

@Override
protected void alterLaser(Laser r) {

    Vec laser = new Vec(r.getStartX(),r.getStartY(),r.getEndX(),r.getEndY());
    Vec mirror = new Vec(this.getStartX(),this.getStartY(),this.getEndX(),this.getEndY());

    double theta,thetaq2,thetaq3,thetaq4;
    theta = laser.angle() + (2 * mirror.absAngle()) - 180;
    theta = Vec.absTheta(theta);
    thetaq2 = 180-theta;
    thetaq3 = theta+180;
    thetaq4 = 360-theta;

    Laser.createNew(r.getEndX(),r.getEndY(),theta,null,this);
    Laser.createNew(r.getEndX(),r.getEndY(),thetaq2,null,this);
    Laser.createNew(r.getEndX(),r.getEndY(),thetaq3,null,this);
    Laser.createNew(r.getEndX(),r.getEndY(),thetaq4,null,this);

}

} }

The easiest way in Java to find the angle a vector makes counterclockwise with respect to positive x axis is to use the Math.atan2 function. 在Java中找到向量相对于正x轴逆时针旋转的角度的最简单方法是使用Math.atan2函数。 https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,%20double) https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#atan2(double,%20double)

This will put the value on the range -pi to pi so you don't have to worry about special casing the different quadrants. 这会将值放在-pipi范围内,因此您不必担心不同象限的特殊大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM