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

来源:互联网 发布:js将字符串数组转json 编辑:程序博客网 时间:2024/05/16 11:53

 具体作用都有注释的

 

?View Code C
 
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
// 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

原创粉丝点击