enter image description here

What I want to do is to find a circle which is tanget to the three big ones. I tried the code below:

    findSmallCircle[c1_Circle, c2_Circle, c3_Circle] :=

But how do I get the center point and radius of each circle c1, c2, c3, and if this cannot be done, what else can I try?

Excuse me if this is a silly question -- I am new to Mathematica

  • $\begingroup$ If it's always 3 circles, you can just write findCircle[Circle[c1_,r1_],Circle[c2_,r2_],Circle[c3_,r3_]] := and use the centers c1..c3 and radii r1..r3 in the function body $\endgroup$ – Niki Estner Jun 27 '13 at 9:25

First of all your parameter syntax is not correct; you'll want to use c1_Circle which is a Pattern named c1 for an expression with a head Circle. Your c1_?Circle is a Pattern named c1 that passes the test function Circle (see PatternTest), which of course is not valid for the built-in function Circle.

You removed the issue above from your question. What remains seems underspecified. You can extract the centers and radii either with patterns (useful for inserting these parts into a specific expression), or simply using Apply, e.g.:

f1[cirs__Circle] := {cirs} /.  Circle[center_, radius_] :> {center, radius}

f1[Circle[{0, 0}, 1], Circle[{1, 1}, 3], Circle[{2, 2}, 2]]
{{{0, 0}, 1}, {{1, 1}, 3}, {{2, 2}, 2}}
f2[cirs__Circle] := List @@@ {cirs}

f2[Circle[{0, 0}, 1], Circle[{1, 1}, 3], Circle[{2, 2}, 2]]
{{{0, 0}, 1}, {{1, 1}, 3}, {{2, 2}, 2}}

If you are interested in the actual calculation of the inner circle you will need to describe the characteristics of the function input.

Since the element manipulation was indeed your focus let me expand upon what I wrote above with a few additional examples.

The replacement rule Circle[center_, radius_] :> {center, radius} is a very simple example of the kind of destructuring that is possible with pattern-based transformations. These can be done either with RuleDelayed as above, or as part of a SetDelayed definition. You can also be more specific with the patterns, or provide for different cases to accommodate a flexible syntax. The documentation for Circle shows a number of different acceptable syntax forms, and to be robust your function should be able to handle these to some degree. As an example I will give a rule that will give a list {x, y, radius} for a variety of Circle forms.

  Circle[{1, 2}],
  Circle[{3, 4}, 7.5],
  Circle[{0, 0}, 1, {Pi/6, 3 Pi/4}]
} /. Circle[{x_, y_}, r_: 1, ___] :> {x, y, r}

{{1, 2, 1}, {3, 4, 7.5}, {0, 0, 1}}

Note that right-hand-side of :> could be any expression into which to substitute values for x, y, r. Here the pattern r_:1 represents a pattern with an Optional value 1 which is used in the case of Circle[{1, 2}].

If you do not require the flexibility and argument testing of pattern matching Apply can be used, of which @@@ is a short form for Apply at levelspec 1. In addition to List as used above this can be any head or function. For example:

{#, #2} & @@ Circle[{0, 0}, 1, {Pi/6, 3 Pi/4}]
{{0, 0}, 1}
  • $\begingroup$ thanks, so If I dont use ?Circle, how do I get the data of circle? $\endgroup$ – tintin Jun 27 '13 at 0:41
  • $\begingroup$ @tintin I added another section to my answer; I don't know if this is what you are after, or the actual calculation of the inner circle. Please clarify. $\endgroup$ – Mr.Wizard Jun 27 '13 at 0:50
  • $\begingroup$ thanks so much for that, I know how to generate the answer if I have the data, my question is to access the circle data,and you do help a lot. thanks for your effort $\endgroup$ – tintin Jun 27 '13 at 1:31
  • $\begingroup$ @tintin You're welcome. I extended my answer; I hope you find the addition informative. $\endgroup$ – Mr.Wizard Jun 27 '13 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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