A little question about the post by MBo, since the ellipse is rotated by theta with respect to the old coordinate, to rotate the line into the ellipse's local coordinate system, I think we need to rotate the line by -theta like sin(-theta)) and cos(-theta) in your formula, it is actually rotating the old coordinate system of line into the ellipse's coordinate, not rotating the ellipse or line. I do not know if this reasoning is correct, please share with me your ideas.