三角関数使っていいんなら
https://wandbox.org/permlink/jdqvEAxpQLNSPGbV