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.

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

share|improve this question
    
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 –  nikie Jun 27 '13 at 9:25
add comment

1 Answer

up vote 3 down vote accepted

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}
share|improve this answer
    
thanks, so If I dont use ?Circle, how do I get the data of circle? –  tintin Jun 27 '13 at 0:41
    
@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. –  Mr.Wizard Jun 27 '13 at 0:50
    
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 –  tintin Jun 27 '13 at 1:31
    
@tintin You're welcome. I extended my answer; I hope you find the addition informative. –  Mr.Wizard Jun 27 '13 at 5:22
add comment

Your Answer

 
discard

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

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