Created
October 18, 2017 08:13
-
-
Save thinkphp/3d1d7634ec98aa4abe4b567b8f167414 to your computer and use it in GitHub Desktop.
Belonging to a point to circles implemented in C Language
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
/** | |
* Adrian Statescu <http://adrianstatescu.com> | |
* Input -> n circle and one Point | |
* Output -> belong or not on disk. | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
#define SIZE 50 | |
typedef struct TypePoint { | |
float x,//abscise | |
y;//ordonate | |
} TPoint; | |
typedef struct TypeCircle { | |
TPoint O; | |
float R; | |
} TCircle; | |
float distance(TPoint p1, TPoint p2) { | |
return (float)sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ); | |
}; | |
int belong(TCircle C, TPoint p) { | |
float dist = distance(C.O, p); | |
if( dist < C.R ) { return 1; } | |
else { return 0; } | |
} | |
TCircle circles[ SIZE ]; | |
TPoint p; | |
int main( void ) { | |
int i, //for iterator | |
n; //number of circles | |
printf("Number of circles: "); | |
scanf("%d", &n); | |
for(i = 0; i < n; ++i) { | |
printf("Circle: ", (i+1)); | |
printf("Enter C(O(x,y),R) = "); | |
scanf("%f %f %f", &circles[i].O.x, &circles[i].O.y, &circles[i].R); | |
} | |
printf("Enter coords for the Point = "); | |
scanf("%f %f", &p.x, &p.y); | |
printf("\nOutput: \n"); | |
for(i = 0; i < n; ++i) { | |
if( belong(circles[ i ], p) ) { | |
printf("%d -> C(%3.3f, %3.3f, %3.3f) \n",(i + 1), circles[i].O.x, circles[i].O.y, circles[i].R); | |
} | |
} | |
return(0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment