随笔 - 47, 文章 - 10, 评论 - 8, 引用 - 0
数据加载中……

C语言中面向对象的尝试

C语言中面向对象的尝试

作者:周志明

 

说明:程序中关于画直线和园的算法,请参阅图形学相关知识.算法部分写在在main函数后.

运行环境:TC2.0(运行时候注意路径问题)

  1 #include < graphics.h >
  2 #include < stdio.h >
  3 #include < math.h >
  4
  5  
  6
  7 void  InitGraphSys()
  8 {
  9   int  driver,mode;
 10  detectgraph( & driver, & mode);
 11  initgraph( & driver, & mode, "" );
 12 }

 13
 14 /**/
 15 typedef  struct  point
 16 {
 17   int  x,y,color;
 18   int  ( * fconstruct)();
 19   int  ( * finput)();
 20   int  ( * fdisplay)();
 21 }
POINT, * Point;
 22
 23 int  Point_construct(Point pt, int  x, int  y, int  color)
 24 {
 25  pt -> x = x;
 26  pt -> y = y;
 27  pt -> color = color;
 28 }

 29
 30 int  Point_input(Point pt)
 31 {
 32  printf( " Input the  point info,format: Pt=x,y,color\n " );
 33   do {
 34   printf( " Pt= " );
 35   scanf( " %d,%d,%d " , & pt -> x, & pt -> y, & pt -> color);
 36  }
while (pt -> x <= 0 || pt -> x >= 639 || pt -> y <= 0 || pt -> y >= 479 || pt -> color < 0 || pt -> color > 15 );
 37  printf( " OK\n " );
 38 }

 39
 40 int  Point_display(Point pt)
 41 {
 42     putpixel(pt -> x,pt -> y,pt -> color);
 43 }

 44
 45 int  Point_init(Point pt)
 46 {
 47  pt -> x = pt -> y = pt -> color = 0 ;
 48  pt -> fconstruct = Point_construct;
 49  pt -> finput = Point_input;
 50  pt -> fdisplay = Point_display;
 51
 52 }

 53
 54 /**/
 55 typedef  struct  line
 56 {
 57  POINT p1,p2;
 58   int  ( * fconstruct)();
 59   int  ( * fcreate)();
 60   int  ( * fdisplay)();
 61 }
LINE, * Line;
 62
 63 int  Line_construct(Line ln,Point from,Point to)
 64 {
 65  ln -> p1 =* from;
 66  ln -> p2 =* to;
 67 }

 68
 69 int  Line_create(Line ln)
 70 {
 71  ln -> p1.finput( & ln -> p1);
 72  ln -> p2.finput( & ln -> p2);
 73 }

 74
 75 int  Line_display(Line ln)
 76 {
 77  dda( & ln -> p1, & ln -> p2);
 78   /* line(ln->p1.x,ln->p1.y,ln->p2.x,ln->p2.y); */
 79 }

 80
 81 int  Line_init(Line ln)
 82 {
 83  Point_init( & ln -> p1),Point_init( & ln -> p2);
 84  ln -> fconstruct = Line_construct;
 85  ln -> fcreate = Line_create;
 86  ln -> fdisplay = Line_display;
 87 }

 88
 89 /**/
 90 typedef  struct  circle
 91 {
 92  POINT pt;
 93   int  r;
 94   int  ( * fcreate)();
 95   int  ( * fdisplay)();
 96 }
CIRCLE, * Circle;
 97
 98 int  Circle_create(Circle crcl)
 99 {
100  crcl -> pt.finput( & crcl -> pt);
101  printf( " the Circle's redius:\nRedius= " );
102  scanf( " %d " , & crcl -> r);
103  printf( " OK\n " );
104
105 }

106 int  Circle_display(Circle crcl)
107 {
108  midptcircle(crcl);
109   /* circle(crcl->pt.x,crcl->pt.y,crcl->r); */
110 }

111
112 int  Circle_init(Circle crcl)
113 {
114  Point_init( & crcl -> pt);
115  crcl -> r = 0 ;
116  crcl -> fcreate = Circle_create;
117  crcl -> fdisplay = Circle_display;
118 }

119
120 /**/
121 typedef  struct  ptnode
122 {
123  POINT pt;
124   struct  ptnode *  next;
125 }
PTNODE, * Ptnode;
126
127 typedef  struct  polygon
128 {
129   int  n;
130  Ptnode pnhead;
131   int  ( * fcreate)();
132   int  ( * fdisplay)();
133 }
POLYGON, * Polygon;
134
135 int  Polygon_create(Polygon pg, int  n)
136 {
137  Ptnode pnpre = NULL,pncur = NULL;
138
139   if ( ! n)  return   1 ;
140  pg -> n = n;
141  pg -> pnhead = pnpre = pncur = (Ptnode)malloc( sizeof (PTNODE));
142  pncur -> next = NULL;
143  Point_init( & pncur -> pt);
144  pncur -> pt.finput( & pncur -> pt);
145   while ( -- n)
146   {
147   pncur = (Ptnode)malloc( sizeof (PTNODE));
148   pncur -> next = NULL;
149   pnpre -> next = pncur;
150   pnpre = pncur;
151   Point_init( & pncur -> pt);
152   pncur -> pt.finput( & pncur -> pt);
153
154  }

155   return   0 ;
156 }

