java,分形——谢宾斯基三角形

//画谢宾斯基三角形的方法
private Graphics g; //画布对象要实例化
private int x1;
private int y1;
private int x2;
private int y2;
private int x3;
private int y3;
public void coordinate(double x1,double y1,double x2,double y2,double x3,double y3,int con)
{
if(con<1){
return;
}
this.x1=(int)x1;
this.x2=(int)x2;
this.x3=(int)x3;
this.y1=(int)y1;
this.y2=(int)y2;
this.y3=(int)y3;
//只是设置颜色 并且颜色与坐标有关
g.setColor(new Color(0,this.x1%255,this.y1%255));
//画一个三角
g.drawLine(this.x1, this.y1, this.x2, this.y2);
g.drawLine(this.x1, this.y1, this.x3, this.y3);
g.drawLine(this.x2, this.y2, this.x3, this.y3);
//计算出下一次画三角的坐标
double x13=(x1+x3)/2;
double y13=(y1+y3)/2;
double x12=(x1+x2)/2;
double x23=(x2+x3)/2;
double y23=(y2+y3)/2;
//计数器
con--;
coordinate(x1,y1,x12,y1,x13,y13,con);
coordinate(x12,y1,x2,y2,x23,y23,con);
coordinate(x13,y13,x23,y23,x3,y3,con);
}[/code]

调用这个方法并传参数 实例化画布对象 可以得到


[img]http://dl2.iteye.com/upload/attachment/0086/7078/e8570d1f-d8ac-3ed8-81b2-4d417c70c2e7.png[/img]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值