0817 AOJ#Aizu ALDS1_5_C Koch Curve

本文介绍了一个使用递归算法绘制Koch曲线的程序实现。通过递归调用深度n来绘制Koch曲线,采用向量计算的方式确定每个顶点的位置,并确保输出精度控制在10^-4范围内。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘要
-根据图形的定义,按照要求输出绘制图形中产生的点
原题目摘要
-

-

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.

Inputundefined

An integer n is given.

Outputundefined

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.

Constraintsundefined

  • 0 ≤ n ≤ 6

Sample Input 1undefined

1

Sample Output 1undefined

0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000

Sample Input 2undefined

2

Sample Output 2undefined

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=1的形状;计算过程中使用了向量的方式生成下个一点
注意
-三角函数计算的时候用的是弧度,使用的时候要换算,也不能使用整型变量
日期
-2017 08 17
附加
-
代码
-


1
#include <cstdio>
2
#include <algorithm>
3
#include <iostream>
4
#include <cstring>
5
#include <vector>
6
#include <stack>
7
#include <queue>
8
#include <list>
9
#include <set>
10
#include <map>
11
#include <memory>
12
#include <cmath>
13
using namespace std;
14
#define MAX 2003
15
16
int n;
17
struct Vector{
18
    double r;
19
    double theta;
20
    Vector operator *(double d){
21
        Vector t;
22
        t.r=r*d;
23
        t.theta=theta;
24
        return t;
25
    }
26
};
27
28
struct Point{
29
    double x,y;
30
    Point makeP (Vector v){
31
        Point t;
32
        t.x = x+v.r*cos(v.theta);
33
        t.y = y+v.r*sin(v.theta);
34
        return t;
35
    }
36
};
37
38
void showP(Point p){
39
    printf("%.8f %.8f\n",p.x,p.y);
40
}
41
42
double arc(double c){
43
    return (c/180)*3.1415926;
44
}
45
void koch(int depth,Point p,Vector v){
46
    if(depth==0){
47
        showP(p.makeP(v));
48
    }
49
    else{
50
        Vector v13;
51
        
52
        v13=v*(1.0/3);
53
        koch(depth-1,p,v13);//1
54
        
55
        p=p.makeP(v13);
56
        v13.theta+=arc(60);
57
        koch(depth-1,p,v13);//2
58
        
59
        
60
        p=p.makeP(v13);
61
        v13.theta-=arc(120);
62
        koch(depth-1,p,v13);//3
63
        
64
        
65
        p=p.makeP(v13);
66
        v13.theta+=arc(60);
67
        koch(depth-1,p,v13);//4
68
    }
69
}
70
71
72
void get_d(){
73
74
}
75
int main(){
76
 
77
    int t;
78
79
    scanf("%d",&t);
80
    
81
    Vector v;v.theta=0,v.r=100;
82
    Point p;p.x=0,p.y=0;
83
 
84
    showP(p);
85
    koch(t,p,v);
86
   return 0;
87
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值