Multiple driving forces shape cities. These forces include the costs of transporting goods and people, the types of predominant local industries, and the policies that govern urban planning. Here, we examine how agglomeration and dispersion change with increasing population and population density. We study the patterns in the evolution of urban forms and analyze the differences between developed and developing countries. We analyze agglomeration across 233 European and 258 Chinese cities using nighttime luminosity data. We find a universal inverted U-shape curve for the agglomeration metric (Lasym index). Cities attain their maximum agglomeration level at an intermediate density, above which dispersion increases. Our findings may guide strategic urban planning for the timely adoption of appropriate development policies.