157
158 int  Polygon_display(Polygon pg)
159 {
160   int  i;
161  LINE ln;
162  Ptnode head = pg -> pnhead;
163
164  Line_init( & ln);
165   for (i = 0 ;i < pg -> n - 1 ;i ++ )
166   {
167   ln.fconstruct( & ln, & head -> pt, & head -> next -> pt);
168   ln.fdisplay( & ln);
169   head = head -> next;
170  }

171  ln.fconstruct( & ln, & head -> pt, & pg -> pnhead -> pt);
172  ln.fdisplay( & ln);
173   return   0 ;
174 }

175
176 int  Polygon_init(Polygon pg)
177 {
178  pg -> n = 0 ;
179  pg -> pnhead = NULL;
180  pg -> fcreate = Polygon_create;
181  pg -> fdisplay = Polygon_display;
182 }

183
184 int  Polygon_destroy(Polygon pg)
185 {
186  Ptnode  pncur = NULL;
187   while (pg -> pnhead != NULL)
188   {
189      pncur = pg -> pnhead;
190      pg -> pnhead = pncur -> next;
191      free(pncur);
192  }

193 }

194
195  
196
197
198 /* ************************************************* */
199 void  main()
200 {
201  LINE ln;
202  CIRCLE cr;
203  POLYGON pg;
204
205   /**/
206  InitGraphSys();
207  setbkcolor( 1 );
208
209   /**/
210  Line_init( & ln);
211  ln.fcreate( & ln);
212  ln.fdisplay( & ln);
213
214  getch(),getch();
215  cleardevice();
216
217
218  Circle_init( & cr);
219  cr.fcreate( & cr);
220  cr.fdisplay( & cr);
221
222  getch(),getch();
223  cleardevice();
224
225  Polygon_init( & pg);
226  pg.fcreate( & pg, 5 );
227  pg.fdisplay( & pg);
228  Polygon_destroy( & pg);
229
230  getch(),getch();
231  closegraph();
232 }

233
234 /* ***************************************** */
235
236 int  dda(POINT  * p1,POINT  * p2)
237 {
238  POINT star = ( * p1);
239   int  dx,dy,steps;
240   float  x,y,xin = 0.0 ,yin = 0.0 ;
241
242  dx = (p2 -> -  p1 -> x);
243  dy = (p2 -> -  p1 -> y);
244  steps = (abs(dx) > abs(dy)) ? abs(dx):abs(dy);
245   if ( ! steps)
246   {
247   printf( " ERROR:the two point overlap! " );
248  }

249   else
250   {
251   xin = ( float )dx / steps;
252   yin = ( float )dy / steps;
253   x = star.x + 0.5 ;
254   y = star.y + 0.5 ;
255    do {
256    star.x = ( int )x;
257    star.y = ( int )y;
258    star.fdisplay( & star);
259    x += xin;
260    y += yin;
261   }
while ( -- steps);
262  }

263   return   0 ;
264 }

265
266 int  bresenham(Point p1,Point p2)        /* 0<=dy<=dx */
267 {
268  POINT star =* p1;
269   int  i,dx,dy;
270   float  e;
271
272  dx = p2 -> -  p1 -> x;
273  dy = p2 -> -  p1 -> y;
274  e = 2 * dy - dx;
275
276   for (i = 0 ;i < abs(dx);i ++ )
277   {
278   star.fdisplay(star.x,star.y,star.color);
279   star.x ++ ;
280    if (e > 0 )
281    {
282    star.y ++ ;
283    e += 2 * (dy - dx);
284   }

285    else
286    {
287    e += 2 * dy;
288   }

289  }

290
291   return   0 ;
292 }

293
294
295 int  transCrclCooder(Circle crcl,Point pt, int   * xx, int   * yy, int  x, int  y)
296 {
297   int  i;
298  xx[ 0 ] = x,yy[ 0 ] = y;
299  xx[ 1 ] = y,yy[ 1 ] = x;
300  xx[ 2 ] = y,yy[ 2 ] =- x;
301  xx[ 3 ] = x,yy[ 3 ] =- y;
302  xx[ 4 ] =- x,yy[ 4 ] =- y;
303  xx[ 5 ] =- y,yy[ 5 ] =- x;
304  xx[ 6 ] =- y,yy[ 6 ] = x;
305  xx[ 7 ] =- x,yy[ 7 ] = y;
306
307   for (i = 0 ;i < 8 ;i ++ )
308   {
309   Point_init( &  pt[i]);
310   pt[i].fconstruct( & pt[i],(crcl -> pt.x + xx[i]),(crcl -> pt.y + yy[i]), 14 );
311   pt[i].fdisplay( &  pt[i]);
312    /* putpixel(crcl->pt.x+xx[i],crcl->pt.y+yy[i],14); */
313  }

314
315 }

316
317 int  midptcircle(Circle crcl)
318 {
319   int  i,x = 0 ,y = crcl -> r,d = 1 - crcl -> r;
320   int  xx[ 8 ],yy[ 8 ];
321  POINT pt[ 8 ];
322
323  transCrclCooder(crcl,pt,xx,yy,x,y);
324
325   while (x < y)
326   {
327    if (d < 0 )
328    {
329    d += 2 * x + 3 ;
330    x ++ ;
331   }

332    else
333    {
334    d += 2 * (x - y) + 5 ;
335    x ++ ;y -- ;
336   }

337   transCrclCooder(crcl,pt,xx,yy,x,y);
338  }

339
340 }

341

posted on 2006-04-04 11:05 编程之道 阅读(161) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理