function [ distance, intersectionx, intersectiony] = IRSensorRight( rect, sensorx, sensory, carTheta)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
    rectxLeft = rect(1);
    rectxRight = rect(1) + rect(3);
    rectyTop = rect(2) + rect(4);
    rectyBottom = rect(2);
    sensorTheta = carTheta - pi/2;
    sensorTheta = mod(sensorTheta, 2*pi);
               
    closestPointx = 0;
    closestPointy = 0;
    closestDistance = -1;
    
    if (sensorTheta == pi/2)
        if (rectxLeft < sensorx && rectxRight > sensorx && rectyBottom > sensory)
            closestPointx = sensorx;
            closestPointy = rectyBottom;
            closestDistance = rectyBottom - sensory;
        end
        
    elseif(sensorTheta == 3*pi/2)
        if (rectxLeft < sensorx && rectxRight > sensorx && rectyTop < sensory)
            closestPointx = sensorx;
            closestPointy = rectyTop;
            closestDistance = sensory - rectyTop;
        end
        
    else
        m = tan(sensorTheta);
        b = sensory - m*sensorx;
        
        lowerxIntersection = (rectyBottom - b)/m;
        if (lowerxIntersection >= rectxLeft && lowerxIntersection <= rectxRight)
            thisDistance = sqrt((lowerxIntersection - sensorx)^2 + (rectyBottom - sensory)^2);
            if (thisDistance < closestDistance || closestDistance == -1)
                closestDistance = thisDistance;
                closestPointx = lowerxIntersection;
                closestPointy = rectyBottom;
            end
        end
        
        upperxIntersection = (rectyTop - b)/m;
        if (upperxIntersection >= rectxLeft && upperxIntersection <= rectxRight)
            thisDistance = sqrt((upperxIntersection - sensorx)^2 + (rectyTop - sensory)^2);
            if (thisDistance < closestDistance || closestDistance == -1)
                closestDistance = thisDistance;
                closestPointx = upperxIntersection;
                closestPointy = rectyTop;
            end
        end
        
        leftyIntersection = m*rectxLeft + b;
        if (leftyIntersection >= rectyBottom && leftyIntersection <= rectyTop)
            thisDistance = sqrt((rectxLeft - sensorx)^2 + (leftyIntersection - sensory)^2);
            if (thisDistance < closestDistance || closestDistance == -1)
                closestDistance = thisDistance;
                closestPointx = rectxLeft;
                closestPointy = leftyIntersection;
            end
        end
        
        rightyIntersection = m*rectxRight + b;
        if (rightyIntersection >= rectyBottom && rightyIntersection <= rectyTop)
            thisDistance = sqrt((rectxRight - sensorx)^2 + (rightyIntersection - sensory)^2);
            if (thisDistance < closestDistance || closestDistance == -1)
                closestDistance = thisDistance;
                closestPointx = rectxRight;
                closestPointy = rightyIntersection;
            end
        end    
            
        if sign(cos(sensorTheta)) ~= sign(closestPointx - sensorx)
            closestPointx = 0;
            closestPointy = 0;
            closestDistance = -1;
        end
    end

    distance = closestDistance;
    intersectionx = closestPointx;
    intersectiony = closestPointy;
end





