/* circle.c 1.1 */ /* Copyright 1992, 1993 O'Reilly and Associates, Inc. Permission to use, copy, and modify this program is hereby granted, as long as this copyright notice appears in each copy of the program source code. */ #include "book_utils.h" #define HALF_SQRT2 0.7071 /* sqrt(2)/2 */ static void redraw( dpy, window, renderer ) Display *dpy; Window window; PEXRenderer renderer; { static PEXCoord4D control_points[] = { {.5, .3, 0, 1}, {.7, .3, 0, HALF_SQRT2}, {.7, .5, 0, 1}, {.7, .7, 0, HALF_SQRT2}, {.5, .7, 0, 1}, {.3, .7, 0, HALF_SQRT2}, {.3, .5, 0, 1}, {.3, .3, 0, HALF_SQRT2}, {.5, .3, 0, 1} }; static float knots[] = {0,0,0,1,1,2,2,3,3,4,4,4}; /* The same points, but without the homogeneous coordinate. */ static PEXCoord pgon_points[] = { {.5, .3, 0}, {.7, .3, 0}, {.7, .5, 0}, {.7, .7, 0}, {.5, .7, 0}, {.3, .7, 0}, {.3, .5, 0}, {.3, .3, 0}, {.5, .3, 0} }; int order, num_knots, i; unsigned int num_points; double tmin, tmax; PEXArrayOfCoord cpts; PEXColor color; XClearWindow( dpy, window ); PEXBeginRendering( dpy, window, renderer ); { SET_COLOR( 1, 1, 1, color ); PEXSetLineColor( dpy, renderer, PEXOCRender, PEXColorTypeRGB, &color ); /* Set the approximation criteria. */ PEXSetCurveApprox( dpy, renderer, PEXOCRender, PEXApproxConstantBetweenKnots, (double)20 ); /* Apply the weights to the control point coords. */ num_points = sizeof(control_points)/sizeof(PEXCoord4D); for ( i = 0; i < num_points; i++ ) { control_points[i].x *= control_points[i].w; control_points[i].y *= control_points[i].w; control_points[i].z *= control_points[i].w; } /* Create the curve. */ order = 3; cpts.point_4d = control_points; num_knots = sizeof(knots)/sizeof(knots[0]); tmin = knots[order-1]; tmax = knots[num_knots - order]; PEXNURBCurve( dpy, renderer, PEXOCRender, PEXRational, order, knots, num_points, cpts, tmin, tmax ); /* Show the control polygon. */ PEXSetLineType( dpy, renderer, PEXOCRender, PEXLineTypeDashed ); PEXPolyline( dpy, renderer, PEXOCRender, num_points, pgon_points ); } PEXEndRendering( dpy, renderer, True ); XFlush( dpy ); } #define WIN_GEOM "500x500+50+50" main( argc, argv ) int argc; char *argv[]; { Display *dpy; Window window; XEvent event; XVisualInfo vis_info; XColor bkgd_color; XStandardColormap cmap_info; PEXColorApproxEntry capx_info; PEXRenderer renderer; PEXRendererAttributes rattrs; PEXExtensionInfo *pexinfo; int done = 0; /* Open a display and initialize PEX. */ dpy = ora_init_pex( argv, &pexinfo ); if ( !dpy ) exit(1); /* Determine the best visual to use. */ ora_find_best_visual( dpy, &vis_info ); /* Get a standard colormap for the visual. */ if ( !ora_get_standard_colormap( dpy, &vis_info, &cmap_info ) ) { fprintf ( stderr, "Cannot find a standard colormap\n" ); exit(1); } ora_set_stdcmap_approx( &vis_info, &cmap_info, &capx_info ); /* Create the window, giving it a gray background. */ bkgd_color.red = bkgd_color.green = bkgd_color.blue = 0x5555; bkgd_color.flags = DoRed | DoGreen | DoBlue; window = ora_create_window( dpy, &vis_info, &cmap_info, WIN_GEOM, &bkgd_color ); if ( IMMED_MODE_SPT( pexinfo ) ) { renderer = ora_setup_renderer( dpy, window, &capx_info, &rattrs ); } else { fprintf( stderr, "No immediate mode.\n" ); exit(1); } /* Read and respond to X events. */ XSelectInput( dpy, window, ExposureMask | ButtonPressMask ); while ( !done ) { XNextEvent( dpy, &event ); switch ( event.type ) { case Expose: /* Flush remaining Expose events, then redraw. */ while ( XCheckTypedWindowEvent( dpy, window, Expose, &event ) ) ; /* empty statement */ redraw( dpy, window, renderer ); break; case ButtonPress: done = 1; break; } } XCloseDisplay( dpy ); return 0; }