现在的位置: 首页 > 综合 > 正文

计算几何的一些代码。。。

2014年09月05日 ⁄ 综合 ⁄ 共 1464字 ⁄ 字号 评论关闭

 具体作用都有注释的

 

?View Code C

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// darkscope.cpp : Defines the entry point for the console application.
//
 
// darkscope.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include 
#include 
#include 
#include 
const double eps=1e-8;
const double pi=3.1415926535897;
using namespace std;
struct point                    //点
{
	double x,y;
	point()
	{}
	point(double xx,double yy)
	{
		x=xx;y=yy;
	}
};
struct vect                    //向量
{
	double x,y;
	vect()
	{}
	vect(double xx,double yy)
	{
		x=xx;
		y=yy;
	}
	vect(point a,point b)
	{
		x=b.x-a.x;
		y=b.y-a.y;
	}
};
double dot_product(vect a,vect b)			//点积
{
	return a.x*b.x+a.y*b.y;
}
double cha_product(vect a,vect b)				//叉积
{
	return a.x*b.y-b.x*a.y;
}
bool cross(point a,point b,point c,point d)			//线段ab,cd是否相交,不含边界
{
	return cha_product(vect(a,c),vect(a,d))*cha_product(vect(b,c),vect(b,d))0?1:-1;
}
double square_of_polygan(vector
 a)  //计算多边形面积,a是顺时针或者逆时针的点集
{
	a.push_back(a[0]);
	double ans=0;
	for (int i=1;i k;
	k.push_back(a);k.push_back(c);k.push_back(d);
	double sacd=square_of_polygan(k);
	k.clear();k.push_back(b);k.push_back(c);k.push_back(d);
	double sbcd=square_of_polygan(k);
	point ans=point(a.x*sbcd/(sacd+sbcd)+b.x*sacd/(sacd+sbcd),a.y*sbcd/(sacd+sbcd)+b.y*sacd/(sacd+sbcd));
	return ans;
}
double angle_of_vector(vect a,vect b)                 //求两个向量的夹角,返回值为弧度数,如果要度数需要ans*180/pi
{
	return acos((a.x*b.x+a.y*b.y)/(sqrt(a.x*a.x+a.y*a.y)*sqrt(b.x*b.x+b.y*b.y)));
}

本文出自 “DarkScope从这里开始(..” 博客,请务必保留此出处http://darkscope.blog.51cto.com/4254649/989030

抱歉!评论已关闭.