#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;
}