\ Forth version of the midpoint circle algorithm from midpoint.py : assert 0= if 69 throw then ; : yield cr . . ; \ `e` is always x² + y² - r² variable x variable y variable e variable r : mid dup r ! x ! 0 y ! 0 e ! begin x @ y @ > while e @ 0> if x @ dup + 1- negate e +! e @ 0 <= assert x @ 1- x ! then x @ y @ yield \ For efficiency you could fold this into the above update of \ `e` in the decrementing-`x` case. y @ dup + 1+ e +! y @ 1+ y ! repeat ; \ A version that keeps `x` on the top of the operand stack \ and doesn't store r. \ `e` is always x² + y² - r² variable y variable e : mid2 0 y ! 0 e ! begin dup y @ > while e @ 0> if dup dup + 1- negate e +! e @ 0 <= assert 1- then dup y @ yield y @ dup + 1+ e +! y @ 1+ y ! repeat ; \ a version that instead keeps `e` on top of the operand stack variable x variable y : mid3 x ! 0 y ! 0 begin x @ y @ > while dup 0> if x @ dup + 1- - dup 0 <= assert x @ 1- x ! then x @ y @ yield y @ dup + 1+ + y @ 1+ y ! repeat ; \ A version that keeps `x` under `e` on the stack variable y : mid4 0 y ! 0 begin over y @ > while dup 0> if over dup + 1- - dup 0 <= assert swap 1- swap then over y @ yield y @ dup + 1+ + y @ 1+ y ! repeat ; \ A version that keeps `y` on the return stack : mid5 0 >r 0 begin over r@ > while dup 0> if over dup + 1- - dup 0 <= assert swap 1- swap then over r@ yield r@ r@ + 1+ + r> 1+ >r repeat r> drop ;