当前位置:网站首页>[Fibonacci sequence and spiral are based on C language]
[Fibonacci sequence and spiral are based on C language]
2022-07-27 02:28:00 【Breezy morning】
Fibonacci sequence and spiral be based on C Language
Fibonacci sequence :fn = f(n-1)+f(n-2),(n>=3),f1=1,f2=1
Describe... In words , That is, any item is the sum of the first two items , The first is 1, The second is 1, The third item begins , Each item is the sum of the first two items .
fn=1,1,2,3,5,8,13,21,34,55,89,144,233…
Fibonacci series is also called golden section series .
Fibonacci spiral
Fibonacci spiral is shown in the figure below :
One 、 What is Fibonacci spiral ?
As shown in the figure above : Fibonacci spiral is composed of straight lines and arcs . The radius of the arc is 1,1,2,3,5,8,13…
That is, the radius of the arc is each term of the Fibonacci series in turn , The straight line starts with the original center , Take the radius of the arc as the length , A straight line connecting the center of the circle and the starting point of the arc , The direction of the straight line is : Right 、 On 、 Left 、 Next 、 Right 、 On 、 Left 、 Next 、 Right …
That is to say :
The first n=0 Straight line to the right : If the starting point of the line is (500,500), With 10 A pixel is a unit , The end of the straight line is (510,500), The center of the arc is (500,500), The starting angle of the arc is 0 degree , The ending angle is 90 degree , Radius is 10;
The first n=1 The line goes up : The starting point of the straight line is (500,500), The end is (500,490), The center of the arc is (500,500), The starting angle of the arc is 90 degree , The ending angle is 180 degree , Radius is the second term of Fibonacci series 1( Here is 10, because 10 Pixels represent 1)
The first n=2 Straight line to the left : The starting point of the straight line is (510,500), The end of the straight line is (490,500), The center of the arc is (510,500), The starting angle is 180 degree , The ending angle is 270 degree , Radius is the third term of Fibonacci series 2( Here is 20)
The first n=3 The line goes down : The starting point of the straight line is (510,490), The end of the straight line is (510,520), The center of the arc is (510,490), The starting angle of the arc is 270 degree , The ending angle is 360 degree , Radius is the third term of Fibonacci series 3( Here is 30)
The first n=4 Straight line to the right : The starting point of the straight line is (490,490), The end of the straight line is (540,490), The center of the arc is (490,490), The starting angle of the arc is 360 degree , The ending angle is 450 degree , Radius is the fourth term of Fibonacci series 5( Here is 50)
…
And so on
The first n=n term : If n % 4 = 0 , Then the straight line goes to the right , The starting point of the straight line is ( Subtract... From the abscissa of the previous term f(n-2), The ordinate of the previous term ), The end of the straight line is ( Add f(n-1), The ordinate of the previous term ), The center of the arc is the starting point of the line , The starting angle of the arc is 90n degree , The ending angle is 90(n+1) degree , The radius of the arc is the th of Fibonacci series n term f(n).
If n % 4 = 1 , Then straight up , The starting point of the straight line is ( Abscissa of the previous term , Add f(n-2)), The end of the straight line is ( Abscissa of the previous term , Subtract f(n-1)), The center of the arc is the starting point of the line , The starting angle of the arc is 90n degree , The ending angle is 90(n+1) degree , The radius of the arc is the th of Fibonacci series n term f(n).
If n % 4 = 2, Then the straight line goes to the left , The starting point of the straight line is ( Add f(n-2), The ordinate of the previous term ), The end of the straight line is ( Subtract... From the abscissa of the previous term f(n-1), The ordinate of the previous term ), The center of the arc is the starting point of the line , The starting angle of the arc is 90n degree , The ending angle is 90(n+1) degree , The radius of the arc is the th of Fibonacci series n term f(n).
If n % 4 = 3, Then straight down , The starting point of the straight line is ( Abscissa of the previous term , Subtract f(n-2)), The end of the straight line is ( Abscissa of the previous term , Add f(n-1)), The center of the arc is the starting point of the line , The starting angle of the arc is 90n degree , The ending angle is 90(n+1) degree , The radius of the arc is the th of Fibonacci series n term f(n).
Two 、 The code is as follows
#include <graphics.h>
int main()
{
initgraph(1000, 1000);
setbkcolor(WHITE);
setcolor(BLACK);
int x = 500, y = 500, x1 = 510, y1 = 500;
int a = 0, b = 10, c = 0;
for (int n = 0; x >= 0 || y >= 0; n++)
{
line(x, y, x1, y1);
arc(x, y, 90 * n, 90 * (n + 1), b);
x1 = x;
y1 = y;
switch (n % 4)
{
case 0:
y = y + a;
y1 = y1 - b;
break;
case 1:
x = x + a;
x1 = x1 - b;
break;
case 2:
y = y - a;
y1 = y1 + b;
break;
case 3:
x = x - a;
x1 = x1 + b;
break;
}
c = a + b;
a = b;
b = c;
}
getch();
closegraph();
}
summary
It's used here EGE Drawing module , The full name is Easy Graphics Engine, Thanks for the drawing module provided by the author . After the Fibonacci spiral is drawn , Like a snail .
The following is a sketch of the analysis , It's a bit messy .
边栏推荐
- CF 1333C Eugene and an array
- excel整行删除,图片一起删除
- 数字集成电路:MOS管器件章(二)
- Constant knowledge explanation of C language
- C language -- nesting of relational and logical operators, if statements, switch statements, and branch structures
- RS-485 bus communication application
- Codeforces Round #796 (Div. 2), problem: (1688C) Manipulating History
- 【斐波那契数列及螺线 基于C语言】
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- [C language] factorial implementation
猜你喜欢
随机推荐
[C language] factorial implementation
About unsafe problems such as fopen and strError encountered in vs2022 or advanced version running environment
(题意+详细思路+加注释代码) Codeforces Round #805 (Div. 3)F. Equate Multisets
Detailed source code of golang bufio reader
(super detailed version, don't know to comment at any time) codeforces round 804 (Div. 2) C the third problem
NPM reports an error, error: eperm: operation not permitted, MKDIR
数字集成电路:MOS管器件章(一)
Explain exi interrupt through the counting experiment of infrared sensor
C语言——二维数组、指针
Self introduction and planning about programming
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
【封神演绎、十五分钟让你彻底学会栈的使用!!!】
Error handling in golang
怎么判断一个数是奇数还是偶数?
离开页面的提示
静态路由基本配置 实现全网可达
RS-485总线通信应用
【用C语言绘制直角坐标系】
Guangguangzai's CSDN journey
【自此文之后,学习链表一片坦途】









