# 计算两点间的距离、点到线的距离，判断一点是否在一个圆内、一点是否在一矩形内、两圆是否相交

```#ifndef HOMEWORK16_H_2013_06_19
#define HOMEWORK16_H_2013_06_19
/*

*/

//点
typedef struct Pointer
{
double  x;
double  y;
} POINT;
extern POINT point1;
extern POINT point2;
extern POINT point3;
extern POINT point4;
extern POINT point5;
//线
typedef struct Line
{
double a;
double b;
double c;
double x;
double y;

}LINE;
extern LINE line1;

//圆
typedef struct Circle
{
double x;
double y;
double r;
}CIRCLE;
extern CIRCLE circle1;
extern CIRCLE circle2;
extern CIRCLE circle3;

//矩形
typedef struct Rect
{
double a[2];
double b[2];
double c[2];
double d[2];
}RECT;
extern RECT rect1;

double poinToPont(POINT point1,POINT point2);
double poinToLine(POINT point3,LINE line1);
double poinToCircle(POINT point4,CIRCLE circle1);
double pointToRect(POINT point5,RECT rect1);
double circleToCircle(CIRCLE circle2,CIRCLE circle3);

#endif```
```/*

*/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "homework16.h"

POINT point1;
POINT point2;
POINT point3;
POINT point4;
POINT point5;
LINE line1;
RECT rect1;
CIRCLE circle1;
CIRCLE circle2;
CIRCLE circle3;

/************************************************************************

************************************************************************/
double poinToPont(POINT point1,POINT point2)
{
double LEN1;
LEN1 = sqrt(abs((point1.x - point2.x)*(point1.x-point2.x)  + (point1.y - point2.y) * (point1.y - point2.y)));
return LEN1;
}

/************************************************************************

LINE line1   直线ax+by+c=0

************************************************************************/
double poinToLine(POINT point3,LINE line1)
{
double LEN2;
LEN2 = (abs(line1.a * point3.x + line1.b * point3.y + line1.c ) )/ (line1.a * line1.a + line1.b * line1.b);
return LEN2;
}
/************************************************************************

CIRCLE circle1 所要判断的圆

************************************************************************/
double poinToCircle(POINT point4,CIRCLE circle1)
{
double LEN3;
LEN3 = sqrt( (point4.x - circle1.x) * (point4.x - circle1.x) + (point4.y-circle1.y) * (point4.y-circle1.y) ) ;
if (LEN3<circle1.r)
{
return 1;
}
else
{
return 0;
}
}
/************************************************************************

RECT rect1  矩形

************************************************************************/
double pointToRect(POINT point5,RECT rect1)
{
long double LEN4ae,LEN4ac,LEN4ab,LEN4bc,LEN4ec;

LEN4ab = sqrt( (rect1.a[0] - rect1.b[0]) * (rect1.a[0] - rect1.b[0])+(rect1.a[1]-rect1.b[1]) * (rect1.a[1]-rect1.b[1]) ) ;
LEN4ac = sqrt( (rect1.a[0] - rect1.c[0]) * (rect1.a[0] - rect1.c[0])+(rect1.a[1]-rect1.c[1]) * (rect1.a[1]-rect1.c[1]) ) ;
LEN4bc = sqrt( (rect1.b[0] - rect1.c[0]) * (rect1.b[0] - rect1.c[0])+(rect1.b[1]-rect1.c[1]) * (rect1.b[1]-rect1.c[1]) ) ;
LEN4ae = sqrt( (rect1.a[0] - point5.x) * (rect1.a[0] - point5.x)+(rect1.a[1]-point5.y) *(rect1.a[1]-point5.y) );
LEN4ec = sqrt( (point5.x - rect1.c[0]) * (point5.x - rect1.c[0])+(point5.y-rect1.c[1]) * (point5.y-rect1.c[1]) ) ;

if (LEN4ae+LEN4ec<LEN4ab+LEN4bc && LEN4ae+LEN4ec>LEN4ac)
{
return 1;
}
else
{
return 0;
}
}
/************************************************************************

CIRCLE circle3 圆2

************************************************************************/
double circleToCircle(CIRCLE circle2,CIRCLE circle3)
{
double  LEN5;
LEN5 = sqrt( (circle2.x - circle3.x) * (circle2.x - circle3.x) + (circle2.y - circle3.y) * (circle2.y - circle3.y) ) ;
if (LEN5 < circle2.r+circle3.r && LEN5 > 0)
{
return 1;
}
else
{
return 0;
}
}```
```/*

*/

#include<stdio.h>
#include<stdlib.h>
#include "homework16.h"

double main(void)
{
//计算两点间的距离
printf("计算两点间的距离n");
printf("请输入两的坐标：（点的格式：x,y）");
scanf("%lf,%lf %lf,%lf",&point1.x,&point1.y,&point2.x,&point2.y);
printf("两点间的距离为：%.3lf",poinToPont(point1,point2));
printf("n");

//计算点到线的距离
fflush(stdin);
printf("nn计算点到线的距离n");
printf("请输入点的坐标：(x,y)");
scanf("%lf,%lf",&point3.x,&point3.y);
printf("请依次输入直线ax+by+c=0的a,b,c：");
scanf("%lf%lf%lf",&line1.a,&line1.b,&line1.c);
printf("点到线的距离为：%.3lf",poinToLine(point3,line1));
printf("n");

//计算一点是否在一个圆内
fflush(stdin);
printf("nn计算一点是否在一个圆内n");
printf("请输入点的坐标：(x,y)");
scanf("%lf,%lf",&point4.x,&point4.y);
printf("请依次输入圆的半径r以及圆心(x,y)：");
scanf("%lf%lf%lf",&circle1.r,&circle1.x,&circle1.y);
printf("在圆内为1，反之为0：%0.lf",poinToCircle(point4,circle1));
printf("n");

//判断一点是否在一矩形内
fflush(stdin);
printf("nn判断一点是否在一矩形内n");
printf("请输入点的坐标：(x,y)");
scanf("%lf,%lf",&point5.x,&point5.y);
printf("请按顺时针方向输入矩形的四个顶点a,b,c,d：");
scanf("%lf,%lf %lf,%lf %lf,%lf %lf,%lf",&rect1.a[0],&rect1.a[1],&rect1.b[0],&rect1.b[1],&rect1.c[0],&rect1.c[1],&rect1.d[0],&rect1.d[1]);
printf("在矩形内为1，反之为0：%d",pointToRect(point5, rect1) );
printf("n");

//判断两圆是否相交
fflush(stdin);
printf("nn判断两圆是否相交n");
printf("请依次输入第一个圆的半径r以及圆心(x,y)：");
scanf("%lf%lf%lf",&circle2.r,&circle2.x,&circle2.y);
printf("请依次输入第二个圆的半径r以及圆心(x,y)：");
scanf("%lf%lf%lf",&circle3.r,&circle3.x,&circle3.y);
printf("在圆内为1，反之为0：%0.lf",circleToCircle(circle2,circle3));

printf("n");
system("pause");
return 0;
}```

0 条评论

• ### 用QQ企业邮箱打造自己的个性域名邮箱

忘了之前在哪看过一篇通过QQ域名邮箱申请自己的个性域名邮箱，前两天拿来试了下，发现在使用“邮我”功能时在收件人那里显示的还是自己的QQ邮箱地址，总感觉有点不舒服...

• ### springBoot生成SQL文件-使用Hibernate5的SchemaExport实现02

在上篇springBoot生成SQL文件-使用Hibernate5的SchemaExport实现01中已经知道从Hibernate5.0.x开始通过程序生成SQ...

• ### 超级方便的boost安装方法

去http://www.boost-consulting.com/products/free 下载BoostPro 1.34....

• ### WebSocket系列之JavaScript中数字数据如何转换为二进制数据

本文主要通过对JavaScript中数字数据与二进制数据之间的转换，让读者能够了解在JavaScript中如何对数字类型（包括但不限于Number类型）进行处理...

——老子

• ### Python正则表达式的简单应用和示例演示

今天小编给大家分享的是Python正则表达式的简单应用和示例演示，将前面学习的Python正则表达式做一个概括。