4
$\begingroup$

I am trying to draw the iterations of the IFS in which from an interval middle third is removed and in the resulting figure left line is shifted 1/6th to the right and right line is shifted 1/6th to the left.

The following code I tried, doesn't seems right from the output image:

    cantorRule = 
 Line[{{a_, n_}, {b_, n_}}] :> 
  With[{d = b - a, 
    np = n - 0.1`}, {Line[{{a + d/6, np}, {a + d/3 + d/6, np}}], 
    Line[{{b - d/3 - d/6, np}, {b - d/6, np}}]}]

Graphics[{CapForm["Butt"], Thickness[.05], 
  Flatten@NestList[# /. cantorRule &, Line[{{0., 0}, {1., 0}}], 6]}]

In the above code how to change the color of the left shifting and right shifting lines different in each step.enter image description here

Update:

cantorRule = 
 Line[{{a_, n_}, {b_, n_}}] :> 
  With[{d = b - a, 
    np = n - 0.1`}, {Line[{{a + d/6, np}, {a + d/3 + d/6, np}}], 
    Line[{{b - d/3 - d/6, np}, {b - d/6, np}}]}]

Graphics[{CapForm["Butt"], Thickness[.05], 
  Flatten@NestList[# /. cantorRule &, Line[{{0., 0}, {1., 0}}], 6]}]

I changed the code as above and got this image:enter image description here How to get the end points of each intervals labelled?

$\endgroup$
2
  • $\begingroup$ @HighPerformanceMark How it is used? $\endgroup$
    – Praveen
    Commented Jul 2, 2019 at 14:44
  • $\begingroup$ @HighPerformanceMark CantorMesh is only doing the middle third rule, right? $\endgroup$
    – Praveen
    Commented Jul 2, 2019 at 14:54

1 Answer 1

3
$\begingroup$

For labeling you can post-process the output to inject Texts:

label = # /. l : Line[p : {{_, _}, {_, _}}] :> {l, Black,
      Text[Rationalize[#[[1]]], Offset[{0, -20}, #], {Center, Top}] & /@ p} &;
lines = Flatten @ NestList[# /. cantorRule &, Line[{{0., 0}, {1., 0}}], 3];
label @ Graphics[{CapForm["Butt"], Thickness[.05], lines}]

enter image description here

Needless to say, if you have many layers there won't be enough space to avoid overlaps.

For coloring, you can Partition the list of lines (starting from the second) and Riffle desired colors into each pair of lines:

coloredlines = Flatten[Riffle[{Red, Yellow}, #] & /@ Partition[lines, 2, 2, {-1, 1}, {}]]
label @ Graphics[{CapForm["Butt"], Thickness[.05], coloredlines}]

enter image description here

Alternatively, use RandomColor[2] instead of {Red, Yellow} to color each piece separately:

enter image description here

$\endgroup$
2
  • $\begingroup$ Can the label font size be reduced? $\endgroup$
    – Praveen
    Commented Jul 3, 2019 at 6:55
  • 1
    $\begingroup$ @supremum, you can change Rationalize[#[[1]]] to Style[Rationalize[#[[1]]], FontSize -> 6] or to Style[Rationalize[#[[1]]], FontSize -> Scaled[.01]] and adjust the numbers (6 , 0.01) as you like. $\endgroup$
    – kglr
    Commented Jul 3, 2019 at 7:04

Your Answer

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

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