Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created December 12, 2008 12:16
Show Gist options
  • Save hitode909/35093 to your computer and use it in GitHub Desktop.
Save hitode909/35093 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<math.h>
typedef struct {
double x;
double y;
} position;
void print_position(position pos){
printf("(%.1lf, %.1lf)\n", pos.x, pos.y);
}
double dist_pos(position pos1, position pos2){
double diffx = pos1.x - pos2.x;
double diffy = pos1.y - pos2.y;
return sqrt(diffx * diffx + diffy * diffy);
}
typedef struct {
position center;
double radius;
} circle;
void print_circle(circle cir){
printf("<(%.1lf, %.1lf) %.1lf>\n", cir.center.x,
cir.center.y, cir.radius);
}
double area_circle(circle cir){
return cir.radius * cir.radius * M_PI ;
}
int circle_include_pos(circle cir, position pos){
return cir.radius > dist_pos(cir.center, pos);
}
int main( void ){
/* 座標を管理できるようにする */
position pos1 = {0.0, 0.0};
position pos2 = {2.0, 0.0};
print_position(pos1);
printf("距離:%.1lf\n", dist_pos(pos1, pos2) );
/* 円を作って表示 */
position pos_for_circle = {0.0, 0.0};
circle cir1 = { pos_for_circle, 2.0 };
print_circle(cir1);
/* 面積だす */
printf("面積:%.1lf\n", area_circle(cir1) );
/* ある点が円の領域内かどうかを調べる */
pos1 = (position){2.0, 2.0};
if( circle_include_pos(cir1, pos1) ){
printf("include!!!\n");
}else{
printf("not include!!!\n");
}
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment