正五边形C语言绘制方法

正五边形C语言绘制方法
平面几何大家都学过,基本的概念就是点、线、面,三角形、矩形、圆形和椭圆形,还有就是多边形。学几何时都强调用圆规直尺三角板作图,学到角度就用到量角尺。那时我对五角星,六角星很感兴趣。后来有了PC,接触到了BASIC语言,那时就用它的内置绘图函数来绘图了。积累了一定的知识和经验,也试编过图画类型的应用程序。

现时有一个行当叫做平面设计,那些个图画的东西都叫平面设计,如商标徽标设计,标牌标识,书籍杂志封面、电影海报、平面广告等等有图画图形图像的东西的设计作品都是平面设计。其中最主要的设计元素就是图形元素。而说到图形元素,关键的东西就是图形和如何绘制图形。

说回来就是我喜欢图形的绘制,用编程语言把图形画出来。我对正五边形,正六边形情有独衷,在编制艺术信息图表时用到正五六边形图案,就试着用语言编制了这个通用函数。我感到干得不错就想和大家来分享。
我设计的方法很简单,就是依据圆内切多边形的方法。这个原理大家都知道,而关键的东西是如何填充颜色。我的填充的方法是从圆心起画五边形,画到指定的尺寸。

下面我写了3个样例来测试绘制五边形的函数。代码很简单,都是些基本的东西,加了注释,一看就明白。

五边形图形绘制的源代码如下:

Pentagon (){    //正五边形绘制方法通用函数
// a=pi/180*i*72 ; 或a=pi/180*i*2 ;  > 画五角星 
//d=0 >3点钟,  d=180 >9点钟  ,d=90 >12点钟
//d=270 >6点钟
//x, y 坐标,Ls=Length, d=Degree 起点
//绘制的方法是圆内切五边形,为方便我不计算证明,直接以半径来代替边长了。我的目的是图形,不是CAD的需要精确计算。
//4个参数 ( flaot x, y, L ;   int d ;  )  x,y圆心定位坐标,L长度=半径,d角度。
        a=pi/180-pi/180*d ;   
          x3=(float)(Ls*cos(a))+x ;   //起始0点
          y3=(float)(Ls*sin(a))+y ;
       // cs.DrawCircle(x3,y3,1);    //此点可加
            x5=x3 ;   y5=y3 ;

   for (m=1; m<=4; m++){   //五边点后4点位
          a=pi/180*m*72-pi/180*d ;   
          x4=(float)(Ls*cos(a))+x ;
          y4=(float)(Ls*sin(a))+y ;   //顺时针
       // cs.DrawCircle(x4,y4,1);    //此点可加
          cs.DrawLine (x5,y5,x4,y4);   //连线 
             x5=x4;  y5=y4 ;    }    
         cs.DrawLine (x5,y5,x3,y3);   //连线0点
}//Pentagon ()

 

4eb20cc7c97f4a39bdc33c9f108b726d.png

