1. 程式人生 > >LA 4413 梅涅勞斯定理

LA 4413 梅涅勞斯定理

#include <bits/stdc++.h>
using namespace std;
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;
typedef vector<Point> Polygon;
Vector operator +(Vector A, Vector B)//
{
	return Vector(A.x + B.x, A.y + B.y);
}
Vector operator -(Point A, Point B)//
{
	return Vector(A.x - B.x , A.y - B.y);
}
Vector operator *(Vector A, double p)//
{
	return Vector(A.x * p, A.y * p);
}
Vector operator /(Vector A, double p)//
{
	return Vector(A.x / p, A.y / p);
}
bool operator <(const Point &a, const Point &b)//
{
	return a.x < b.x || (a.x == b.x && a.y < b.y);
}
const double eps = 1e-10;
int dcmp(double x)//
{
	if (fabs(x) < eps) return 0;
	else return x < 0 ? -1 : 1;
}
bool operator ==(const Point &a, const Point &b)//
{
	return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
}
double Dot(Vector A, Vector B)//
{
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A)//
{
	return sqrt(Dot(A, A));
}
int T;
Point P, Q, R;
double m1, m2, m3, n1, n2, n3;
int main(int argc, char const *argv[])
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%lf%lf%lf%lf%lf%lf", &P.x, &P.y, &Q.x, &Q.y, &R.x, &R.y);
		scanf("%lf%lf%lf%lf%lf%lf", &m1, &m2, &m3, &n1, &n2, &n3);
		double RP = Length(P - R);
		double PQ = Length(P - Q);
		double RQ = Length(Q - R);
		double DPCQ = m1 * RP / (m1 + m2) / RQ; 
		double FRBP = n2 * RQ / (n2 + n3) / PQ; 
		double QEAR = m3 * PQ / (m3 + n1) / RP; 
		double DPPA = m1 * m3 / (m1 + m2) / n1; 
		double FRCR = m1 * n2 / (n2 + n3) / m2; 
		double EQBQ = m3 * n2 / (m3 + n1) / n3; 
		double DP = (DPPA * RP + EQBQ * DPPA * PQ / QEAR + EQBQ * FRCR * DPPA * RQ / QEAR / FRBP) / (1 - EQBQ * FRCR * DPPA / QEAR / FRBP / DPCQ);
		double AR = DP / DPPA - RP;
		Point A = R + (R - P) * (AR / RP);
		double BP = FRCR / FRBP * (DP / DPCQ + RQ);
		Point B = P + (P - Q) * (BP / PQ);
		double CQ = DP / DPCQ;
		Point C = Q + (Q - R) * (CQ / RQ);
		printf("%.8lf %.8lf %.8lf %.8lf %.8lf %.8lf\n", A.x, A.y, B.x, B.y, C.x, C.y);
	}
	return 0;
}

梅涅勞斯定理應該這樣記: 分為主三角形abc和次三角形def,沿著ab,bc,ca的順序走,中間經過次要三角形的頂點,這樣就好記啦。

(AF/FB)*(BD/BC)*(CE/EA) = 1;

題圖中有三個交叉的三角形,也就是有6個關係。由此我們可以推出所有邊的比例,結合已知的三條邊,就可以算出所有長度,通過移動向量,算出ABC點。