胡言亂語:
這題原本我亂看題目,所以有重寫一次...
題目要求輸出一圖形順序,我以為先輸出完矩形、在圓形、在三角形...
因此思考了一下,要怎麼樣存圖形比較方便。分別設三個array存不一樣的圖形是笨方法。最後:
struct Point{
double x,y;
};
struct graph{
char ch; //判斷是什麼圖形
Point a,b,c;
double radius;
}P[11];
多設的變數可以不用理他,像是圓形就只需用到Point a,b,c根本用不到,但沒關係
這樣只要從P[1]~P[i]比較是否在圖形內就好
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstdio> | |
#include<cstdlib> | |
#include<cmath> | |
using namespace std; | |
int sum;//計算測試點數量 | |
bool tag;//標記是否已有在圖形內 | |
//存點 | |
struct Point{ | |
double x,y; | |
}; | |
//存圖形 | |
struct graph{ | |
char ch; | |
Point a,b,c; | |
double radius; | |
}P[11]; | |
//計算三角形面積 | |
double get_area(Point a, Point b, Point c) { | |
return fabs(0.5 * ((c.y-a.y)*(b.x-a.x) - (b.y-a.y)*(c.x-a.x))); | |
} | |
//判斷是否為矩形內 | |
void Test_rectangles(Point t,int i){ | |
if(t.x>P[i].a.x&&t.x<P[i].b.x&&t.y<P[i].a.y&&t.y>P[i].b.y){ | |
printf("Point %d is contained in figure %d\n",sum,i); | |
tag=false; | |
} | |
} | |
//判斷是否為圓形內 | |
void Test_circles(Point t,int i){ | |
if(sqrt((t.x-P[i].a.x)*(t.x-P[i].a.x)+(t.y-P[i].a.y)*(t.y-P[i].a.y))-P[i].radius<0.00001){ | |
printf("Point %d is contained in figure %d\n",sum,i); | |
tag=false; | |
} | |
} | |
//判斷是否在三角形內 | |
void Test_triangles(Point t,int i){ | |
if(get_area(t,P[i].b,P[i].c)+get_area(P[i].a,t,P[i].c)+get_area(P[i].a,P[i].b,t) | |
-get_area(P[i].a,P[i].b,P[i].c)<0.00001){ | |
printf("Point %d is contained in figure %d\n",sum,i); | |
tag=false; | |
} | |
} | |
int main(){ | |
char ch1;//讀數圖形的圖案 | |
sum = 0;//計算點數量 | |
int m=0;//計算圖形數量 | |
while(cin>>ch1){ | |
m++; | |
P[m].ch = ch1; | |
if(ch1=='*') break; | |
switch(ch1){ | |
case 'r':{ | |
cin>>P[m].a.x>>P[m].a.y>>P[m].b.x>>P[m].b.y; | |
break; | |
} | |
case 'c':{ | |
cin>>P[m].a.x>>P[m].a.y>>P[m].radius; | |
break; | |
} | |
case 't':{ | |
cin>>P[m].a.x>>P[m].a.y>>P[m].b.x>>P[m].b.y>>P[m].c.x>>P[m].c.y; | |
break; | |
} | |
} | |
} | |
Point point; | |
while(cin>>point.x>>point.y){ | |
tag=true; | |
sum++;//計算測試點數量 | |
if(point.x==9999.9&&point.y==9999.9) break; | |
for(int i=1;i<=m;i++){ | |
switch(P[i].ch){ | |
case 't':{ | |
Test_triangles(point,i); | |
break; | |
} | |
case 'c':{ | |
Test_circles(point,i); | |
break; | |
} | |
case 'r':{ | |
Test_rectangles(point,i); | |
break; | |
} | |
} | |
} | |
if(tag) | |
printf("Point %d is not contained in any figure\n",sum); | |
} | |
} |
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。