#ifndef HOMEWORK16_H_2013_06_19#define HOMEWORK16_H_2013_06_19/*日期: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
/*
日期:2013-06-19*/
#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;
/************************************************************************函数名:poinToPont功能: 计算点到点的距离参数: point1,POINT point2返回值:两点间的距离************************************************************************/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;}
/************************************************************************函数名:poinToLine功能:参数:POINT point3 点的坐标 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;}/************************************************************************函数名:poinToCircle功能:判断一点是否在一圆内参数:POINT point4 所要判断的点 CIRCLE circle1 所要判断的圆返回值:点在圆内返回1,不在圆内返回0;************************************************************************/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; }}/************************************************************************函数名:pointToRect功能:判断一点是否在一矩形内参数: POINT point5 点 RECT rect1 矩形返回值:1在矩形内,0不在矩形内************************************************************************/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; }}/************************************************************************函数名:circleToCircle功能:判断两圆是否相交参数:CIRCLE circle2 圆1 CIRCLE circle3 圆2返回值:1相交,0不相交************************************************************************/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; }}
/*功能:计算两点间的距离、点到线的距离,判断一点是否在一个圆内、一点是否在一矩形内、两圆是否相交
日期:2013-06-20*/
#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;}
评论