64,561
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const float PI = 3.1415926;
const float R = 1;
//--------------------------------------------------------------
// 功能:判断两浮点数是否相等
inline bool IsEqual(float a, float b)
{
if (fabs(a - b) <= 1E-7) return true;
return false;
}
//--------------------------------------------------------------
/*------功能:求两线段的交点,返回TRUE代表有交点,否则没有交点---*/
bool Interaction(float x1, float y1, float x2, float y2,
float x3, float y3, float x4, float y4,
float &outX, float &outY)
{
if (x2 == x1) //和Y轴平行
{
if (x3 == x4) return false;
//否则就把X1 代入第二个直线方程中
outX = x1;
outY = (y4 - y3) / (x4 - x3) * (outX - x3) + y3;
if (outX < 0 || outX > 10000 || outY < 0 || outY > 10000) return false;
if (outX < min(x3,x4) || outX > max(x3,x4)) return false;
return true;
}
if (y1 == y2) //和X轴平行
{
if (y3 == y4) return false;
outY = y1;
outX = (outY - y3) * (x4 - x3) / (y4 - y3) + x3;
if (outX < 0 || outX > 10000 || outY < 0 || outY > 10000) return false;
if (outX < min(x3,x4) || outX > max(x3,x4)) return false;
return true;
}
if (x3 == x4) //直线二与Y轴平行
{
//if (x1 == x2) return false;
outX = x3;
outY = (y2 - y1) / (x2 - x1) * (outX - x1) + y1;
if (outX < 0 || outX > 10000 || outY < 0 || outY > 10000) return false;
if (outX < min(x1, x2) || outX > max(x1, x2)) return false;
return true;
}
if (y3 == y4) //直线二与X轴平行
{
outY = y3;
outX = (outY - y1) * (x2 - x1) / (y2 - y1) + x1;
if (outX < 0 || outX > 10000 || outY < 0 || outY > 10000) return false;
if (outX < min(x1, x2) || outX > max(x1, x2)) return false;
return true;
}
float k1 = (y2 - y1) / ( x2 - x1);
float k2 = (y4 - y3) / ( x4 - x3);
if ( true == IsEqual( k1, k2) ) //两直线平行
{
return false; //没有交点
}
outX = (-k2*x3 + k1*x1 + y3 - y1) / (k1 - k2); //由两方程解得
outY = k1*outX - k1*x1 + y1;
return true;
}
//--------------------------------------------------------------
//功能:求两点的距离
inline float DistanceBetweenPoint(float x1, float y1, float x2, float y2)
{
return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
}
//--------------------------------------------------------------
//功能:求三角形面积公式
float Area(
float x1, float y1,
float x2, float y2,
float x3, float y3
)
{
float a = DistanceBetweenPoint(x1,y1,x2,y2);
float b = DistanceBetweenPoint(x1,y1,x3,y3);
float c = DistanceBetweenPoint(x2,y2,x3,y3);
float p = (a + b + c) / 2;
return sqrt(p * (p - a) * (p - b) * (p - c));
}
typedef struct
{
float x, y;
}Point;
int main()
{
//0 144 72 288 216
short angle[5];
for (int i = 0; i != 5; ++i) cin >> angle[i];
sort(angle, angle + 5);
/*
for (int i = 0; i != 5; ++i)
{
cout << angle[i] << endl;
}
cout << endl;*/
Point pt[5];
for (int i = 0; i != 5; ++i)
{
switch (angle[i])
{
case 0:
pt[i].x = R;
pt[i].y = 0;
break;
case 90:
pt[i].x = 0;
pt[i].y = R;
break;
case 180:
pt[i].x = -R;
pt[i].y = 0;
break;
case 270:
pt[i].x = 0;
pt[i].y = -R;
break;
default:
pt[i].x = R * cosf(angle[i] * PI / 180);
pt[i].y = R * sinf(angle[i] * PI / 180);
break;
}
//cout << pt[i].x << " " << pt[i].y << endl; //delete
}
//cout << endl; //delete
Point interPt[5];
Interaction(pt[0].x, pt[0].y, pt[2].x, pt[2].y,
pt[1].x, pt[1].y, pt[4].x, pt[4].y, interPt[0].x, interPt[0].y);
Interaction(pt[0].x, pt[0].y, pt[2].x, pt[2].y,
pt[1].x, pt[1].y, pt[3].x, pt[3].y, interPt[1].x, interPt[1].y);
Interaction(pt[1].x, pt[1].y, pt[3].x, pt[3].y,
pt[2].x, pt[2].y, pt[4].x, pt[4].y, interPt[2].x, interPt[2].y);
Interaction(pt[0].x, pt[0].y, pt[3].x, pt[3].y,
pt[2].x, pt[2].y, pt[4].x, pt[4].y, interPt[3].x, interPt[3].y);
Interaction(pt[0].x, pt[0].y, pt[3].x, pt[3].y,
pt[1].x, pt[1].y, pt[4].x, pt[4].y, interPt[4].x, interPt[4].y);
/*
for (int i = 0; i != 5; ++i) //delete
{
cout << interPt[i].x << " " << interPt[i].y << endl;
}
cout << endl;*/
float EArea[11];
float CArea = PI * R * R;
for (int i = 0; i != 5; ++i)
{
if (i < 4)
{
EArea[i] = (float)(angle[i + 1] - angle[i]) / 360 * CArea -
Area(0, 0, pt[i].x, pt[i].y, pt[i + 1].x, pt[i + 1].y) +
Area(interPt[i].x, interPt[i].y, pt[i].x, pt[i].y, pt[i + 1].x, pt[i + 1].y);
}
else
{
EArea[i] = (float)(360 - angle[i] + angle[0]) / 360 * CArea -
Area(0, 0, pt[i].x, pt[i].y, pt[0].x, pt[0].y) +
Area(interPt[i].x, interPt[i].y, pt[i].x, pt[i].y, pt[0].x, pt[0].y);
}
}
for (int i = 0; i != 5; ++i)
{
if (i == 0)
{
EArea[5 + i] = Area(pt[i].x, pt[i].y, interPt[4].x, interPt[4].y,
interPt[i].x, interPt[i].y);
}
else
{
EArea[5 + i] = Area(pt[i].x, pt[i].y, interPt[i - 1].x, interPt[i - 1].y,
interPt[i].x, interPt[i].y);
}
//cout << EArea[i] << endl;
}
EArea[10] = Area(interPt[0].x, interPt[0].y, interPt[1].x, interPt[1].y,
interPt[2].x, interPt[2].y) +
Area(interPt[0].x, interPt[0].y, interPt[3].x, interPt[3].y,
interPt[2].x, interPt[2].y) +
Area(interPt[0].x, interPt[0].y, interPt[3].x, interPt[3].y,
interPt[4].x, interPt[4].y);
double M = 0;
for (int i = 0; i != 11; ++i)
{
M += EArea[i];
//cout << EArea[i] << endl;
}
M /= 11;
float D = 0;
for (int i = 0; i != 11; ++i)
{
D += (EArea[i] - M) * (EArea[i] - M);
}
D /= 11;
cout.setf(cout.showpoint);
cout.precision(4);
cout.setf(ios::fixed);
cout << D << endl;
return 0;
}