標籤:

matlab怎麼把圓周率按照這種方式環形排列?


----更新----

關於存矢量圖已經找到原因, 在 export setup &> Fonts 下可以設置導出字型大小的最小值.

故下文中代碼有更新

-------

這個不難, 比較繁瑣的是參數的選取. 先上圖. (不知道圖片是不是掛了, 順便給個鏈接)

http://images2015.cnblogs.com/blog/524505/201604/524505-20160414133259488-727525125.png假設如下:

0. 字元位置由半徑 radius 和轉角 theta 確定1. 字型大小(fontsize)線性變化

2. 每個近似圓周上排布的字元數目(no)需要根據字型大小確定

3. 每兩個相鄰字元的轉角變化(theta)根據 no 確定

4. 每兩個字元間的半徑變化也要根據 no 確定

5. 單個字元的旋轉角度 rotate 由轉角決定

剩下的事情就是調參數

close all; clear all; clc;
a = [...
"3.14159265358979323846264338327950288419716939937510",...
"58209749445923078164062862089986280348253421170679",...
"82148086513282306647093844609550582231725359408128",...
"48111745028410270193852110555964462294895493038196",...
"44288109756659334461284756482337867831652712019091",...
"45648566923460348610454326648213393607260249141273",...
"72458700660631558817488152092096282925409171536436",...
"78925903600113305305488204665213841469519415116094",...
"33057270365759591953092186117381932611793105118548",...
"07446237996274956735188575272489122793818301194912",...
"98336733624406566430860213949463952247371907021798",...
"60943702770539217176293176752384674818467669405132",...
"00056812714526356082778577134275778960917363717872",...
"14684409012249534301465495853710507922796892589235",...
"42019956112129021960864034418159813629774771309960",...
"51870721134999999837297804995105973173281609631859",...
"50244594553469083026425223082533446850352619311881",...
"71010003137838752886587533208381420617177669147303",...
"59825349042875546873115956286388235378759375195778",...
"18577805321712268066130019278766111959092164201989",...
"38095257201065485863278865936153381827968230301952",...
"03530185296899577362259941389124972177528347913151",...
"55748572424541506959508295331168617278558890750983",...
"81754637464939319255060400927701671139009848824012",...
"85836160356370766010471018194295559619894676783744",...
"94482553797747268471040475346462080466842590694912",...
"93313677028989152104752162056966024058038150193511",...
"25338243003558764024749647326391419927260426992279",...
"67823547816360093417216412199245863150302861829745",...
"55706749838505494588586926995690927210797509302955",...
"32116534498720275596023648066549911988183479775356",...
"63698074265425278625518184175746728909777727938000",...
"81647060016145249192173217214772350141441973568548",...
"16136115735255213347574184946843852332390739414333",...
"45477624168625189835694855620992192221842725502542",...
"56887671790494601653466804988627232791786085784383",...
"82796797668145410095388378636095068006422512520511",...
"73929848960841284886269456042419652850222106611863",...
"06744278622039194945047123713786960956364371917287",...
"46776465757396241389086583264599581339047802759009",...
"94657640789512694683983525957098258226205224894077",...
"26719478268482601476990902640136394437455305068203",...
"49625245174939965143142980919065925093722169646151",...
"57098583874105978859597729754989301617539284681382",...
"68683868942774155991855925245953959431049972524680",...
"84598727364469584865383673622262609912460805124388",...
"43904512441365497627807977156914359977001296160894",...
"41694868555848406353422072225828488648158456028506",...
"01684273945226746767889525213852254995466672782398",...
"64565961163548862305774564980355936345681743241125",...
"15076069479451096596094025228879710893145669136867",...
"22874894056010150330861792868092087476091782493858",...
"90097149096759852613655497818931297848216829989487",...
"22658804857564014270477555132379641451523746234364",...
"54285844479526586782105114135473573952311342716610",...
"21359695362314429524849371871101457654035902799344",...
"03742007310578539062198387447808478489683321445713",...
"86875194350643021845319104848100537061468067491927",...
"81911979399520614196634287544406437451237181921799",...
"98391015919561814675142691239748940907186494231961",...
"56794520809514655022523160388193014209376213785595",...
"66389377870830390697920773467221825625996615014215",...
"03068038447734549202605414665925201497442850732518",...
"66600213243408819071048633173464965145390579626856",...
"10055081066587969981635747363840525714591028970641",...
"40110971206280439039759515677157700420337869936007",...
"23055876317635942187312514712053292819182618612586",...
"73215791984148488291644706095752706957220917567116",...
"72291098169091528017350671274858322287183520935396",...
"57251210835791513698820914442100675103346711031412",...
"67111369908658516398315019701651511685171437657618",...
"35155650884909989859982387345528331635507647918535",...
"89322618548963213293308985706420467525907091548141",...
"65498594616371802709819943099244889575712828905923",...
"23326097299712084433573265489382391193259746366730",...
"58360414281388303203824903758985243744170291327656",...
"18093773444030707469211201913020330380197621101100",...
"44929321516084244485963766983895228684783123552658",...
"21314495768572624334418930396864262434107732269780",...
"28073189154411010446823252716201052652272111660396",...
"66557309254711055785376346682065310989652691862056",...
"47693125705863566201855810072936065987648611791045",...
"33488503461136576867532494416680396265797877185560",...
"84552965412665408530614344431858676975145661406800",...
"70023787765913440171274947042056223053899456131407",...
"11270004078547332699390814546646458807972708266830",...
"63432858785698305235808933065757406795457163775254",...
"20211495576158140025012622859413021647155097925923",...
"09907965473761255176567513575178296664547791745011",...
"29961489030463994713296210734043751895735961458901",...
"93897131117904297828564750320319869151402870808599",...
"04801094121472213179476477726224142548545403321571",...
"85306142288137585043063321751829798662237172159160",...
"77166925474873898665494945011465406284336639379003",...
"97692656721463853067360965712091807638327166416274",...
"88880078692560290228472104031721186082041900042296",...
"61711963779213375751149595015660496318629472654736",...
"42523081770367515906735023507283540567040386743513",...
"62222477158915049530984448933309634087807693259939",...
"78054193414473774418426312986080998886874132604721"];
a = a(1:1400);
figure(1); hold on;
N = length(a);
fontsize_all = linspace(20,2,N);
x = zeros(N,1); y = x; rotate = x;
radius = 5000;
theta = pi/2;
for ii = 1:N;
fontsize = fontsize_all(ii);
rotate(ii) = -90+180*theta/pi;
x(ii) = radius*cos(theta);
y(ii) = radius*sin(theta);
no = (2*pi*radius)/fontsize;
radius = radius - 350*(fontsize/no) - 0.3;
theta = theta - 15*(2*pi/no);
end
h = text(x(:),y(:),a(:),"fontname","Arial","Margin",0.001,...
"Interpreter","none","HorizontalAlignment","center");
set(h,{"fontsize"},num2cell(fontsize_all)",{"rotation"},num2cell(rotate) );

set(gcf,"position",[50,50,600,600],"color","w");
axis equal;
axis(5000*[-1,1,-1,1]); axis off;

實際使用了小數點後 近1400位數據

可惜的是 Matlab 渲染效果還是不好, 保存為矢量圖後和 figure 中顯示不一樣, 因此只能截圖了, 清晰度上還有差距.

--------------------------------------------

在我的電腦上存為矢量圖或者 PDF 時字型大小小於 8 時存儲字型大小會重新設置為8. 我也不知道這是matlab的問題還是我自己系統的問題. @Falccm 提醒說可以用 print, 也是提高點陣圖清晰度一個辦法. 但目前我還沒找到小字型大小前提下出矢量圖的辦法. (已經解決)


借花獻佛一下:

graphics - How to make the digits of Pi go around in a spiral like this?

Answer of Martin Büttner :

numbers =
Translate[#, {-4.5, -10}] /@
First[First[
ImportString[ExportString[
Style[#, FontSize -&> 24, FontFamily -&> "Arial"],
"PDF"], "PDF", "TextMode" -&> "Outlines"]
]] /@ {"."}~Join~CharacterRange["0", "9"];

With[{fontsize = 0.0655, digits = 10000},
Graphics[
MapIndexed[
With[{angle = (-(#2[[1]] - 2) +
Switch[#2[[1]], 1, -0.1, 2, 0, _, 0.6]) fontsize},
With[{scale = (1 - 1.5 fontsize)^(-angle/(2 Pi))},
GeometricTransformation[
numbers[[# + 2]],
RightComposition[
ScalingTransform[{1, 1} 0.1 fontsize*scale],
TranslationTransform[{0, scale}],
RotationTransform[Pi/4 + angle]
]
]
]
] ,
Insert[First@RealDigits[Pi, 10, digits], -1, 2]
],
PlotRange -&> {{-1.1, 1.1}, {-1.1, 1.1}}
]
]

Answer of Dr. belisarius

ClearAll[height, nbr, width, nextt, w, angles, list, f, digits];
f = 50;
digits = 3000;
s = -5/4 Pi;
nbrSpacing = 12/10;
list = First@RealDigits[Pi, 10, digits];

w[nbr_] := w[nbr] = Cases[First[First[ImportString[ExportString[
Style[ToString@nbr, FontFamily -&> "Courier", FontSize -&> 10], "PDF"],
"TextMode" -&> "Outlines"]]],
FilledCurve[a__] :&> {EdgeForm[Black], FilledCurve[a]}, Infinity]

parmsFont = {Min@#, Max@#} /@ Transpose[w[1][[1, 2, 2, 1]]];
{aspectRatio, center, origHeight} = {Divide @@ (Subtract @@@ #), Mean[#[[1]]],
-Subtract @@ (#[[2]])} @parmsFont;

height[t_] := E^(t/f) - E^((t - 2 Pi)/f) // N

nbr[n_, t_] := {GeometricTransformation[w[n],
Composition[TranslationTransform[E^(t/f) {Cos[t], Sin[t]}],
ScalingTransform[height[t]/origHeight {1, 1}],
RotationTransform[t - Pi/2]]]}

width[t_] := aspectRatio height[t]
nextt[t_] := nextt[t] = (t - nbrSpacing width[t] E^(s/f)/E^(t/f))
angles = NestList[nextt, s, digits - 1] // N;

Graphics@MapThread[nbr, {list, angles}]

f = 80 時

f = 25 時

還有其他一些答案,不過都沒有這兩個效果好!


居然被舉報了,現在才知道討論mma和題主居然是「不宜公開討論的政治內容」,就問你們怕不怕?我反正是怕了

以下是修改的答案,懇請大人物手下留情啊:

你們mma有個好,MATLAB問題來得比誰都快,就不懂一個道理,那什麼大財才是Best!

而且提問用的圖的風格怎麼好像graphics - How to make the digits of ?€ go around in a spiral like this?最佳答案改了改參數生成的嘛,一定是我眼花了,一看還是匿名提問,嗯懂了啊,肯定是我眼花了啊


Something along the line..

varPi = vpa(pi,360);
piStr = char(varPi);
initialR = 10;
ax = axes;
ax.XLim = [-10,10];
ax.YLim = [-10,10];
for i = 1:numel(piStr)
theta = deg2rad(mod(i,360))*10;
rho = initialR - log(i)*1.1;
[x,y] = pol2cart(theta,rho);
hold on;
text(x,y,piStr(i),"rotation",rad2deg(theta)-90,"FontSize",rho*2);
end
hold off;


較短的mathematica代碼

numbers = ImportString[ExportString[Style[#,20,FontFamily-&>"Arial"],"PDF"]
][[1,1,2,1,1]] /@Characters@".0123456789";

Graphics@MapIndexed[(s=0.07; t=(2-Tr@#2)s;
MapAt[Map[(1-1.5s)^(-t/(2π))(0.1s RotationMatrix@t.#+{-Sin@t,Cos@t}),#,{2}] ,
#,{2}]), numbers[[Insert[RealDigits[π,10,1000][[1]],-1,2]+2]]]


推薦閱讀:

如何評價 MATLAB R2017a (Prerelease)?
請問Matlab如何計算大整數?比如 1000! ?
為什麼招程序員不考慮MATLAB技能?
各位大神,請問如何用matlab證明人耳對聲音的相位不敏感?
請通俗易懂地解釋一下guidata()的用法?以及他是如何幫助參數在GUI間傳遞的?

TAG:MATLAB |