You only need to be dense and presenting your bayonets to discourage horses and riders, a line or column could accomplish that. The purpose of the square as a shape itself is that the riders can not encircle you, and the right-angles are preferable over a circle or oval because it's easier to arrange each side in multiple ranks and ensure equal spacing between men. A disadvantage lay in the corners, but in the early to mid-18th century these had gaps with regimental artillery deployed, in which case the cavalry that tried to breach the corner would face a blast of canister.
In the Russo-Turkish War of 1806-1812, Turkish cavalry was so plentiful and speedy, that the Russians actually on occasion marched from place to place in square - essentially a line, followed by two columns, and line again in the rear. If sudden contact was made, the front would hold, the columns would perform a quarter-face, and the rear line did an about-face. Na ruku! - Charge your bayonets! Show me a video of an NW regiment doing that, and I might start playing again.