求画五角星的算法

yanghang119911 2003-11-28 09:14:07
知道五角星的中心点,并可以任意假设中心点到各个端点的距离,请问怎么画?用C#的函数
...全文
1273 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
MH2o 2003-11-29
  • 打赏
  • 举报
回复
up
FileNewExit 2003-11-29
  • 打赏
  • 举报
回复
void MyDraw(Graphics g,Point center,int radius,double[] ratio){
if(ratio == null || ratio.Length < 5)
return;
Point[] pts = new Point[10];
pts[0] = new Point(center.X,(int)(center.Y - radius * ratio[0]));
for(int i = 1; i < 5; i++){
pts[2 * i] = RotateTheta(pts[2 * (i - 1)],center,72.0);
pts[2 * i].X = (int)(center.X + (pts[2 * i].X - center.X) * ratio[i]);
pts[2 * i].Y = (int)(center.Y + (pts[2 * i].Y - center.Y) * ratio[i]);
}
//通过直线求交点的方式来取得1,3,5,7,9点坐标
pts[1] = Intersection(pts[0],pts[4],pts[2],pts[8]);
pts[3] = Intersection(pts[0],pts[4],pts[2],pts[6]);
pts[5] = Intersection(pts[2],pts[6],pts[4],pts[8]);
pts[7] = Intersection(pts[0],pts[6],pts[4],pts[8]);
pts[9] = Intersection(pts[0],pts[6],pts[2],pts[8]);

Pen pen = new Pen(new SolidBrush(Color.Blue));

g.DrawPolygon(pen,pts);
}

//求两直线交点的方法,不知道又没有库函数调用
//我的这个版本有点简单,就是线性代数中的克拉默算法,估计有没有考虑到的地方,
//你自己看着办吧
Point Intersection(Point pt1,Point pt2,Point pt3,Point pt4){

int A1 = pt1.Y - pt2.Y,B1 = pt2.X - pt1.X,C1 = pt1.Y * (pt2.X - pt1.X) - pt1.X * (pt2.Y - pt1.Y),
A2 = pt3.Y - pt4.Y,B2 = pt4.X - pt3.X,C2 = pt3.Y * (pt4.X - pt3.X) - pt3.X * (pt4.Y - pt3.Y);
//A1 * x + B1 * y = C1
//A2 * x + B2 * y = C2
int D = A1 * B2 - B1 * A2,Dx = C1 * B2 - B1 * C2,Dy = A1 * C2 - A2 * C1;
double x = Dx/(double)D,y = Dy /(double)D;
return new Point((int)x,(int)y);
}

//事实上,你还可以改写这些方法,给他增加重载版本.比如:pts[0]自定义出来,顶点与中心的连线的夹角不一定非72度,这些利用我写出来的函数很方便就可以实现了,当然,这些函数你有必要加工成你需要的~~~
wincore 2003-11-28
  • 打赏
  • 举报
回复
up
不知道微软有没有现成的函数
BossFriday 2003-11-28
  • 打赏
  • 举报
回复
收藏
lengfeng8866 2003-11-28
  • 打赏
  • 举报
回复
偶数学太次,看不懂你们的代码,对不起。。。
FileNewExit 2003-11-28
  • 打赏
  • 举报
回复
void MyDraw(Graphics g,Point center,int radius){
Point[] pts = new Point[10];

pts[0] = new Point(center.X,center.Y - radius);
pts[1] = RotateTheta(pts[0],center,36.0);
double len = radius * Math.Sin(18.0 * Math.PI/180.0)/Math.Sin(126.0 * Math.PI/180.0);
pts[1].X = (int)(center.X + len * (pts[1].X - center.X)/radius);
pts[1].Y = (int)(center.Y + len * (pts[1].Y - center.Y)/radius);
for(int i = 1; i < 5; i++){
pts[2 * i] = RotateTheta(pts[2 * (i - 1)],center,72.0);
pts[2 * i + 1] = RotateTheta(pts[2 * i - 1],center,72.0);
}
Pen pen = new Pen(new SolidBrush(Color.Blue));

g.DrawPolygon(pen,pts);
}


Point RotateTheta(Point pt,Point center,double theta){
int x = (int)(center.X + (pt.X - center.X) * Math.Cos(theta * Math.PI/180) - (pt.Y - center.Y) * Math.Sin(theta * Math.PI/180)),
y = (int)(center.Y + (pt.X - center.X) * Math.Sin(theta * Math.PI/180) + (pt.Y - center.Y) * Math.Cos(theta * Math.PI/180));
return new Point(x,y);
}
yanghang119911 2003-11-28
  • 打赏
  • 举报
回复
因为这样画还可以填充颜色
yanghang119911 2003-11-28
  • 打赏
  • 举报
回复
谢谢你的算法,我的要求还有就是在五角星的内部没有多余的线,
你的算法在内部会有交叉线出现,
能不能做到求出五角星的十个点,然后利用画多边形的函数来画呢
PointF[] pointGather={p1,p2,p3,p4,p5,p6,p7,p8,p9,p10};
g.DrawPolygon(new Pen(clr),pointGather);

请你试试行吗?
谢谢
FileNewExit 2003-11-28
  • 打赏
  • 举报
回复
试试:

void MyDraw(Graphics g,Point center,int radius){
Point[] pts = new Point[5];
//获取五角星5个顶点
pts[0] = new Point(center.X,center.Y - radius);
pts[1] = Rotate72(pts[0],center);
pts[2] = Rotate72(pts[1],center);
pts[3] = Rotate72(pts[2],center);
pts[4] = Rotate72(pts[3],center);

//简单地拉5条线
Pen pen = new Pen(new SolidBrush(Color.Blue));
g.DrawLine(pen,pts[0],pts[2]);
g.DrawLine(pen,pts[0],pts[3]);
g.DrawLine(pen,pts[1],pts[3]);
g.DrawLine(pen,pts[1],pts[4]);
g.DrawLine(pen,pts[2],pts[4]);

}

//旋转72
private Point Rotate72(Point pt,Point center){
int x = (int)(center.X + (pt.X - center.X) * Math.Cos(72.0 * Math.PI/180) - (pt.Y - center.Y) * Math.Sin(72.0 * Math.PI/180)),
y = (int)(center.Y + (pt.X - center.X) * Math.Sin(72.0 * Math.PI/180) + (pt.Y - center.Y) * Math.Cos(72.0 * Math.PI/180));
return new Point(x,y);
}
yanghang119911 2003-11-28
  • 打赏
  • 举报
回复
yanghang119911 2003-11-28
  • 打赏
  • 举报
回复
to:FileNewExit((呵呵)):
如果有兴趣你的算法还可以再改进一下,传一个放大倍数进去后,如size:
用放大倍数乘上你的半径,size*radius做为距离,不同的放大倍数画出的五角星的形状不一样,而且该算法画出的不是正五角星:)

星期天给你结分
tjq_tang 2003-11-28
  • 打赏
  • 举报
回复
楼主您自己不能推算?

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