当前位置:网站首页>[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 .
边栏推荐
- Use of golang - sync package (waitgroup, once, mutex, rwmutex, cond, pool, map)
- Codeforces Round #810 (Div. 2), problem: (B) Party
- The problem of storing elements in TreeSet collection
- Guangguangzai's CSDN journey
- 数字集成电路:CMOS反相器(一)静态特性
- RS-485总线通信应用
- 猜拳小程序 基于Object-C 新手上路
- 全连MGRE与星型拓扑MGRE
- 今天浅讲一下转义字符【萌新版】
- Nb-iot access to cloud platform
猜你喜欢
随机推荐
Record the star user of handsomeblog
lvs+keepalived项目实战
打开编程的大门
MGRE, PPP, HDLC comprehensive experiment
Hcip OSPF knowledge summary
C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
NAT网络地址转化实验
PPOCRLabel格式的数据集操作总结。
NAT network address conversion experiment
记录第N次SQL异常
多点双向重发布和路由策略-拓扑实验
js中的数组方法和循环
求解100~200之间的素数
LabelImg标注的xml格式转yolov5
Find a specific number in an ordered array
[C language] factorial implementation
HCIP-第六天-OSPF静态大实验
聊聊自动化测试的度量指标
Today, let's talk about escape characters [cute new version]
First knowledge of C language (2)









