UVa 11437 - Triangle Fun

入门题,给了A,B,C三点的坐标,通过参数方程可以求得,在BC上点的坐标 p = B + ( C - B ) t , 因为D是三等分点,所以t = 1 / 3. 可以求得D = 2 / 3 * B + 1 / 3 * C,同理可以求得E,F。

这样就可以求得点P, Q, R三点的坐标。例如求P点坐标方法如下

Point GetLineIntersection(Point p, Vector v, Point Q, Vector w) { //求两条直线的交点
  Vector u = p - Q;
  double t = Cross(w, u) / Cross(v, w);
  return p + v * t;
}
Point getP(Point A, Point B, Point C) {
  Point D = B * 2 / 3 + C / 3;
  Vector AD = D - A;
  Point E = C * 2 / 3 + A / 3;
  Vector BE = E - B;
  return GetLineIntersection(A, AD, B, BE);
}   

求三角形面积的方法就好多了,我提供两种方式。

一 用的是求出QR的距离,在求出P点到直线QR的距离(注意是直线),这种方法。

二 调用Aera2这个函数。

/*************************************************************************
 > File Name: UVa11437.cpp
 > Author: AcToy
 > Mail: ycsgldy@163.com 
 > Created Time: 2013年07月18日 星期四 17时48分55秒
 ************************************************************************/

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>

using namespace std;
typedef unsigned int u32;
typedef long long i64;
typedef unsigned long long u64;
typedef vector<int> IV;
typedef vector<bool> BV;
typedef pair<int,int> II;
typedef vector<II> IIV;
#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
const int INF = 0x7FFFFFFF;
const double eps = 1E-10;
const double PI = acos(-1);
struct Point {
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) { } 
};
typedef Point Vector;
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); }
double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); }
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;
}
Point GetLineIntersection(Point p, Vector v, Point Q, Vector w) {
	Vector u = p - Q;
	double t = Cross(w, u) / Cross(v, w);
	return p + v * t;
}
double DistanceToLine(Point P, Point A, Point B) {
	Vector v1 = B - A, v2 = P - A;
	return fabs(Cross(v1, v2)) / Length(v1);
}
Point read_point() {
	double x, y;
	scanf("%lf%lf", &x, &y);
	return Point(x, y);
}
Point getP(Point A, Point B, Point C) {
	Point D = B * 2 / 3 + C / 3;
	Vector AD = D - A;
	Point E = C * 2 / 3 + A / 3;
	Vector BE = E - B;
	return GetLineIntersection(A, AD, B, BE);
}
Point getR(Point A, Point B, Point C) {
	Point D = B * 2 / 3 + C / 3;
	Vector AD = D - A;
	Point F = A * 2 / 3 + B / 3;
	Vector CF = F - C;
	return GetLineIntersection(A, AD, C, CF);
}
Point getQ(Point A, Point B, Point C) {
	Point F = A * 2 / 3 + B / 3;
	Vector CF = F - C;
	Point E = C * 2 / 3 + A / 3;
	Vector BE = E - B;
	return GetLineIntersection(B, BE, C, CF);
}
int main() {
	int Case;
	Point A, B, C, D, E, F, P, Q, R;
	scanf("%d", &Case);
	while(Case--) {
		A = read_point(), B = read_point(), C = read_point();
		P = getP(A, B, C), R = getR(A, B, C), Q = getQ(A, B, C);
		double cnt = Length(R - Q);
		printf("%.0lf\n", cnt * DistanceToLine(P, R, Q) / 2);
		//printf("%.0lf\n", Area2(P, Q, R) / 2);
	}
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值