function [ distance ] = IRSensorCar(sensorx,...
    sensory, sensorTheta, otherCarx, otherCary, otherCarTheta)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
    
    %rotate everything so that other car is oriented on x and y axis
    rotationMatrix = [cos(otherCarTheta), sin(otherCarTheta); -sin(otherCarTheta),...
        cos(otherCarTheta)];
    otherCarCoords = [otherCarx; otherCary];
    sensorCoords = [sensorx; sensory];
    
    otherCarCoords = rotationMatrix*otherCarCoords;
    sensorCoords = rotationMatrix*sensorCoords;
    sensorTheta = sensorTheta - otherCarTheta;
    otherCarTheta = 0;
    
    %find car coordinates (bottomleft corner, height, and width) for
    %other car:
    carLength = .40;  %in pixels
    carWidth = .20;
    carxLeft = otherCarCoords(1) - carLength;
    caryBottom = otherCarCoords(2) - carWidth/2;    
    
    %write as rectangle and plug into rectangle sensor
    rectxLeft = carxLeft;
    rectxRight = carxLeft + carLength;
    rectyTop = caryBottom + carWidth;
    rectyBottom = caryBottom;
    sensorTheta = mod(sensorTheta, 2*pi);
    
    sensorx = sensorCoords(1);
    sensory = sensorCoords(2);
               
    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