题意:给定两个点A和B,求穿过这两个点的面积最小的正n边形的面积。
题解:画图就能推断,AB连线一定是n边形第0个点和第n/2个点的连线,因为这条连线是正n边形最长的一条边,这样面积才能最小,正n边形的面积 S = 边心距 * 周长 / 2。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps = 1e-9;
const double PI = acos(-1);
struct Point {
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
double dcmp(double x) {
if (fabs(x) < eps)
return 0;
return x < 0 ? -1 : 1;
}
Vector operator + (const Point& A, const Point& B) {
return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (const Point& A, const Point& B) {
return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (const Point& A, double a) {
return Vector(A.x * a, A.y * a);
}
Vector operator / (const Point& A, double a) {
return Vector(A.x / a, A.y / a);
}
double Cross(const Vector& A, const Vector& B) {
return A.x * B.y - A.y * B.x;
}
double Dot(const Vector& A, const Vector& B) {
return A.x * B.x + A.y * B.y;
}
double Length(const Vector& A) {
return sqrt(Dot(A, A));
}
bool operator < (const Point& A, const Point& B) {
return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A, const Point& B) {
return A.x == B.x && A.y == B.y;
}
double torad(double deg) {
return deg / 180.0 * PI;
}
int main() {
Point A, B;
int n;
while (scanf("%lf%lf%lf%lf%d", &A.x, &A.y, &B.x, &B.y, &n) == 5 && dcmp(A.x + A.y + B.x + B.y + n)) {
if (n % 2) {
double len = Length(A - B);
int m = n / 2;
double rad1 = torad(360.0 / n * m / 2.0);
double a1 = len / 2.0 / sin(rad1);
double rad = torad(360.0 / (2.0 * n));
double h = a1 * cos(rad);
double a = a1 * sin(rad) * 2;
double s = a * n * h / 2;
printf("%lf\n", s);
}
else {
double len = Length(A - B);
double rad = torad(360.0 / (2.0 * n));
double h = len / 2.0 * cos(rad);
double a = len * sin(rad);
double s = a * n * h / 2;
printf("%lf\n", s);
}
}
return 0;
}