A square inside an equilateral triangle Given an equilateral triangle and a point $D$ on one of its sides, I need to construct a square $DEFG$ with the vertices $E, F$ lying on the other two sides of the triangle and $G$ somewhere inside it (see picture).

I know if $D$ is the midpoint of the respective side, the problem is easy, but how about the general case? Are there any solutions at all? Actually, my intuition says there should not be if $D$ is not quite close to the middle. 
Furthermore, I have tried using analytic geometry but it quickly became messed up....so I wonder also if we can construct such a square with compass and ruler only. 
Thanks in advance.
 A: As $DF=DE\sqrt 2$ and the angle $\angle EDF=45^{\circ},$ the point $F$ is obtained from $E$ through the rotation composed with the homothety (common center $D$, angle and ratio as above).   
Construct in this transformation the image of the side that should contain $E.$ Its intersection (if it exists) with the side that doesn't contain $D$ is $F.$

A: Let 
the vertex of the triangle between $D$ and $E$ be $A$ 
and 
the vertex of the triangle between $E$ and $F$ be $C$
Let the length of the side of the triangle be $a$
length of the side of the square be $u$
$\angle ADE = \theta$ (therefore, $\angle EFC = \frac{5 \pi}{6} - \theta$)
length of $AE$ be $x$, therefore length of $CE$ is $a-x$
length of $AD$ be $y$
Then consider the triangle $ ADE$ and use Sine rule:
$\displaystyle \frac{u}{\sin \frac{\pi}{3}} = \frac{x}{\sin \theta} = \frac{y}{\sin \left(\frac{2 \pi}{3} - \theta \right)}$ ........ (1)
Next consider triangle $CEF$ and use Sine rule:
$\displaystyle \frac{u}{\sin \frac{\pi}{3}} = \frac{a-x}{\sin \left( \frac{5 \pi}{6} - \theta \right)}$ ........ (2)
All the above quantities of (1) and (2) are equal to 
$\frac{a}{\sin \theta + \sin \left( \frac{5 \pi}{6} - \theta \right)}$
[Ratio of the sum of the numerators and denominators of $\frac{x}{\sin \theta}$ and $\frac{a-x}{\sin \left( \frac{5 \pi}{6} - \theta \right)}$]
Hence 
$\displaystyle u = \frac{a \sin \frac{\pi}{3}}{\sin \theta + \sin \left( \frac{5 \pi}{6} - \theta \right)}$
$\displaystyle x = \frac{a \sin \theta}{\sin \theta + \sin \left( \frac{5 \pi}{6} - \theta \right)}$
$\displaystyle y = \frac{a \sin \left(\frac{2 \pi}{3} - \theta \right)}{\sin \theta + \sin \left( \frac{5 \pi}{6} - \theta \right)}$
Note: 


*

*Suppose only $y$ is known. You can easily find $\theta$ and then calculate $u$ and $x$ 

*Not all the values of $y$ are admissible. For example, if $y > \sqrt{3} a$, then the equation does not have any solution. Practically, $0 \leq y \leq a$
A: $\;\;\;$

Let triangle $ABC$ be equilateral.

Using coordinates, and then solving algebraically, we get the following result:

If $D$ is on side $BC$, strictly between $B$ and $C$, there is at most one square  $DEFG$ such that


*

*$E$ is on side $CA$, strictly between $C$ and $A$.$\\[4pt]$

*$F$ is on side $AB$, strictly between $A$ and $B$.$\\[4pt]$

*$G$ is in the interior of triangle $ABC$.


and such a square exists if and only if
$$4-2\sqrt{3} < \frac{|BD|}{|BC|} < \sqrt{3}-1\qquad(\mathbf{*})$$
Moreover, if $(\mathbf{*})$ is satisfied, then letting 
$$d=\frac{|BD|}{|BC|}$$
the points $E,F,G$ are uniqely determined by
\begin{align*}
\frac{|CE|}{|CA|}&=2-\sqrt{3}+d\left(\frac{\sqrt{3}-1}{2}\right)\\[4pt]
\frac{|AF|}{|AB|}&=d-2+\sqrt{3}\\[4pt]
\end{align*}
and where $G$ is the reflection of $E$ over the line $DF$.
A: After receiving these nice comments and answers of you, I decided to post more a big comment than an answer to my question. So supposing that the given triangle is $ABC$ and the point belongs to the $BC$ side. We further can assume (due to homoiothesis) that the sides of the triangle have length equal to $2$. Then posing an horthogonal coord system we can have the following scheme:

$D(d, 0), E(a,\sqrt{3}a+\sqrt{3}), F(b,-\sqrt{3}b+\sqrt{3}), G(z,w)$
So if there is such a square $DEFG$ then $ED\bot EF \iff (a-d)(b-a)+3(a+1)(a+b)=0\qquad(\mathbf{1})$
and $\vert ED\vert = \vert EF\vert \iff (a-d)^2+3(a+1)^2=(b-a)^2+3(a+b)^2\qquad(\mathbf{2})$
The point $G$ would be the reflection of $E$ through  the line $DF$ iff :
$(z-a)(b-d)+(w-\sqrt3 a-\sqrt3)(-\sqrt3 b +\sqrt3)=0$        (3)
$(b-d)\frac{\sqrt3(1+a)+w}{2}+\sqrt3(b-1)(\frac{a+z}{2}-d) =0$      (4)
$-1<a,b,z<1,w>0$    (5)
If we want point $G$ to lie in the triangle we should force:
$\sqrt{3}(z+1)-w>0$ and $\sqrt{3}(1-z)-w>0$  (6)
If we solve the above system, using for example WolframAlpha we get a unique solution iff: $$3-2\sqrt{3}<d<4\sqrt{3}-7<0$$
