http://blog.csdn.net/u010025211/article/details/25032209
开源库:http://www.cs.cmu.edu/~quake/triangle.html
下载地址: 链接:http://pan.baidu.com/s/1jIlxK6U 密码:2xgg
知识点一:平面中判断一个点是否在三角形内部。
#include <stdio.h> //m,n表示待检测点的x,y坐标,a,b,c表示三角形的三个点 bool isInTriangle(double m,double n,double ax,double bx,double cx,double ay,double by,double cy) { double u = (m-ax)*(by-ay)-(n-ay)*(bx-ax); u/=(cx-ax)*(by-ay)-(cy-ay)*(bx-ax); double v = (m-ax)*(cy-ay)-(n-ay)*(cx-ax); v/=(bx-ax)*(cy-ay)-(by-ay)*(cx-ax); return u>0&&v>0&&(u+v)<=1; } int main(void) { bool answer = true; answer = isInTriangle(0.5,0.7,0,0,1,1,0,0); printf("%s ", answer?"Point is in triangle.":"Points isn't in triangle."); answer = isInTriangle(0.5,0.3,0,0,1,1,0,0); printf("%s ", answer?"Point is in triangle.":"Points isn't in triangle."); }
知识点二、判断一点是否在三角形的外接圆内
1、外接圆的圆心
#include <stdio.h> void getCenterOfCircle(double &x,double &y,double x1,double x2,double x3,double y1,double y2,double y3) { x=(x2*x2-x1*x1+y2*y2-y1*y1)*(y3-y1)-(x3*x3-x1*x1+y3*y3-y1*y1)*(y2-y1); x/=2*(x2-x1)*(y3-y1)-2*(x3-x1)*(y2-y1); y=(x2*x2-x1*x1+y2*y2-y1*y1)*(x3-x1)-(x3*x3-x1*x1+y3*y3-y1*y1)*(x2-x1); y/=2*(y2-y1)*(x3-x1)-2*(y3-y1)*(x2-x1); } int main(void) { bool answer = true; double x=0,y=0; getCenterOfCircle(x,y,0,3,3,4,4,0); printf("%f %f ", x,y); }
2.判断点(m,n)是否在三角形的外接圆内
#include <stdio.h> void getCenterOfCircle(double &x,double &y,double x1,double x2,double x3,double y1,double y2,double y3) { x=(x2*x2-x1*x1+y2*y2-y1*y1)*(y3-y1)-(x3*x3-x1*x1+y3*y3-y1*y1)*(y2-y1); x/=2*(x2-x1)*(y3-y1)-2*(x3-x1)*(y2-y1); y=(x2*x2-x1*x1+y2*y2-y1*y1)*(x3-x1)-(x3*x3-x1*x1+y3*y3-y1*y1)*(x2-x1); y/=2*(y2-y1)*(x3-x1)-2*(y3-y1)*(x2-x1); } bool isInCircumcircle(double m,double n,double x1,double x2,double x3,double y1,double y2,double y3) { double x=0; double y=0; getCenterOfCircle(x,y,x1,x2,x3,y1,y2,y3); return (m-x)*(m-x)+(n-y)*(n-y)<=(x1-x)*(x1-x)+(y1-y)*(y1-y); } int main(void) { double x=0,y=0; bool answer = true; answer = isInCircumcircle(0,-1,0,3,3,4,4,0); printf("%s ", answer?"Point is in Circumcircle.":"Points isn't in Circumcircle."); }