Find angle of which a cannon of fixed length has to fire

A cannon is firing at a target $(x, y)$ units away. The length of the cannon is $l$, which determines the origin point of the cannonball along with $\theta$. The linear speed of which the cannonball is fired is $v$, and the gravitational acceleration is $g$.

$\theta$ needs to be the angle where the cannon will hit the target. $x$, $y$, $l$, $v$ and $g$ are all constants. The diagram is shown in the picture link.

I am writing code for the cannon that would need to be simplified. Is there an equation to find $\theta$ in terms of $x$, $y$, $l$, $v$, and $g$?

Starting position of the cannon ball:

$x_0 = \cos(\theta)$

$y_0 = \sin(\theta)$

You have a downwards acceleration of $g$, and the initial velocity is as following:

$v_{0x} = v \times \cos(\theta)$

$v_{0y} = v \times \sin(\theta)$

Now, we can calculate the position of the ball as a function of time $t$:

$x(t) = x_{0} + v_{0x}t = \alpha$

$y(t) = y_{0} + v_{0y}t -\dfrac{gt^2}{2} = \beta$

The goal is to hit the target at $(\alpha, \beta)$. In other words, we want $x(t)$ to be equal to $\alpha$ at the same time $y(t)$ is equal to $\beta$.

$\cos(\theta)\times(1 +vt)=\alpha$

$\sin(\theta) \times (1+vt)=\beta +\dfrac{gt^2}{2}$

$\therefore \tan(\theta)=\dfrac{\beta + \frac{gt^2}{2}}{\alpha}$

We also know that $t = \dfrac{\alpha - \cos(\theta)}{v \times \cos(\theta)}$

Now use the fact that $\sec(\theta)$ is $\sqrt{\tan^2(\theta) + 1}$, and you'll end up having an equation only in terms of $\tan(\theta)$. The equation is going to be really complex and maybe even unsolvable using conventional methods. But since you're coding, you can just use the last equation and guess the answer. Create a value $estimate$ and store the value you're guessing inside it. Change it until you get a desirable amount of accuracy.

Starting from Soroush khoubyarian's answer. $$t = \dfrac{\alpha - \cos(\theta)}{v \, \cos(\theta)}=\frac{\alpha \sec (t)}{v}-\frac{1}{v}=\frac{\alpha \sqrt{1+\tan^2(\theta) }}{v}-\frac{1}{v}$$ Let $T=\tan(\theta)$ to make the equation $$\alpha T=\beta +\frac g {2v^2}\left(\alpha \sqrt{1+T^2 }-1 \right)^2$$ that is to say $$\frac{2 v^2 (\alpha T-\beta )}{g}=\left(\alpha \sqrt{1+T^2 }-1 \right)^2$$Expanding, isolating the remaining square root and squaring again would lead (hoping no mistake) to a quartic equation in $T$ $$a T^4+b T^3+c T^2+d T +e=0$$ where $$a=\alpha^4 \qquad b=-\frac{4 \alpha ^3 v^2}{g}\qquad c=2 \left(\alpha ^4+\frac{\alpha ^2 \left(g^2+2 \beta g v^2+2 v^4\right)}{g^2}-2\right)$$ $$d=-\frac{4 \alpha v^2 \left(\alpha ^2 g+g+2 \beta v^2\right)}{g^2}\qquad e=\frac{\left(\left(\alpha ^2-1\right) g+2 \beta v^2\right) \left(\left(\alpha ^2+3\right) g+2 \beta v^2\right)}{g^2}$$ Now, have fun with the resolution of the quartic equation