找回密码
 注册

QQ登录

只需一步,快速开始

搜索

直线插补C语言

[复制链接]
coolice 发表于 2020-1-20 11:02:16 | 显示全部楼层 |阅读模式
  1. #include "conio.h"
  2. #include "graphics.h"
  3. #include "process.h"
  4.    #define Ni_circle 0
  5.    #define Shun_circle 1
  6.    void init_graph();
  7.    void draw_Base_circle();
  8. void draw_cabu_circle();
  9.    void close_graph();
  10.    void acrroods();
  11.    static float x0,y0;
  12.    void line_cabu(), draw_line(),draw_line_cabu();
  13.    void line_cabu()        /*此函数控制直线插步两次*/
  14.      {    int i;
  15.          init_graph();
  16.          sleep(1);
  17.       for(i=0;i<2;i++)      {
  18.          line(0,120,300,120); outtextxy(310,120,"Z");
  19.          line(100,10,100,300); outtextxy(110,300,"X");
  20.          outtextxy(90,130,"O");
  21.          draw_line();
  22.          if(i==0)
  23.                draw_line_cabu(6);
  24.          else draw_line_cabu(2);
  25.        gotoxy(50,5);
  26.          getch();
  27.          cleardevice();
  28.          setcolor(WHITE);
  29.       }
  30.    }
  31.    void draw_line()/*画直线*/
  32.    {
  33.      line(100,120,600,450);
  34.       textcolor(YELLOW);
  35.       directvideo=0;
  36.      gotoxy(45,5); cprintf("Line from:X0   Y0 Z0 ");
  37.      gotoxy(45,6); cprintf("Line to :X500 Y0 Z330");
  38.      gotoxy(45,7); cprintf("Units    :Pixel");
  39.      gotoxy(45,8); cprintf("Line now:");
  40.    }
  41.    void draw_line_cabu(int step)/*关键的直线插补函数*/
  42.     {
  43.       int Xe=600,Ye=450;
  44.       float Fm,Xm=100,Ym=120;
  45.       setcolor(RED);
  46.       moveto(Xm,Ym);
  47.       while(Xm<=Xe&&Ym<=Ye)
  48.            {
  49.                Fm=(Ym-120)*(Xe-100)-(Xm-100)*(Ye-120);
  50.                if(Fm>=0)
  51.                         Xm=Xm+step;
  52.                else
  53.                         Ym=Ym+step;
  54.                 lineto(Xm,Ym);
  55.                 gotoxy(55,8); printf("X%3.0f   Y0 Z%3.0f",Xm-100,Ym-120);
  56.                 delay(1100);
  57.             }
  58.      }
  59.      /* 圆插补部分的函数区*/
  60.     void init_graph()            /*图形系统初始化*/
  61.    {
  62.       int gdrive=DETECT,gmode;
  63.       initgraph(&gdrive,&gmode,"");
  64.       cleardevice();
  65.    }
  66.    void acrroods()               /*屏幕中心坐标*/
  67.    {
  68.       x0=getmaxx()/2;
  69.       y0=getmaxy()/2;
  70.    }   void draw_Base_circle()         /*画圆及写参数*/

  71.    {
  72.           line(x0-200,y0,x0+200,y0); outtextxy(x0+220,y0,"Z");
  73.           line(x0,y0-180,x0,y0+180); outtextxy(x0+10,y0+180,"X");
  74.           outtextxy(x0-10,y0+10,"O");
  75.           circle(x0,y0,150);
  76.           textcolor(YELLOW);
  77.           directvideo=0;
  78.           gotoxy(46,2);cprintf("Circle start:X0 Y0 Z150");
  79.           gotoxy(46,3);cprintf("Circle end :X0 Y0 Z150");
  80.           gotoxy(46,4);cprintf("Units       :Pixel");
  81.           gotoxy(46,5);cprintf("Circle now:");
  82.    }
  83.     void close_graph()             /*关图形系统*/
  84.    {
  85.      closegraph();
  86.    }
  87.    void draw_cabu_circle(int sstep,int Directory)/*关键的圆插补函数*/
  88.    {
  89.      int flag=0;
  90.      float Fm,Xm,Ym;
  91.      Xm=x0+150; Ym=y0;
  92.      moveto(Xm,Ym);
  93.      setcolor(RED);
  94.      while(1)   /*分象限,顺圆和逆圆讨论*/
  95.      {
  96.          Fm=(Xm-x0)*(Xm-x0)+(Ym-y0)*(Ym-y0)-150*150;/*圆判断公式*/
  97.            if(Fm>=0){
  98.               if(!Directory){ /*逆圆判断*/
  99.                    if(Xm>=x0&&Ym<=y0)
  100.                        {
  101.                           if(flag)    break;   /*if语句判断象限,以下一样*/
  102.                             else       Xm=Xm-sstep;
  103.                         }
  104.                     if(Xm<=x0&&Ym<=y0)
  105.                         {
  106.                           flag=1;      Ym=Ym+sstep;
  107.                         }
  108.                      if(Xm<=x0&&Ym>=y0)
  109.                                           Xm=Xm+sstep;
  110.                      if(Xm>=x0&&Ym>=y0)
  111.                                           Ym=Ym-sstep;
  112.                                   }
  113.              else {                    /*it is Directory's else*/
  114.                       if(Xm>x0&&Ym<y0)
  115.                                         Ym=Ym+sstep;
  116.                      if(Xm<=x0&&Ym<=y0)
  117.                                          Xm=Xm+sstep;
  118.                      if(Xm<x0&&Ym>y0) {
  119.                             flag=1;     Ym=Ym-sstep;}
  120.                      if(Xm>=x0&&Ym>=y0) {
  121.                              if(flag) break;
  122.                                           Xm=Xm-sstep;}
  123.                       }
  124.                }
  125.            else{        /*it is Fm's else*/
  126.                 if(!Directory) {
  127.                      if(Xm>x0&&Ym<y0)
  128.                        {
  129.                           if(flag)    break;
  130.                             else       Ym=Ym-sstep;
  131.                         }
  132.                     if(Xm<=x0&&Ym<=y0)
  133.                         {
  134.                           flag=1;      Xm=Xm-sstep;
  135.                         }
  136.                      if(Xm<=x0&&Ym>=y0)
  137.                                           Ym=Ym+sstep;
  138.                      if(Xm>=x0&&Ym>=y0)
  139.                                           Xm=Xm+sstep;
  140.                                       }
  141.                 else{
  142.                       if(Xm>x0&&Ym<y0)
  143.                                         Xm=Xm+sstep;
  144.                     if(Xm<=x0&&Ym<=y0)
  145.                                          Ym=Ym-sstep;
  146.                      if(Xm<=x0&&Ym>=y0){
  147.                             flag=1;           Xm=Xm-sstep;}
  148.                      if(Xm>=x0&&Ym>=y0) {
  149.                              if(flag) break;
  150.                                 else      Ym=Ym+sstep;}
  151.                    }
  152.              }
  153.            lineto(Xm,Ym);
  154.            gotoxy(58,5); printf("X%3.0f Y0 Z%3.0f     ",Ym-y0,Xm-x0);
  155.            delay(800);
  156.          }
  157.      }
  158.      void circle_demo(int Directory)    /*控制圆插补两次*/
  159.    {
  160.      int i=0,sstep;
  161.      init_graph();
  162.      sleep(2);
  163.      acrroods(&x0,&y0);
  164.      for(i=0;i<2;i++)
  165.      {
  166.       draw_Base_circle(150);
  167.        if(i==0){
  168.             sstep=6;
  169.           draw_cabu_circle(sstep,Directory);}
  170.        else{
  171.                sstep=1;
  172.             draw_cabu_circle(sstep,Directory);}
  173.        getch();
  174.        cleardevice();
  175.        setcolor(WHITE);
  176.      }
  177.    }
  178. /* 圆插补部分的函数区结束*/
  179. main()/*主函数负责写封面和函数调用*/
  180.    {
  181.      int choice=0;
  182.      init_graph();
  183. while(choice!=4)
  184.     {
  185.            setfillstyle(1,RED);
  186.            bar(200,30,400,80);
  187.            setcolor(GREEN);
  188.            settextstyle(3,0,10);
  189.            outtextxy(220,50,"DEMO PROGRAM BY P.Y.F");
  190.            setcolor(WHITE);
  191.            settextstyle(0,0,1);
  192.            outtextxy(200,120,"1. Line demo.");
  193.            outtextxy(200,140,"2. Shun_Circle demo.");
  194.            outtextxy(200,160,"3. Ni_Circle demo.");
  195.            outtextxy(200,180,"4. Quit the program.");
  196.            outtextxy(160,200,"Please enter your choice:");   gotoxy(46,13);
  197.            scanf("%d",&choice);
  198.     switch(choice)
  199.           {
  200.             case 1: line_cabu();break;
  201.             case 2: circle_demo(Ni_circle);break;
  202.             case 3: circle_demo(Shun_circle);break;
  203.             case 4: break;
  204.             default: printf("\nChoice wrong,try again!");
  205.           }
  206.     }
  207.     close_graph();
  208. }
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-26 00:38

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表