home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////
- //
- // CODE TO DRAW A BEZIER CURVE
- // BEZIER.C
- //
- //////////////////////////////
-
- #include <dos.h>
- #include <graphics.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <math.h>
-
-
- typedef struct {
- float x,y;
- } Point2D;
-
- void InitGraphics(void);
- void WorldToDevice(float wx, float wy, int * dx, int * dy);
- void DrawAPoint2D(Point2D p);
- void DrawALine(Point2D p1, Point2D p2);
- float Bernstein(int c, float u);
-
-
- float WxLeft, WxRight, WyTop, WyBottom;
- int DyMax, DxMax, DyMin, DxMin;
-
-
- void main(void)
- {
- int i;
- float t, B, step;
- Point2D c, c_old, pnt, p[4];
-
- InitGraphics();
-
- WxLeft = -3;
- WxRight = 3;
- WyTop = 3;
- WyBottom = -3;
-
- DyMin = 0;
- DxMin = 0;
- DyMax = getmaxy();
- DxMax = getmaxy();
-
-
- setviewport((getmaxx()-getmaxy())/2,0,getmaxx(),getmaxy(),1);
-
- setbkcolor(WHITE);
- cleardevice();
- setcolor(BLUE);
-
- p[0].x = -1.0;
- p[0].y = 0.0;
-
- p[1].x = -0.3;
- p[1].y = 2.0;
-
- p[2].x = 0.3;
- p[2].y = -2.0;
-
- p[3].x = 1.0;
- p[3].y = 0.0;
-
- DrawAPoint2D(p[0]);
- DrawAPoint2D(p[1]);
- DrawAPoint2D(p[2]);
- DrawAPoint2D(p[3]);
- DrawALine(p[0], p[1]);
- DrawALine(p[1], p[2]);
- DrawALine(p[2], p[3]);
-
- setlinestyle(SOLID_LINE, 1, 3);
- step = 20.0;
- for(t=0; t<=(int)step; t++) {
- c.x = c.y = 0.0;
- for(i=0;i<=3;i++) {
-
- B = Bernstein(i,t/step);
- c.x += p[i].x * B;
- c.y += p[i].y * B;
-
- }
- if(!t) c_old = c;
- else {
- DrawALine(c_old, c);
- c_old = c;
- }
- }
- getch();
- closegraph();
- }
-
- /* ================================================================== */
-
- float Bernstein(int c, float u) {
- switch(c) {
- case 0: return (pow((1.0-u),3));
- case 1: return (3.0 * u * pow((1.0-u),2));
- case 2: return (3.0 * pow(u,2) * (1.0-u));
- default: return (pow(u,3));
- }
- }
-
- void WorldToDevice(float wx, float wy, int * dx, int * dy) {
- *dx = (WxLeft - wx) * DxMax / (WxLeft - WxRight);
- *dy = (WyTop - wy) * DyMax / (WyTop - WyBottom);
- }
-
- void DrawAPoint2D(Point2D p) {
- int dx, dy;
- WorldToDevice(p.x, p.y, &dx, &dy);
- rectangle(dx-2,dy-2,dx+2,dy+2);
- }
-
- void DrawALine(Point2D p1, Point2D p2) {
- int dx1, dy1, dx2, dy2;
- WorldToDevice(p1.x, p1.y, &dx1, &dy1);
- WorldToDevice(p2.x, p2.y, &dx2, &dy2);
- line(dx1,dy1,dx2,dy2);
- }
-
-
- void InitGraphics(void)
- {
- int gdriver = DETECT, gmode, errorcode;
- initgraph(&gdriver, &gmode, "");
- errorcode = graphresult();
- if (errorcode != grOk) /* an error occurred */
- {
- printf("Graphics error: %s\n", grapherrormsg(errorcode));
- printf("Press any key to halt:");
- getch();
- exit(1); /* terminate with an error code */
- }
- setviewport(0,0,getmaxx(),getmaxy(),1);
- }
-
-
-
-
-
-
-
-
-
-
-