Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I started a notebook dealing with metrics in a triangle and its intersection points by a circle. Up to know 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 (ie plot nine segments of lines, six of them with a Transparent graphics attributes) but I found in Stack Exchange question 8199 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.

share|improve this question
    
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. –  Kuba Mar 21 at 23:08
    
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. –  Sigismond Kmiecik Mar 22 at 6:10
    
Ok, so the data is: 3 points for traingle, center and radius for circle, right? Also, is precission important? –  Kuba Mar 22 at 8:52
    
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. –  Sigismond Kmiecik Mar 22 at 16:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.