问题见3楼!我不会,请求帮助!
[此贴子已经被作者于2004-10-07 14:36:48编辑过]
不好意思,下面是用一个Graphics g1和Pen p1绘制n边形的程序,可是运行显示不出来,用的是DrawLine画线,而如果用DrawEllipse反而有圆出来,可是我要的是多边形不是圆,请高手不吝指教!谢谢!
/*递归画多边形*/
private void button1_Click(object sender, System.EventArgs e) {
int n=5; //n为边数,5就是五边形 int level=4; //递归的深度 double size=80.0f; //最大多边形的大小 double ratio=0.4f; //下一级多边形的大小比率 Graphics g1=pictureBox1.CreateGraphics(); Pen p1=new Pen(Color.Blue); Class1 ploy=new Class1(); ploy.setdata(n); ploy.snow(g1,p1,250,220,n,level,size,ratio);
} //End button1_Click function
public class Class1 {
double[] dx=new double[20]; double[] dy=new double[20];
public void setdata(int n) { const double pi=3.14159265; double theta=2*pi/n; //把圆周分成n份 int i=0;
dx[i]=0; //数组第一个值赋0 dy[i]=0;
for(i=1;i<=n;i++) { dx[i]=Math.Sin(i*theta); dy[i]=-Math.Cos(i*theta); } } //End setdata function
public void snow(Graphics g1,Pen p1,double x,double y,int n,int level,double size,double ratio) { double xx,yy; int level1; double size1; if(level>0) for(int i=0;i<=n;i++) { xx = x + dx[i] * size; yy = y + dy[i] * size;
level1 = level - 1; size1 = size * ratio;
snow(g1,p1,xx,yy,n,level1,size1,ratio); ploygon(g1,p1,x,y,n,size); } } //End snow function
public void ploygon(Graphics g1,Pen p1,double x,double y,int n,double size) { int i,j; int x1,x2,y1,y2; for(i=0;i<=n;i++) { if(i==n) j=1; else j=i+1; x1=(int)(x+dx[i]*size)-70; y1=(int)(y+dy[i]*size)-75; x2=(int)(x+dx[i]*size)-70; y2=(int)(y+dy[i]*size)-75; g1.DrawLine(p1,x1,y1,x2,y2); //这里不知怎么的DrawEllise就画得出,DrawLine就画不出 } } //End ploygon function
}