下面是2个测试样例:
test (){   //测试 draw pentagon  上下方向例(1)
      cs.ClearDraw (0,src);   //清屏
      clearOutput();
      cs.SetFillMode (1);//0不填色,1填色
         cs.SetColor (255,230,250,250);
      cs.DrawRect (0,2,720,780);   //back board
         cs.SetColor (255,140,140,140);
      cs.DrawRect (24,24,706,706);   //back      
         cs.SetColor (255,250,250,250);
      cs.DrawRect (20,20,700,700);   //back 
      cs.SetFillMode (0);//0不填色,1填色
         cs.SetColor (255,0,0,250);
      cs.DrawRect (20,20,700,700);   //back 
      cs.DrawRect (26,25,694,695);   //back 
 
 // 正五边形绘制,起点位置:d=0°-360°
// d=0 >3点钟,  d=180 >9点钟,
// d=90 >12点钟,  d=270 >6点钟
     cs.SetFillMode (0);//0不填色,1填色
     cs.SetStrokeWidth(2);    //线
//******** d=90 >12 点钟方向
      x=150 ; y=200 ; Ls=100 ;   d=90 ;   
        cs.SetColor(255,250,0,250);  
        Pentagon ();     //边线

         x=350 ;    //填色
        cs.SetColor(255,0,200,250);  //set fill color
        kn=(int)Ls ;     //set steps
   for (n=0; n<=kn; n++){   //fill pentagon填色
        Ls=n;            
        Pentagon ();   }

        x=550 ;
        cs.SetColor(255,0,200,0);  //set fill color
        kn=(int)Ls ;     //set steps
   for (n=0; n<=kn; n++){   //fill pentagon填色
        Ls=n;            
        Pentagon ();   }
        cs.SetColor(255,250,150,0);  //套框线
        Pentagon ();

//********d=270  6点钟方向
      x=150 ; y=450 ; Ls=100 ;   d=270 ;   
         cs.SetColor (255,0,150,250);
       cs.SetColor(255,250,0,250);  
        Pentagon ();     //边线

         x=350 ;    //填色
        cs.SetColor(255,0,200,250);  //set fill color
        kn=(int)Ls ;     //set steps
   for (n=0; n<=kn; n++){   //fill pentagon填色
        Ls=n;            
        Pentagon ();   }

        x=550 ;
        cs.SetColor(255,0,200,0);  //set fill color
        kn=(int)Ls ;     //set steps
   for (n=0; n<=kn; n++){   //fill pentagon填色
        Ls=n;            
        Pentagon ();   }
        cs.SetColor(255,250,150,0);  //套框线
        Pentagon ();

 //题标:  艺术立体字制作
      cs.SetFillMode (1);//0不填色,1填色
      cs.SetTextStyle (1);
      cs.SetStrokeWidth(1);

  cs.SetColor(255,0,0,250);   //标注
    cs.SetTextSize (24);
   cs.DrawText ("绘线不填色", 95,330) ;
   cs.DrawText ("填色 ", 327,330) ;
   cs.DrawText ("套线填色 ", 507,330) ;
      cs.SetTextSize (44);
           ss="样例:  正五边形绘制方法(1)" ;
      cs.SetColor(255,50,120,20);      //立体字
           cs.DrawText (ss,114,654);    //阴影
      cs.SetColor(255,0,250,0);
           cs.DrawText (ss,110,650);    //本字
      cs.SetFillMode (0);//0不填色,1填色
      cs.SetColor(255,250,150,0);
           cs.DrawText (ss,110,650);    //框线
    cs.Update ();
}//test ()

 

9e844fce50dc40caaa85007466dd1226.png 

test1 (){   //draw pentagon  四个方向例(2)
   cs.ClearDraw (0,src);   //清屏
      clearOutput();
      cs.SetFillMode (1);//0不填色,1填色
         cs.SetColor (255,230,250,250);
      cs.DrawRect (0,2,720,780);   //back board
         cs.SetColor (255,140,140,140);
      cs.DrawRect (24,24,706,706);   //back      
         cs.SetColor (255,250,250,250);
      cs.DrawRect (20,20,700,700);   //back 
      cs.SetFillMode (0);//0不填色,1填色
         cs.SetColor (255,0,0,250);
      cs.DrawRect (20,20,700,700);   //back 
      cs.DrawRect (26,25,694,695);   //back 
 
 // 正五边形绘制,起点位置:d=0°-360°
// d=0 >3点钟,  d=180 >9点钟,
// d=90 >12点钟,  d=270 >6点钟
     cs.SetFillMode (0);//0不填色,1填色
     cs.SetStrokeWidth(2);    //线
//******** d=0 > 3 点钟方向
      x=500 ; y=310 ; Ls=100 ;   d=0 ;   
        cs.SetColor(255,0,250,200);  // fill color
        kn=(int)Ls ;     //set steps
   for (n=0; n<=kn; n++){   //fill pentagon填色
        Ls=n;            
        Pentagon ();   }
        cs.SetColor(255,250,150,0);  //套框线
        Pentagon ();
//******** d=90 > 12 点钟方向
      x=360 ; y=170 ; Ls=100 ;   d=90 ;   
        cs.SetColor(255,0,250,200);  // fill color
        kn=(int)Ls ;     //set steps
   for (n=0; n<=kn; n++){   //fill pentagon填色
        Ls=n;            
        Pentagon ();   }
        cs.SetColor(255,250,150,0);  //套框线
        Pentagon ();
//******** d=180 > 9 点钟方向
      x=220 ; y=310 ; Ls=100 ;   d=180 ;   
        cs.SetColor(255,0,250,200);  // fill color
        kn=(int)Ls ;     //set steps
   for (n=0; n<=kn; n++){   //fill pentagon填色
        Ls=n;            
        Pentagon ();   }
        cs.SetColor(255,250,150,0);  //套框线
        Pentagon ();
//******** d=270 > 6点钟方向
       x=360 ; y=450 ; Ls=100 ;   d=270 ;   
        cs.SetColor(255,0,250,200);  // fill color
        kn=(int)Ls ;     //set steps
   for (n=0; n<=kn; n++){   //fill pentagon填色
        Ls=n;            
        Pentagon ();   }
        cs.SetColor(255,250,150,0);  //套框线
        Pentagon ();

 //题标:  艺术立体字制作
      cs.SetFillMode (1);//0不填色,1填色
      cs.SetTextStyle (1);
      cs.SetStrokeWidth(1);
      cs.SetTextSize (44);
           ss="样例:  正五边形绘制方法(2)" ;
      cs.SetColor(255,50,120,20);      //立体字
           cs.DrawText (ss,114,654);    //阴影
      cs.SetColor(255,0,250,0);
           cs.DrawText (ss,110,650);    //本字
      cs.SetFillMode (0);//0不填色,1填色
      cs.SetColor(255,250,150,0);
           cs.DrawText (ss,110,650);    //框线
    cs.Update ();
}//test1 ()
 

