3
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ Hi, I'm sorry, what is the question? If how to draw a triangle, then you need nothing more than EdgeForm@Thick, FaceForm@None, Polygon[{a, b, c}] in Epilog. $\endgroup$
    – Kuba
    Mar 21, 2014 at 23:08
  • $\begingroup$ The question is about getting the cartesian coordinates of the points where a circle intersects a triangle - with less code as here where I had to plot three lines and solve three equations, where each line intersects the circle. $\endgroup$
    – Sigis K
    Mar 22, 2014 at 6:10
  • $\begingroup$ Ok, so the data is: 3 points for traingle, center and radius for circle, right? Also, is precission important? $\endgroup$
    – Kuba
    Mar 22, 2014 at 8:52
  • $\begingroup$ Yes, I start with 3 points for the triangle , 1 point for the centre of the circle, length of its radius. I could not find a way to write a single Solve between the equation of the circle and an analytic expression representing the triangle. Precision of the intersection points coordinates is very important. $\endgroup$
    – Sigis K
    Mar 22, 2014 at 16:35

1 Answer 1

5
$\begingroup$

Use RegionIntersection. Your example:

triangle = Line[{{2, 4}, {7, 1}, {-1, -4}, {2, 4}}];
circle = Circle[{5/2, 4/7}, 3];

pts = RegionIntersection[triangle, circle]

Point[{{1/476 (1847 - 15 Sqrt[1335]), 1/476 (1367 + 9 Sqrt[1335])}, {1/476 (1847 + 15 Sqrt[1335]), 1/476 (1367 - 9 Sqrt[1335])}, {1/623 (2225 + 12 Sqrt[9523]), (-1424 + 15 Sqrt[9523])/ 1246}, {1/623 (2225 - 12 Sqrt[9523]), (-1424 - 15 Sqrt[9523])/1246}}]

Visualization:

Graphics[{triangle, circle, Red, PointSize[Large], pts}]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Pleasant surprise! A first answer nearly four years since my question was posted! $\endgroup$
    – Sigis K
    Mar 7, 2018 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.