In your examples what you want can be achieved by just using the make planar buttons under edit geometry...as long as its on that plane of course. Loop select, convert to verts, deselect the end verts, make planar, vertex snap back to the end verts.
no luck im afraid. I tried to do it by creating a transformation matrix from the normal of a triangle derived from the first, second and middle vertex and then flattening all the verts in that coordinate space but it wasn't consistant(my math isnt great so im not sure if my logic or code is faulty.). after that I started…