当前位置:网站首页>[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 .
边栏推荐
- 指针得真正奥义!!!
- JUC concurrent programming
- C语言的常数知识讲解
- 猜拳小程序 基于Object-C 新手上路
- MySQL course 1. simple command line -- simple record welcome to supplement and correct errors
- Full company mGRE and star topology mGRE
- 【C语言】阶乘实现
- HCIP-第三天-广域网拓扑实验
- Sort the three integers from large to small (introduce various methods in detail)
- Detailed source code of golang bufio reader
猜你喜欢

最新京东短信登录+傻妞机器人保姆级部署教程(2022/7/24)

STM32 introductory tutorial lesson 2

Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
![[C language] factorial implementation](/img/a1/9e603321d364c7f457116bd9e462aa.png)
[C language] factorial implementation

面试必问 | 一个线程从创建到消亡要经历哪些阶段?

Wechat applet: user wechat login process (attached: flow chart + source code)

静态路由基本配置 实现全网可达

通过ensp让静态路由实现全网可达

ESP8266Wi-Fi数据通讯

NB-IOT接入云平台
随机推荐
静态路由综合实验
NAT网络地址转换协议-拓扑实验
Lecture 3 - GPIO input / output library function usage and related routines
Codeforces Round #809 (Div. 2), problem: (C) Qpwoeirut And The City
【C语言程序设计】分支结构
[C language] factorial implementation
C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
HCIP-第六天-OSPF静态大实验
Can bus communication application
【自此文之后,学习链表一片坦途】
HCIP-第一天
数字集成电路:MOS管器件章(二)
全连MGRE与星型拓扑MGRE
C语言 学生信息管理系统 基于数组 可以存取到文本文件
指针得真正奥义!!!
ESP8266Wi-Fi数据通讯
Influence of pre frequency division value and automatic reload value on interrupt frequency
今天浅讲一下转义字符【萌新版】
Hcip OSPF knowledge summary
RISC-V工具链编译笔记