c++mathgame-physics

How can I calculate the angle to the horizontal axis of a reflected ray from a rotated line?


I’m having a C++ program and i want to calculate the reflection angle for a light ray. In the example, I have a yellow line(light ray) with an angle of 135 degrees to the X-Coordinate that hits the red line from which it should be reflected. The green line is the reflected line. I have already tried to calculate the angle between the red and the yellow line, but I don’t know what to do with the rotated line. So here is my question: How do I calculate the angle of the green line to the horizontal axis? Code would be nice.

This Code should calculate the angle between the yellow and the red line:

float a = atan(abs((HitPointWithRedLine.x - YellowLineStartPoint.x)) / abs(HitPointWithRedLine.y - YellowLineStartPoint.y)) * 180 / 3.14; 

But because I don’t know what to do with the angle of the line, I tried this (which obviously doesn’t work):

float resultAngle = a - angleLine

Example

Soution:

This solution might not be the best but it works. It might be better to use vectors rather then angles for those callculations:

    int LineAngle = 0;

    int IncidentAngle = 135;
    int beta = 180 - IncidentAngle;

    int AngleOfRefrection = beta + 2 * LineAngle;


//edge case:
    if (IncidentAngle == 90)
        AngleOfRefrection = 270;
        
    else if (IncidentAngle == 180)
        AngleOfRefrection = 0;

    else if (IncidentAngle == 270)
        AngleOfRefrection = 90;

    else if (IncidentAngle == 360 || IncidentAngle == 0)
        AngleOfRefrection = 180;

Solution

  • Step 0: Figure out your coordinate system, which way is zero degrees, which way is 90 degrees, and so on.

    Step 1: Generate some test cases

    Step 2: Work out by hand what the answers should be. Here are a few, but you'll want to handle all the corner cases you can think of:

    Step 3: Write some code that gets the right answers

    Step 4: Return the result in the correct format.

    Good luck with that assignment! :-)