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;
}


根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值