Loading...
首页问答  

这是一个“利用递归绘画分形图案”的代码

毛毛狗
毛毛狗  发布于 2020-12-06 23:11:32 86

这是一个“利用递归绘画分形图案”的代码,但是怎么写才能把它画出来呢?

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;



public class mouse implements MouseListener {
       // int[][] points = new int[3][2];

       int[][] points = new int[2][2];

       int temp = 0;

       double arg = Math.PI / 3;

       int length = 15;

       Graphics g;



       public void setGraphics(Graphics g) {
              this.g = g;

       }



       @Override

       public void mouseClicked(MouseEvent e) {
       }



       @Override

       public void mousePressed(MouseEvent e) {


              points[temp][0] = e.getX();

              points[temp][1] = e.getY();

              temp++;

              if (temp == 2) {
                     int x1 = points[0][0];

                     int y1 = points[0][1];

                     int x2 = points[1][0];

                     int y2 = points[1][1];

                     draw(x1, y1, x2, y2, length);

                     temp %= 2;

              }

       }



       public void draw(int x1, int y1, int x3, int y3, int num) {
              int x4 = (x1 + y1 + x3 - y3) / 2;

              int y4 = (-x1 + y1 + x3 + y3) / 2;

              int x2 = (x1 - y1 + x3 + y3) / 2;

              int y2 = (x1 + y1 - x3 + y3) / 2;

              g.drawLine(x1, y1, x2, y2);

              g.drawLine(x2, y2, x3, y3);

              g.drawLine(x3, y3, x4, y4);

              g.drawLine(x4, y4, x1, y1);

              num--;

              if (num <= 0)

                     return;

              double r = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2;

              int x = (int) ((x1 * (1 / Math.tan(Math.PI / 2 - arg)) + x2 * (1 / Math.tan(arg)) + y2 - y1)

                            / ((1 / Math.tan(arg)) + (1 / Math.tan(Math.PI / 2 - arg))));

              int y = (int) ((y1 * (1 / Math.tan(Math.PI / 2 - arg)) + y2 * (1 / Math.tan(arg)) + x1 - x2)

                            / ((1 / Math.tan(arg)) + (1 / Math.tan(Math.PI / 2 - arg))));

              int tx1 = x1 - y1 + y;

              int ty1 = x1 + y1 - x;

              int tx2 = x - y + y2;

              int ty2 = x + y - x2;

              draw(tx1, ty1, x, y, num);

              draw(tx2, ty2, x2, y2, num);

       }



       public void mouseReleased(MouseEvent e) {
              // TODO Auto-generated method stub



       }



       @Override

       public void mouseEntered(MouseEvent e) {
              // TODO Auto-generated method stub



       }



       @Override

       public void mouseExited(MouseEvent e) {
              // TODO Auto-generated method stub



       }

}
添加附件:文件小于20M 文件格式必须为doc,docx,xls,xlsx,pdf,ppt,pptx,txt,zip,rar,tar,7z,gz
上传
文件格式必须为doc,docx,xls,xlsx,pdf,ppt,pptx,txt,zip,rar,tar,7z,gz,gz
去提问
写专栏

最新问题

自媒体


海思讨论群

海思讨论群 海思讨论群

Opencv双目视觉

Opencv双目视觉 Opencv双目视觉

联系我们
易百纳技术社区客服QQ: 1289459644
易百纳技术社区客服手机: 17714209983
易百纳技术社区
确定要删除此文章、专栏、评论吗?
确定
取消
易百纳技术社区