I started a notebook dealing with metrics in a triangle and its intersection points by a circle. Up to now I reached the point of having plotted a triangle, a circle with an arbitrary centre and I know the coordinates where they intersect.
Before going further with adding more meaningful circles and more lines I would like to know if there is a better way of finding the coordinates of the points where the triangle intersects the circle.
Here is my code:
eqc[c_, r_] := {(x - c[[1]])^2 + (y - c[[2]])^2 -
r^2} (* c: {x,y} coord.centre r: radius *)
a = {2, 4}; b = {7, 1}; c = {-1, -4}; (* triangle vertices *)
c1 = eqc[{5/2, 4/7}, 3]; (* expression for plotting the circle *)
fctline[a_, b_] :=
Reduce[Det[{{x, y, 1}, (* Finds equation of a line between 2 points a{x,y} b{x,y) *)
{a[[1]], a[[2]], 1},
{b[[1]], b[[2]], 1}}] == 0, y];
lineA = fctline[c, b]; (* fct of line oposite vertex A *)
lineB = fctline[c, a];
lineC = fctline[a, b];
coordpts[c_, line_] :=
Module[{eqns}, (* module computing coords.
intersection of a line with a circle *)
eqns = {c == 0, y - line[[2]] == 0};
{x, y} /. Solve[eqns, {x, y}]];
trpts = Flatten[{coordpts[c1, lineA], coordpts[c1, lineB],
coordpts[c1, lineC]}, 1];
symbolsI = FromCharacterCode /@ (Range[Length@trpts] + 944);
symbolsV = {A, B, C};
plotA = Plot[{Transpose[(y /. Solve[# == 0, y]) & /@ c1]}, {x, -5,
15}, (* triangle to be added next step *)
PlotStyle -> {Black, Black, Black, Blue},
PlotRange -> {{-3, 8}, {-6, 6}}, PlotRangePadding -> 0.5,
AspectRatio -> Automatic,
Epilog -> {
{Red, PointSize[0.01], Point[trpts],
(Text[Style[#, 15], #2 + {-0.2, .2}] & @@@
Transpose[{symbolsI, trpts}])},
{Black, PointSize[0.001], Point[{a, b, c}],
(Text[Style[#, 20], #2 + {-0.3, .3}] & @@@
Transpose[{symbolsV, {a, b, c}}])}}]
To plot the sides of the triangle with lines strictly limited to the sides of the triangle I anticipated a rather repetitive process (i.e. plot nine segments of lines, six of them with a Transparent
graphics attributes) but I found in Proper way to Plot a single function in two different styles? a nice solution to deal with that with less code.
SetAttributes[splitplot, HoldAll]; (* thanks to Simon Woods *)
splitplot[pieces__] := Piecewise[{#}, I] & /@ Unevaluated@pieces
splitplot[{v_, c_}] := splitplot[{v, c}, {v, ! c}]
splitstyle[styles__] :=
Module[{st =
Directive /@ {styles}}, {{Last[st = RotateLeft@st], #}} &]
sideA = Plot[{splitplot[{lineA[[2]],
x < -1 || x > 7}, {lineA[[2]], -1 <= x <= 7}]} ,
{x, -2, 8},
PlotStyle ->
splitstyle[{Transparent, Black}, {Transparent, Black}, {Black}]];
sideB = Plot[{splitplot[{lineB[[2]],
x < -1 || x > 2}, {lineB[[2]], -1 <= x <= 2}]} ,
{x, -2, 8},
PlotStyle ->
splitstyle[{Transparent, Black}, {Transparent, Black}, {Black}]];
sideC = Plot[{splitplot[{lineC[[2]], x < 2 || x > 7}, {lineC[[2]],
2 <= x <= 7}]} ,
{x, -2, 8},
PlotStyle ->
splitstyle[{Transparent, Black}, {Transparent, Black}, {Black}]];
Show[plotA, sideA, sideB, sideC, Axes -> None, ImageSize -> 600]
As you can see I considered the triangle as a set of three lines. That's certainly not elegant but a triangle is not a locust of points than can be determined by a general function as for any conics, function that you can integrate or differentiate. There is perhaps a function for it in higher mathematics and if so may be a built-in (undocumented) in MMA is available.
EdgeForm@Thick, FaceForm@None, Polygon[{a, b, c}]
inEpilog
. $\endgroup$Solve
between the equation of the circle and an analytic expression representing the triangle. Precision of the intersection points coordinates is very important. $\endgroup$