/*---------------------------------------------------------------*/ /* Program chapter4_3 */ /* */ /* This program reads the coordinates of three points and then */ /* references a function to determine the coordinates of the */ /* center of a circle through the points and the radius of the */ /* circle. */ #include #include int main(void) { /* Declare variables. */ double x1, x2, x3, y1, y2, y3, m12, xc, yc, r; double circle_x_coord(double x1,double y1,double x2,double y2, double x3,double y3); /* Get user input from the keyboard. */ printf("Enter x and y coordinates for first point: \n"); scanf("%lf %lf",&x1,&y1); printf("Enter x and y coordinates for second point: \n"); scanf("%lf %lf",&x2,&y2); printf("Enter x and y coordinates for third point: \n"); scanf("%lf %lf",&x3,&y3); /* Use a function to determine the x coordinate of the center. */ xc = circle_x_coord(x1,y1,x2,y2,x3,y3); /* Compute the y coordinate of the center. */ m12 = (y2 - y1)/(x2 - x1); yc = -(1/m12)*(xc - (x1 + x2)/2) + (y1 + y2)/2; /* Compute the radius of the circle. */ r = sqrt((x1 - xc)*(x1 - xc) + (y1 - yc)*(y1 - yc)); /* Print circle parameters. */ printf("\nCenter of Circle: (%.1f,%.1f) \n",xc,yc); printf("Radius of Circle: %.1f \n",r); /* Exit program. */ return 0; } /*---------------------------------------------------------------*/ /* This function computes the x coordinate of the center of a */ /* circle given three points on the circle. */ double circle_x_coord(double x1,double y1,double x2,double y2, double x3,double y3) { /* Declare variables. */ double m12, m23, xc_num, xc_den, xc; /* Compute slopes of the two lines between points. */ m12 = (y2 - y1)/(x2 - x1); m23 = (y3 - y2)/(x3 - x2); /* Compute the x coordinate of the center of the circle. */ xc_num = m12*m23*(y1 - y3) + m23*(x1 + x2) - m12*(x2 + x3); xc_den = 2*(m23 - m12); xc = xc_num/xc_den; /* Return the x coordinate of the center. */ return xc; } /*---------------------------------------------------------------*/