e76dd24956bb4eeea55950e5e066e697.png
//绘制五边形信息图表样例:
drawPentagon3 (){    //中心为五角星形
     cs.ClearDraw (0,src);   //清屏
      clearOutput();
      cs.SetFillMode (1);//0不填色,1填色
         cs.SetColor (255,230,250,250);
      cs.DrawRect (0,2,720,880);   //back board
         cs.SetColor (255,140,140,140);
      cs.DrawRect (24,24,706,806);   //back      
         cs.SetColor (255,250,250,250);
      cs.DrawRect (20,20,700,800);   //back 
      cs.SetFillMode (0);//0不填色,1填色
         cs.SetColor (255,0,0,250);
      cs.DrawRect (20,20,700,800);   //back 
      cs.DrawRect (26,25,694,795);   //back  

 // 正五边形绘制
     cs.SetFillMode (1);//0不填色,1填色
     cs.SetStrokeWidth(2);    //线

      dx=360 ;  dy=360 ;   //center
      L1=200 ;  Ls=124 ;    //12点钟方向
         cs.SetFillMode (0);//0不填色,1填色
    for (i=0; i<=200; i++){   //中心五角星
           cr=250-i; cg=i+50;  cb=100;  //渐变色
        cs.SetColor(255,cr, cg, cb);  
       cs.DrawCircle (dx,dy, i);       }

 //**** draw five Pentagon *****
     cs.SetFillMode (1);//0不填色,1填色
          d=72+90 ;  //d +90 , 12点钟方向
     for (i=0; i<=4; i++){    
          ma=pi/180*i*72+pi/180*90 ;
           x0=(float)(L1*cos(ma))+dx ;   
           y0=(float)(L1*sin(ma))+dy ;
          kn=(int)Ls ;   x=x0;  y=y0;    //set steps
    for (n=0; n<=kn; n++){   //fill pentagon
     cs.SetColor(255,200,250,250);  //set fill color
          Ls=n;            
          Pentagon ();   }  
     cs.SetColor(255,250,0,0);  //套框线
          Pentagon ();   }  

//打印标号
    for (n=0; n<=4; n++){    //draw 标号
           ma=pi/180*n*72+180 ;
           d=n*72+90;   Ls=124;
           x5=(float)(130*cos(ma))+dx ;   
           y5=(float)(130*sin(ma))+dy ;
               cs.SetColor(255,250,200,0);  
           cs.DrawCircle (x5,y5,31) ;     
               cs.SetColor(255,0,250,250);  
           cs.DrawCircle (x5,y5,29) ;     
               cs.SetTextSize (36);
           cs.SetColor(255,250,0,250);  
           ss="0"+intToString (n+1);
           cs.DrawText (ss, x5-20 ,y5+12 ) ;     }
    cs.Update ();
}//drawPentagon3 ()

//****End********

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值