갈루아의 반서재

728x90

Spiral Squares


이번에는 사각형을 이용한 나선 그리기입니다.


> plot.new()

> plot.window(xlim = c(-1, 1), ylim = c(-1, 1), asp = 1)

> square = seq(0, 2 * pi, length = 5)[1:4] // 0에서 2π 사이를 5등분한 후 그 중 0에서부터 4개의 값만 취합니다

> n = 51 // 사각형 갯수

> r = rep(1.12, n) // 1.12를 n 만큼 반복

> r = cumprod(r) // cumprod() function returns the cumulative multiplication results.

> r = r/r[n]

> theta = seq(0, 2*pi, length = n)

> for (i in n:1) {

x = r[i] * cos(theta[i] + square)

y = r[i] * sin(theta[i] + square)

polygon(x, y, col = "gray")

}



이해가 쉽도록 각각의 과정을 떼어서 실행해보면 다음과 같습니다. 



> n=51

> r=rep(1.12,n)

> r

 [1] 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12

[15] 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12

[29] 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12

[43] 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12 1.12

> r=cumprod(r)

> r

 [1]   1.120000   1.254400   1.404928   1.573519   1.762342   1.973823

 [7]   2.210681   2.475963   2.773079   3.105848   3.478550   3.895976

[13]   4.363493   4.887112   5.473566   6.130394   6.866041   7.689966

[19]   8.612762   9.646293  10.803848  12.100310  13.552347  15.178629

[25]  17.000064  19.040072  21.324881  23.883866  26.749930  29.959922

[31]  33.555113  37.581726  42.091533  47.142517  52.799620  59.135574

[37]  66.231843  74.179664  83.081224  93.050970 104.217087 116.723137

[43] 130.729914 146.417503 163.987604 183.666116 205.706050 230.390776

[49] 258.037669 289.002190 323.682453

> r=r/r[n]

> r

 [1] 0.003460181 0.003875403 0.004340452 0.004861306 0.005444662 0.006098022

 [7] 0.006829785 0.007649359 0.008567282 0.009595356 0.010746798 0.012036414

[13] 0.013480784 0.015098478 0.016910295 0.018939530 0.021212274 0.023757747

[19] 0.026608677 0.029801718 0.033377924 0.037383275 0.041869268 0.046893580

[25] 0.052520809 0.058823307 0.065882103 0.073787956 0.082642510 0.092559612

[31] 0.103666765 0.116106777 0.130039590 0.145644341 0.163121662 0.182696261

[37] 0.204619813 0.229174190 0.256675093 0.287476104 0.321973237 0.360610025

[43] 0.403883228 0.452349215 0.506631121 0.567426856 0.635518078 0.711780248

[49] 0.797193878 0.892857143 1.000000000


> theta = seq(0, 2*pi, length=n)
> theta
 [1] 0.0000000 0.1256637 0.2513274 0.3769911 0.5026548 0.6283185 0.7539822
 [8] 0.8796459 1.0053096 1.1309734 1.2566371 1.3823008 1.5079645 1.6336282
[15] 1.7592919 1.8849556 2.0106193 2.1362830 2.2619467 2.3876104 2.5132741
[22] 2.6389378 2.7646015 2.8902652 3.0159289 3.1415927 3.2672564 3.3929201
[29] 3.5185838 3.6442475 3.7699112 3.8955749 4.0212386 4.1469023 4.2725660
[36] 4.3982297 4.5238934 4.6495571 4.7752208 4.9008845 5.0265482 5.1522120
[43] 5.2778757 5.4035394 5.5292031 5.6548668 5.7805305 5.9061942 6.0318579
[50] 6.1575216 6.2831853







엑셀을 이용해 그 과정을 살펴보면 다음과 같다.

pi 3.141592654
n 51
# square=seq(0, 2*pi, length=5)
0 0
1 1.570796327
2 3.141592654
3 4.71238898
4 6.283185307


  x y x y x y x y x y
i 1 1 2 2 3 3 4 4 5 5
theta=seq(0, 2*pi, length=n) 0.125663706 0.125663706 0.251327412 0.251327412 0.376991118 0.376991118 0.502654825 0.502654825 0.628318531 0.628318531
r 0.003460181 0.003460181 0.003875403 0.003875403 0.004340452 0.004340452 0.004861306 0.004861306 0.005444662 0.005444662
1 0.003432897 0.000433676 0.00375365 0.000963774 0.00403565 0.001597827 0.004259995 0.002341952 0.004404824 0.003200292
2 -0.000433676 0.003432897 -0.00096377 0.00375365 -0.00159783 0.00403565 -0.00234195 0.004259995 -0.00320029 0.004404824
3 -0.003432897 -0.000433676 -0.00375365 -0.00096377 -0.00403565 -0.00159783 -0.00425999 -0.00234195 -0.00440482 -0.00320029
4 0.000433676 -0.003432897 0.000963774 -0.00375365 0.001597827 -0.00403565 0.002341952 -0.00425999 0.003200292 -0.00440482
x=r[i]*cos(theta[i]+square) y = r[i]*sin(theta[i]+square)


엑셀로 그려보면 다음과 같다



728x90