In fact, this looks like a reduced version of the above longhand algorithm. I think I did it long hand originally because when you reduce the math it becomes very opaque (i.e., hard for others to understand).

What do think about leaving the old code, but commented out and explaining this is what the following math is actually doing?