Koch Curve(递归分治)

该博客介绍了一种使用递归算法来绘制科赫曲线的方法。科赫曲线是一种著名的分形,通过将线段分为三等份并替换中间部分为等边三角形的两倍边长来构建。代码示例中,程序接受一个整数n作为深度,递归地绘制n级别的科赫曲线。当n为0时,输出起始和结束点,随着n增加,曲线变得更加复杂。提供的代码实现了从(0,0)到(100,0)的科赫曲线,并满足精度要求,输出每个点的坐标。

题目链接

Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n.

The Koch curve is well known as a kind of fractals.

You can draw a Koch curve in the following algorithm:

Divide a given segment (p1, p2) into three equal segments.
Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.
Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).

You should start (0, 0), (100, 0) as the first segment.

Input
An integer n is given.

Output
Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4.

Constraints
0 ≤ n ≤ 6
Sample Input 1

1

Sample Output 1

0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Sample Input 2

2

Sample Output 2

0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250449
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250449
33.33333333 19.24500897
44.44444444 19.24500897
50.00000000 28.86751346
55.55555556 19.24500897
66.66666667 19.24500897
61.11111111 9.62250449
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250449
88.88888889 0.00000000
100.00000000 0.00000000

分析:
仔细分析这道题目,你会发现,n=0,n=1,n=2……都是呈现出一种相似处理方法的,那么就可以想到是用递归来实现,因为这题n<=6的,递归的时间复杂度完全是可以的;

在这里插入图片描述

规定:按照粉色箭头的方向,逆时针旋转为正,顺时针旋转为负,假设初始化时,角度angle=0度
那么,x2 = x1+lencos(angle+60);y2=y1+lensin(angle+60),angle+=60,其中len是边长长度
同理,x3 = x2+lencos(angle-120);y3=y2+lensin(angel-120),angle-=60,此时angle=-60度,也就是第三个粉色箭头的方向,因为牵扯到向量矢量性,所以存在负角度;
当然上述是n=1的情况,那么n=2的情况也是相同的,只不过当n=2时,处理完1/4的时候,angle=60,不是0了,也就是下面的代码:

	    DFS(n-1,angle);//1/4
        angle+=60.0;
        DFS(n-1,angle);//2/4
        angle-=120.0;
        DFS(n-1,angle);//3/4
        angle+=60.0;
        DFS(n-1,angle);//4/4

全部代码:

#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <cstring>
#include <stack>
#include <string>
using namespace std;

typedef long long ll;
const int N = 1e5+199;
const double Pi = acos(-1);
double len;
double x,y;
double du(double x){//计算弧度制
    return (x/180.0*Pi);
}
void DFS(int n,double arc){
    double angle = arc;
    if(n==0){
        x=x+(len*cos(du(angle)));
        y=y+(len*sin(du(angle)));
        printf("%.8f %.8f\n",x,y);
        return;
    }else{
        DFS(n-1,angle);
        angle+=60.0;
        DFS(n-1,angle);
        angle-=120.0;
        DFS(n-1,angle);
        angle+=60.0;
        DFS(n-1,angle);
    }
}
int main()
{
//    #ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//    #endif // ONLINE_JUDGE
    int n;
    scanf("%d",&n);
    len = 100.0/(double)pow(3,n);
    printf("0.00000000 0.00000000\n");
    if(n>0)
        DFS(n,0.0);
    else
        printf("100.00000000 0.00000000\n");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值