"""
QuadTree: Hanging Nodes
=======================

You can give the refine method a function, which is evaluated on every
cell of the TreeMesh.

Occasionally it is useful to initially refine to a constant level
(e.g. 3 in this 32x32 mesh). This means the function is first evaluated
on an 8x8 mesh (2^3).

"""
import discretize
import matplotlib.pyplot as plt


def run(plotIt=True):
    M = discretize.TreeMesh([8, 8])

    def refine(cell):
        xyz = cell.center
        dist = ((xyz - [0.25, 0.25]) ** 2).sum() ** 0.5
        if dist < 0.25:
            return 3
        return 2

    M.refine(refine)
    if plotIt:
        M.plotGrid(nodes=True, centers=True, faces_x=True)
        plt.legend(
            (
                "Grid",
                "Cell Centers",
                "Nodes",
                "Hanging Nodes",
                "X faces",
                "Hanging X faces",
            )
        )


if __name__ == "__main__":
    run()
    plt.show()

@@ -729,20 +729,21 @@
        %22
-Grid
+Nodes
 %22,%0A     
@@ -758,19 +758,20 @@
    %22
-Cell Center
+Hanging Node
 s%22,%0A
@@ -791,45 +791,19 @@
    %22
-Nodes%22,%0A                %22Hanging Node
+Cell Center
 s%22,%0A
@@ -856,32 +856,56 @@
 nging X faces%22,%0A
+                %22Grid%22,%0A
             )%0A  

Switch order (again) of legend for example