#include #include #define N 6 static double p[N+1]; typedef struct xy XY; struct xy { double x; double y; } ; /* * Find the point at which the line a1-a2 intersects the line b1-b2, * possibly after extending one or both lines enough that they * intersect. Return the fraction of the way from a1 to a2 this is * (ie, 0 if a1, 1 if a2). * * In METAFONT-speak, if I haven't forgotten too much of it, this is * * k1 = whatever (k1 is the return value) * k2 = whatever * p = k1[a1,a2] * p = k2[b1,b2] * * Since we don't care about p, we eliminate it: * * k1[a1,a2] = k2[b1,b2] * * Expanding into individual equations, * * (1-k1) a1x + k1 a2x = (1-k2) b1x + k2 b2x * (1-k1) a1y + k1 a2y = (1-k2) b1y + k2 b2y * * This is two equations in two unknowns. Isolating the unknowns, * * k1 (a2x - a1x) + k2 (b1x - b2x) = b1x - a1x * k1 (a2y - a1y) + k2 (b1y - b2y) = b1y - a1y * * This then gives, successively * * k2 = ((b1y - a1y) - k1 (a2y - a1y)) / (b1y - b2y) * * k1 = ((b1x - a1x) - k2 (b1x - b2x)) / (a2x - a1x) * * (b1y - a1y) - k1 (a2y - a1y) * (b1x - a1x) - ---------------------------- (b1x - b2x) * b1y - b2y * k1 = ------------------------------------------------------ * a2x - a1x * * (b1x - a1x) (b1y - b2y) - (b1y - a1y) (b1x - b2x) + k1 (a2y - a1y) (b1x - b2x) * k1 = ------------------------------------------------------------------------------ * (a2x - a1x) (b1y - b2y) * * k1 (a2x - a1x) (b1y - b2y) = (b1x - a1x) (b1y - b2y) - (b1y - a1y) (b1x - b2x) + k1 (a2y - a1y) (b1x - b2x) * * k1 [(a2x - a1x) (b1y - b2y) - (a2y - a1y) (b1x - b2x)] = (b1x - a1x) (b1y - b2y) - (b1y - a1y) (b1x - b2x) * * (b1x - a1x) (b1y - b2y) - (b1y - a1y) (b1x - b2x) * k1 = ------------------------------------------------- * (a2x - a1x) (b1y - b2y) - (a2y - a1y) (b1x - b2x) */ static double intersection(XY a1, XY a2, XY b1, XY b2) { return( (((b1.x - a1.x) * (b1.y - b2.y)) - ((b1.y - a1.y) * (b1.x - b2.x))) / (((a2.x - a1.x) * (b1.y - b2.y)) - ((a2.y - a1.y) * (b1.x - b2.x))) ); } static void find_p(void) { int i; for (i=N;i>=0;i--) { p[i] = intersection((XY){1,0},(XY){0,1},(XY){0,0},(XY){cos(i*M_PI/(2*N)),sin(i*M_PI/(2*N))}); } } static XY alpha(XY p1, double a, XY p2) { return((XY){ ((1-a)*p1.x) + (a*p2.x), ((1-a)*p1.y) + (a*p2.y) }); } static void print_line(XY p1, XY p2) { printf("%g %g moveto %g %g lineto\n",p1.x*500,p1.y*500,p2.x*500,p2.y*500); } static void draw_lines(void) { int i; XY p1; XY p2; XY p3; p1 = (XY){0,0}; p2 = (XY){1,0}; p3 = (XY){.5,sqrt(.75)}; for (i=N;i>=0;i--) { print_line(alpha(p1,p[i],p2),alpha(p3,p[i],p2)); print_line(alpha(p2,p[i],p3),alpha(p1,p[i],p3)); print_line(alpha(p3,p[i],p1),alpha(p2,p[i],p1)); } } int main(void); int main(void) { find_p(); draw_lines(); return(0); }