码迷,mamicode.com
首页 > 其他好文 > 详细

Koch曲线

时间:2014-12-28 23:39:35      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:

Koch曲线是一种分形,完整的Koch曲线像雪花,维基百科上记录Koch曲线最早出现在海里格·冯·科赫的论文《关于一条连续而无切线,可由初等几何构作的曲线》中,它的定义如下,给定线段AB,科赫曲线可以由以下步骤生成:

  • 将线段分成三等份(AC,CD,DB)
  • 以CD为底,向外(内外随意)画一个等边三角形DMC
  • 将线段CD移去
  • 分别对AC,CM,MD,DB重复1~3

完整的Koch雪花是由一个等边三角形分别按照以上步骤得到的分形曲线。

一些关于Koch曲线的介绍:

http://en.wikipedia.org/wiki/Koch_snowflake

http://www.matrix67.com/blog/archives/243

用Java实现Koch曲线的代码Koch.java

package com.elvalad;

import java.awt.*;

/**
 * Created by elvalad on 2014/12/28.
 */
public class Koch {
    private double x1;
    private double y1;
    private double x2;
    private double y2;
    private Color color = new Color(43, 77, 219);

    /**
     * Koch曲线构造函数
     * @param x1 Koch曲线起始点横坐标
     * @param y1 Koch曲线起始点纵坐标
     * @param x2 Koch曲线终止点横坐标
     * @param y2 Koch曲线终止点纵坐标
     * @param color Koch曲线的颜色
     */
    public Koch(double x1, double y1, double x2, double y2, Color color) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.color = color;
    }

    /**
     * @param g
     */
    public void draw(Graphics g) {
        g.setColor(this.color);
        this.drawShape(g, this.x1, this.y1, this.x2, this.y2);
    }

    /**
     *
     * @param g
     * @param x1 Koch曲线起始点横坐标
     * @param y1 Koch曲线起始点纵坐标
     * @param x2 Koch曲线终止点横坐标
     * @param y2 Koch曲线终止点纵坐标
     */
    private void drawShape(Graphics g, double x1, double y1, double x2, double y2) {
        double c = 1.0;
        double x3 = 0;
        double y3 = 0;
        double x4 = 0;
        double y4 = 0;
        double x5 = 0;
        double y5 = 0;
        double l = 0;
        double alpha = 0;
        g.setColor(this.color);
        if (((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y2)) < c) {
            g.drawLine((int)x1, 500 - (int)y1, (int)x2, 500 - (int)y2);
        } else {
            x3 = x1 + (x2 - x1) / 3;
            y3 = y1 + (y2 - y1) / 3;
            x4 = x2 - (x2 - x1) / 3;
            y4 = y2 - (y2 - y1) / 3;
            l = Math.sqrt(((y2 - y1)*(y2 - y1) + (x2 - x1)*(x2 - x1))) / 3;
            alpha = Math.atan((y4 - y3) / (x4 - x3));
            if ((alpha >= 0) && (x4 - x3) < 0 ||
                    (alpha <= 0) && (x4 - x3 < 0)) {
                alpha = alpha + Math.PI;
            }
            x5 = x3 + Math.cos(alpha + Math.PI / 3)*l;
            y5 = y3 + Math.sin(alpha + Math.PI / 3)*l;
            drawShape(g, x1, y1, x3, y3);
            drawShape(g, x3, y3, x5, y5);
            drawShape(g, x5, y5, x4, y4);
            drawShape(g, x4, y4, x2, y2);
        }
    }
}

技术分享

Koch曲线

标签:

原文地址:http://www.cnblogs.com/elvalad/p/4190736.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!