#include #include #include #include int main () { double x1, y1, x2, y2, x, y; double m, l; double a, b, c, D; double d; /* Seed the random number generator by the current time. This results in different random sequences in different runs. */ srand((unsigned int)time(NULL)); /* Generate the two points randomly in the unit square */ x1 = (double)rand() / (double)RAND_MAX; y1 = (double)rand() / (double)RAND_MAX; x2 = (double)rand() / (double)RAND_MAX; y2 = (double)rand() / (double)RAND_MAX; printf("P = (%lf,%lf), Q = (%lf,%lf)\n", x1, y1, x2, y2); /* This assignment does not handle horizontal lines */ if (y1 == y2) exit(1); /* Compute the perpendicular bisector */ m = (x1 - x2) / (y2 - y1); l = (y1+y2)/2 - m*(x1+x2)/2; printf("The perpendicular bisector of PQ has equation:\n"); printf("\ty = (%lf)x + (%lf).\n", m, l); d = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); printf("Distance between P and Q is %lf\n", sqrt(d)); /* Find the two desired points on the perpendicular bisector by solving the quadratic equation ax^2 + bx + c = 0 */ a = 1 + m*m; b = 2*(m*(l-y1)-x1); c = x1*x1 + (l-y1)*(l-y1) - d; D = b*b - 4*a*c; /* Discriminant */ if (D < 0) exit(2); /* This should never happen */ D = sqrt(D); x = (-b + D) / (2*a); y = m*x + l; printf("The first point is U = (%lf,%lf)\n", x, y); d = (x1-x)*(x1-x) + (y1-y)*(y1-y); printf("Distance between P and U is %lf\n", sqrt(d)); d = (x2-x)*(x2-x) + (y2-y)*(y2-y); printf("Distance between Q and U is %lf\n", sqrt(d)); x = (-b - D) / (2*a); y = m*x + l; printf("The second point is V = (%lf,%lf)\n", x, y); d = (x1-x)*(x1-x) + (y1-y)*(y1-y); printf("Distance between P and V is %lf\n", sqrt(d)); d = (x2-x)*(x2-x) + (y2-y)*(y2-y); printf("Distance between Q and V is %lf\n", sqrt(d)); exit(0); }