How do I draw a triangle given the lengths of the sides?
In version 10, use SSSTriangle
which stands for side-side-side triangle.
To answer your first question: when in doubt, fall back to basic construction geometry. Fix two points arbitrarily, and "construct" the third by finding the point of intersection of two circles with these points as centers and radii as the sides. Example:
sideAB = 4;sideBC = 5;sideCA = 6;
ptA = {0, 0};ptB = ptA + {sideAB, 0};
ptC = {x, y} /. Last@Solve[x^2 + y^2 - sideCA^2 == 0 &&
y^2 + (x - sideAB)^2 - sideBC^2 == 0, {x, y}]
Graphics[{FaceForm[], EdgeForm[{Thick, Black}], Polygon[{ptA, ptB, ptC}]}]
I'm not sure how to draw a triangle if all I care about is the length of the sides. (I'm happy to place one of the vertices at the origin and place one of the sides on the nonnegative side of the $x$-axis, but that doesn't really matter.) Is there a straightforward way?
Given a triangle with side lengths $p \leq q \leq r$ (assuming the lengths satisfy the triangle inequality, of course), with hypotenuse on the positive $x$-axis, and the origin as one endpoint, you can solve a system of equations to get the third point, apart from $(0,0)$ and $(r,0)$:
FullSimplify[{x, y} /.
Last[Solve[{x^2 + y^2 == p^2, (x - r)^2 + y^2 == q^2}, {x, y}]]]
{(p^2 - q^2 + r^2)/(2 r),
Sqrt[-(p - q - r) (p + q - r) (p - q + r) (p + q + r)]/(2 r)}
and thus, either of
Graphics[Line[{{0, 0}, {r, 0}, {(p^2 - q^2 + r^2)/(2 r),
Sqrt[-(p - q - r) (p + q - r) (p - q + r) (p + q + r)]/(2 r)}, {0, 0}}]]
or
Graphics[Polygon[{{0, 0}, {r, 0}, {(p^2 - q^2 + r^2)/(2 r),
Sqrt[-(p - q - r) (p + q - r) (p - q + r) (p + q + r)]/(2 r)}}]]
does the job.
Verify the triangle:
FullSimplify[
Norm /@ Differences[{{0, 0}, {r, 0}, {(p^2 - q^2 + r^2)/(2 r),
Sqrt[-(p - q - r) (p + q - r) (p - q + r) (p + q +
r)]/(2 r)}, {0, 0}}], 0 <= p <= q <= r && p + q >= r]
{r, q, p}
This raises the additional question, "how do I draw a triangle in Mathematica, given three angles?". (Say I want to it to reside somewhere in the unit circle.)
The law of sines saves your bacon here (the ratio of a side length and the sine of the opposite angle gives the diameter of the triangle's circumcircle). Skipping details, here's how to inscribe a triangle with specified angles into the unit circle:
parts = IntegerPartitions[180, {3}];
(* generate corresponding sides from randomly chosen angles *)
angles = parts[[RandomInteger[{1, Length[parts]}]]];
{r, q, p} = 2 Sin[angles Degree];
Graphics[{Line[{{1, 0}, {1 - p^2/2, p Sqrt[1 - p^2/4]},
{1 - q^2/2, -q Sqrt[1 - q^2/4]}, {1, 0}}],
Circle[{0, 0}, 1]}]
You can use the next two snippets to verify that the triangle generated fits the specifications:
(* check side lengths *)
Norm /@ RotateLeft[Differences[
N[{{1, 0}, {1 - p^2/2, p Sqrt[1 - p^2/4]},
{1 - q^2/2, -q Sqrt[1 - q^2/4]}, {1, 0}}]]] - {r, q, p} // Chop
(* check angles *)
(Apply[VectorAngle, Map[Function[pt, pt - First[#]], Rest[#]]]/Degree) & /@
NestList[RotateLeft, N[{{1, 0}, {1 - p^2/2, p Sqrt[1 - p^2/4]},
{1 - q^2/2, -q Sqrt[1 - q^2/4]}}], 2] - angles // Chop
Both snippets should return {0, 0, 0}
if all goes well.