use linear partition algorithm to avoid the last image problem