UVA 11437 Triangle Fun

转载请注明出处忆梦http://blog.csdn.net/yimeng2013/article/details/17329917



计算几何入门题,熟悉使用模板。

题意:求出三角形PQR的面积。

方法一: 几何证明面积关系求解:http://blog.csdn.net/freezhanacmore/article/details/8171942

方法二: 利用叉积直接求面积:
#include<cstdio>
#include<cmath>
//定义点
struct Point
{
	double x, y;
	Point(double x = 0, double y = 0) : x(x), y(y) {}
};
typedef Point Vector; //Vector 为 Point的别名

//向量+向量=向量    点+向量=点
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);
}

//点积:两者长度乘积在乘上夹角余弦 XaXb + YaYb
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 Angle(Vector A, Vector B)
{
	return acos(Dot(A, B) / Length(A) / Length(B));
}

//叉积:两向量v和w的叉积等于v和w组成的三角形的有向面积的两倍 XaYb - XbYa
double Cross(Vector A, Vector B)
{
	return A.x*B.y - A.y*B.x;	
}

double Area2(Point A, Point B, Point C)
{
	return Cross(B-A, C-A);
}


//直线一般用参数表示 P = P0 + tv
//如何已知直线上两点A,B ;则方向向量为B-A,所以参数方程为A+(B-A)t

//两直线交点
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 read_point()
{
	Point temp;
	scanf("%lf %lf", &temp.x, &temp.y);
	return temp;
}

Point GetD(Point A, Point B)
{
	return (B-A)/3.0 + A;
}
int main ()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		Point A, B, C, D, E, F, P, Q, R;
		A = read_point();
		B = read_point();
		C = read_point();
		D = GetD(B, C);
		E = GetD(C, A);
		F = GetD(A, B);
		P = GetLineIntersection(A, D-A, B, E-B);
		R = GetLineIntersection(A, D-A, C, F-C);
		Q = GetLineIntersection(B, E-B, C, F-C);
		double ans = Area2(P, R, Q) / 2.0;
		
		printf("%.0lf\n", fabs(ans));

	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值