主页 > 程序人生 > 发一个最小圆的模板,顺便测试code

发一个最小圆的模板,顺便测试code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <stdio.h>
#include <math.h>
#include<string.h>
const int maxn = 501;
const double eps = 1e-6;
struct TPoint
{
    double x, y;
    TPoint()
    {
        x=y=0;
    }
    TPoint operator-(TPoint &a)
    {
        TPoint p1;
        p1.x = x - a.x;
        p1.y = y - a.y;
        return p1;
    }
};
 
struct TCircle
{
    double r;
    TPoint centre;
    TCircle()
    {
        r=0;
        centre.x=centre.y=0;
    }
};
 
struct TTriangle
{
    TPoint t[3];
    TTriangle()
    {
        int i;
        for(i=0;i<3;i++)
        {
            t[i].x=t[i].y=0;
        }
    }
 
};
 
TCircle c;
TPoint a[maxn];
 
double distance(TPoint p1, TPoint p2)
{
    TPoint p3;
    p3.x = p2.x - p1.x;
    p3.y = p2.y - p1.y;
    return sqrt(p3.x * p3.x + p3.y * p3.y);
}
 
double triangleArea(TTriangle t)
{
    TPoint p1, p2;
    p1 = t.t[1] - t.t[0];
    p2 = t.t[2] - t.t[0];
    return fabs(p1.x * p2.y - p1.y * p2.x) / 2;
}
 
TCircle circumcircleOfTriangle(TTriangle t)
{
    // 三角形的外接圆
    TCircle tmp;
    double a, b, c, c1, c2;
    double xA, yA, xB, yB, xC, yC;
    a = distance(t.t[0], t.t[1]);
    b = distance(t.t[1], t.t[2]);
    c = distance(t.t[2], t.t[0]);
    //根据S = a * b * c / R / 4;求半径R
    tmp.r = a * b * c / triangleArea(t) / 4;
    xA = t.t[0].x; yA = t.t[0].y;
    xB = t.t[1].x; yB = t.t[1].y;
    xC = t.t[2].x; yC = t.t[2].y;
    c1 = (xA * xA + yA * yA - xB * xB - yB * yB) / 2;
    c2 = (xA * xA + yA * yA - xC * xC - yC * yC) / 2;
 
    tmp.centre.x = (c1 * (yA - yC) - c2 * (yA - yB)) /
        ((xA - xB) * (yA - yC) - (xA - xC) * (yA - yB));
    tmp.centre.y = (c1 * (xA - xC) - c2 * (xA - xB)) /
        ((yA - yB) * (xA - xC) - (yA - yC) * (xA - xB));
 
    return tmp;     
}
 
TCircle MinCircle2(int tce, TTriangle ce)
{
    TCircle tmp;
    if(tce == 0) tmp.r = -2;
    else if(tce == 1)
    {
        tmp.centre = ce.t[0];
        tmp.r = 0;
    }
    else if(tce == 2)
    {
        tmp.r = distance(ce.t[0], ce.t[1]) / 2;
        tmp.centre.x = (ce.t[0].x + ce.t[1].x) / 2;
        tmp.centre.y = (ce.t[0].y + ce.t[1].y) / 2;     
    }
    else if(tce == 3) tmp = circumcircleOfTriangle(ce);
    return tmp;
}
 
void MinCircle(int t, int tce, TTriangle ce)
{
    int i, j;
    TPoint tmp;
    c = MinCircle2(tce, ce);
    if(tce == 3) return;
    for(i = 1;i <= t;i++)
    {
        if(distance(a[i], c.centre) > c.r)
        {
            ce.t[tce] = a[i];
            MinCircle(i - 1, tce + 1, ce);
            tmp = a[i];
            for(j = i;j >= 2;j--)
            {
                a[j] = a[j - 1];
            }
            a[1] = tmp;
        }
    }
}
 
void run(int n)
{
    TTriangle ce;
    int i;
    MinCircle(n, 0, ce);
    printf("%.2lf\n", c.centre.x, c.centre.y, c.r);// 输出部分
}
 
int main()
{
    int n;
    while(scanf("%d", &n) != EOF && n)
    {
        for(int i = 1;i <= n;i++)
            scanf("%lf %lf", &a[i].x, &a[i].y);// 输入
        run(n);
    }
    return 0;
}

相关日志

, , , , ,

评论:2

参与评论
  1. 回复 kongpo0412
    09/08/24

    example6.c:9: 错误: expected specifier-qualifier-list before ‘TPoint’
    example6.c:25: 错误: expected specifier-qualifier-list before ‘TPoint’
    example6.c:35: 错误: expected specifier-qualifier-list before ‘TPoint’
    example6.c:47: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘c’
    example6.c:48: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘a’
    example6.c:50: 错误: expected ‘)’ before ‘p1’
    example6.c:58: 错误: expected ‘)’ before ‘t’
    example6.c:66: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘circumcircleOfTriangle’
    example6.c:91: 错误: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘MinCircle2’
    example6.c:110: 错误: expected declaration specifiers or ‘…’ before ‘TTriangle’
    example6.c: 在函数‘MinCircle’中:
    example6.c:113: 错误: ‘TPoint’未声明(在此函数内第一次使用)
    example6.c:113: 错误: (即使在一个函数内多次出现,每个未声明的标识符在其
    example6.c:113: 错误: 所在的函数内也只报告一次。)
    example6.c:113: 错误: expected ‘;’ before ‘tmp’
    example6.c:114: 错误: ‘c’未声明(在此函数内第一次使用)
    example6.c:114: 警告: 隐式声明函数‘MinCircle2’
    example6.c:114: 错误: ‘ce’未声明(在此函数内第一次使用)
    example6.c:118: 警告: 隐式声明函数‘distance’
    example6.c:118: 错误: ‘a’未声明(在此函数内第一次使用)
    example6.c:121: 错误: 提供给函数‘MinCircle’的实参太多
    example6.c:122: 错误: ‘tmp’未声明(在此函数内第一次使用)
    example6.c: 在函数‘run’中:
    example6.c:134: 错误: ‘TTriangle’未声明(在此函数内第一次使用)
    example6.c:134: 错误: expected ‘;’ before ‘ce’
    example6.c:136: 错误: ‘ce’未声明(在此函数内第一次使用)
    example6.c:136: 错误: 提供给函数‘MinCircle’的实参太多
    example6.c:137: 错误: ‘c’未声明(在此函数内第一次使用)
    example6.c:137: 警告: 提供给格式字符串的实参太多
    example6.c:135: 警告: 未使用的变量‘i’
    example6.c: 在函数‘main’中:
    example6.c:145: 错误: 在 C99 模式之外使用‘for’循环初始化声明
    example6.c:146: 错误: ‘a’未声明(在此函数内第一次使用)

    • 回复 混沌的云
      09/08/24

      同学这个是c++代码。。。你用c编译不错才怪。。。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

引用:0

下面所列的是引用到本博客的链接
发一个最小圆的模板,顺便测试code 来自 混沌的云
顶部