optimize calculate direction and opposite