I am having trouble finding the $z$-component of an electric field discussed in problem 2.4 of Griffith’s introduction to electrodynamics.
Suppose we have a square loop of side length $a$ carrying a linear charge density of $\lambda$. To find the electric field a distance $z$ above the center of the square loop, it is sufficient to find the electric field at $z$ due to one side of the square, take the component of that electric field in the $z$ direction, and multiply by $4$. This $z$ component is found by multiplying the electric field $\mathbf{E}$ by $\cos\theta$, where $\theta$ is the angle made by the separation vector $\mathbf{r}$ from the square loop to $z$ intersecting the line from the center of the square loop to $z.$ Now, according to Griffiths, $\cos\theta = \frac{z}{\sqrt{z^2 + (a/2)^2}}$. But I do not see how this is true. This expression just seems to be the cosine of the angle between $\mathbf{r}$ and $z$ when $\mathbf{r}$ is coming from the midpoint of the side of the loop. But as you vary the source of $\mathbf{r}$ across the side of the loop, to account for the contribution of the whole side of the loop to $\mathbf{E}$, that angle will change. If so then $\cos\theta$ should be $\frac{z}{\sqrt{z^2 + x^2 + (a/2)^2}}$ to reflect this. But apparently this cannot be right. Where is the error in my reasoning?