If I average based on a axis, the angle you see in the image is lost; no desired. If the vertices for the polygon, or polygons can be stored then averaged based on the vertices world position ?
u can snap all vertice to the position of single vertice by holding v first the moving on a single axis (holding v is shortcut to some snap setting i forgot what it was) be sure to try it with a simple box first than u will get the concept just dont select the vertices u want to snap to
Kodde your script runs off faces & vertices ? I ran the script on both and when the script is run on vertices I get this error; planarAverage# Result: <function planarAverage at 0x00000044EAADBF98> #
If you want to flatten based on the vertices average normal, just select the vertices you want to do that, select the scale tool, on the tool options choose normals average, then just scale, they will flatten on the chosen axis that is rotated to face the selection average normal.