朱色虫居
Pages
Home
Featured Posts
2006/09/12
478_2
/*
"Points in Figures: Rectangles, Circles and Triangles"
Level:5.0
Date:2006/9/11
技巧:
長方形的判斷: x > 左上角的x && x < 右下角的x && y < 左上角的y && y > 右下角的y
圓形的判斷: (x - 圓心的x)^2 + (y - 圓心的y)^2 < 半徑^2 三角形的判斷:利用向量判斷是否線段相交, 不但較快, 也較省記憶體. */ #include
#include
struct shape{
char kind; //rectangle, circle or triangle
double x1;
double y1;
double x2;
double y2;
double x3;
double y3;
double radius;
}s[10];
double cross(double x1, double y1, double x2, double y2);
main(){
const float epsilon = 0.000001;
double t1,t2,t3,t0;
int i;
int no=1;
bool contain = false;
int len = 0;
double x,y;
char temp;
while(scanf("%c",&temp)){
if(temp == 'r'){
scanf("%lf %lf %lf %lf",&s[len].x1,&s[len].y1,
&s[len].x2,&s[len].y2);
s[len].kind = 'r';
s[len].radius = 0;
s[len].x3 = 0;
s[len].y3 = 0;
len++;
}
else if (temp == 'c'){
scanf("%lf %lf %lf", &s[len].x1, &s[len].y1, &s[len].radius);
s[len].kind = 'c';
s[len].x2 = s[len].y2 = s[len].x3 = s[len].y3 = 0;
len++;
}
else if (temp == 't'){
scanf("%lf %lf %lf %lf %lf %lf", &s[len].x1, &s[len].y1,
&s[len].x2, &s[len].y2, &s[len].x3, &s[len].y3);
s[len].kind = 't';
s[len].radius = 0;
len++;
}
else if(temp == '*')
break;
}
while(scanf("%lf %lf",&x,&y)){
if(x == 9999.9 && y == 9999.9)
break;
for(i=0;i
s[i].x1 && x < s[i].x2 && y < s[i].y1 && y > s[i].y2){
printf("Point %d is contained in figure %d\n",no,i+1);
contain = true;
}
else if (s[i].kind == 'c' &&
(x-s[i].x1)*(x-s[i].x1)+(y-s[i].y1)*(y-s[i].y1)
< s[i].radius * s[i].radius){ printf("Point %d is contained in figure %d\n",no,i+1); contain = true; } else if (s[i].kind == 't'){ if(cross(x - s[i].x1,y-s[i].y1,s[i].x2-s[i].x1,s[i].y2-s[i].y1) * cross(s[i].x3-s[i].x1,s[i].y3-s[i].y1,s[i].x2-s[i].x1,s[i].y2-s[i].y1)>=0){
if(cross(x-s[i].x2,y-s[i].y2,s[i].x3-s[i].x2,s[i].y3-s[i].y2) *
cross(s[i].x1-s[i].x2,s[i].y1-s[i].y2,s[i].x3-s[i].x2,s[i].y3-s[i].y2)>=0){
if(cross(x-s[i].x3,y-s[i].y3,s[i].x1-s[i].x3,s[i].y1-s[i].y3) *
cross(s[i].x2-s[i].x3,s[i].y2-s[i].y3,s[i].x1-s[i].x3,s[i].y1-s[i].y3)>=0){
printf("Point %d is contained in figure %d\n",no,i+1);
contain = true;
}
}
}
}
}
if(contain == false)
printf("Point %d is not contained in any figure\n",no);
no++;
contain = false;
}
}
double cross(double x1, double y1, double x2, double y2){
return x1 * y2 - y1* x2;
}
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment