Proofs: Binary space partitioning tree
These are the human proofs of Binary space partitioning tree, at full length.
BUILD keeps the partition invariant and BACK-TO-FRONT gives a painter's order full
This proof shows: correctness, termination.
We use the names of the pseudocode. The input is a list of segments of positive length, no two of which cross or touch. For a node , the function is affine, so on a segment from to its value at the point is .
Invariant (P). For each node : (1) each fragment of lies on ; (2) each fragment of the subtree has at both ends, and not at both ends; (3) the same for with ; (4) the fragments of each input segment cover it and meet only at their ends.
Fact 1. Let a fragment have at both ends and not at both. Then at each point of the relative interior of . Proof. At such a point the value is a combination of the two end values with positive weights, both at least 0 and one positive. The mirror statement holds for .
BUILD. By induction on , BUILD returns a tree that satisfies (P) and whose fragments, for each segment of , cover it and meet only at their ends. Line L1 handles empty. Otherwise each segment goes to exactly one case. If is 0 at both ends, lies on the line and joins (part 1). If the end values are both and not both 0, goes to ; if both and not both 0, to . Otherwise one value is positive and the other negative, so line L8 cuts at the point with , where . The front part has end values and , the back part and a negative value, so parts 2 and 3 hold for them, and the two parts cover and meet only at the cut point, so part 4 holds. The lists and have at most entries each, since goes to neither, so the induction applies to them and the recursion stops. The argument for BUILD does not use the condition that the segments do not cross; only the claim below does. Parts 2 and 3 for hold because every fragment in the subtree is a part of a member of , which lies in the closed front half-plane, and a part of a segment with both end values has both end values and, by Fact 1, does not lie on the line. INSERT is the same filter for one segment, so it keeps (P) too.
Hiding. For a viewpoint , say that a fragment hides a point if is not on the line of and the open segment from to meets the relative interior of . When is on the line of , the fragment is seen edge on and covers nothing. A list of fragments is a painter's order for if no fragment hides a point of the relative interior of a fragment that comes later.
Claim. BACK-TO-FRONT outputs the fragments of the subtree in a painter's order for .
Proof. By induction on the height. The empty tree is clear. Let have children with outputs that are painter's orders by induction. A pair of fragments from one child keeps its order. For the list : its fragments all lie on . If is not on the line, the segment from to a point of the line meets the line only at , so it misses the other fragments of except at , which is not on the open segment. If is on the line, no fragment of hides anything by definition. It remains to treat pairs from different parts.
Case , order back, on, front (line L11). Let come from the back subtree, and from or the front subtree, and let be in the relative interior of . By parts 1 and 2 and Fact 1, . Each point of the open segment from to has with . By part 3 and Fact 1, the relative interior of has . So the open segment misses it, and does not hide . Let come from and from the front subtree: the interior of has , and the same computation shows the open segment has , so again does not hide .
Case (line L12) is the mirror image, with front and back swapped.
Case , order front, on, back (line L13). Let come from the front subtree and from or the back subtree, with in the relative interior of . By parts 1 and 3 and Fact 1, . If , each point of the open segment has , while the interior of has , so they do not meet. If , then is in by Fact 1, and the whole segment from to lies on , where ; the interior of has , so they do not meet. Let come from and from the back subtree: then is on the line of , so hides nothing.
In every case no earlier fragment hides a point of a later one, which proves the claim. Each node is expanded once, so the walk stops after steps.