在Julia中无需使用递归绘制Koch曲线。

huangapple go评论65阅读模式
英文:

Drawing Koch's curve without recursion in Julia

问题

I want to draw a Koch curve in Julia without recursion. My strategy is to divide the given line into three segments using the segPoints function:

function seg_points(startPoint, endPoint, n=3)
    mids = []
    for i in 1:n-1
        push!(mids, i/n * [startPoint[1]+endPoint[1], startPoint[2]+endPoint[2]])
    end
    return mids
end

Then, the function polygon is used to find the top third point:

function polygon(p1, p2, n=3)
    x = p2[1]-p1[1]
    y = p2[2]-p1[2]
    P3 = p1 + [x * cos(pi/3) - y * sin(pi/3), x * sin(pi/3) + y * cos(pi/3)]
    return P3
end

Finally, the function koch_curve is used to draw the fractal:

function koch_curve(order::Int, startPoint = [0.0,0.0], endPoint = [1.0,0.0])
    pts=[startPoint, endPoint]
    for i in 1:order
        order_points =[]
        for j in 2:length(pts)
            a = pts[j-1]
            b = pts[j]
            c, d = seg_points(a, b)
            e = polygon(c, d)
            push!(order_points, c, d, e)
        end
        for pt in order_points
            push!(pts, pt)
        end
    end
    return pts
end

But it doesn't work. The output plot is this with order=5:
在Julia中无需使用递归绘制Koch曲线。

英文:

I want to draw a Koch curve in Julia without recursion. My strategy is to divide the given line into three segments using the segPoints function:

function seg_points(startPoint, endPoint, n=3)
		mids = []
		for i in 1:n-1
			push!(mids, i/n * [startPoint[1]+endPoint[1], startPoint[2]+endPoint[2]])
		end
		return mids
	end

Then, the function polygon is used to find the top third point.

function polygon(p1, p2, n=3)
	x = p2[1]-p1[1]
	y = p2[2]-p1[2]
	P3 = p1 + [x * cos(pi/3) - y * sin(pi/3), x * sin(pi/3) + y * cos(pi/3)]
	return P3
end

Finally in the function koch_curve is to draw the fractal:

function koch_curve(order::Int, startPoint = [0.0,0.0], endPoint = [1.0,0.0])
	pts=[startPoint, endPoint]
	for i in 1:order
		order_points =[]
		for j in 2:length(pts)
			a = pts[j-1]
			b = pts[j]
			c, d = seg_points(a, b)
			e = polygon(c, d)
			push!(order_points, c, d, e)
		end
		for pt in order_points
			push!(pts, pt)
		end
	end
	return pts
end

But it doesn't work. The output plot is this with order=5:
在Julia中无需使用递归绘制Koch曲线。

答案1

得分: 2

以下是代码的翻译:

首先,中点没有被正确计算。如果您想找到等分线段的点,应该使用加权平均值:

function seg_points(startPoint, endPoint, n=3)
    mids = []
    for i in 1:n-1
        push!(mids, (1 - i/n) * startPoint + i/n * endPoint)
    end
    return mids
end

然后,您需要按正确的顺序将这些点添加,因为线段由相邻的点定义:

function koch_curve(order::Int, startPoint = [0.0,0.0], endPoint = [1.0,0.0])
    pts=[startPoint, endPoint]
    for i in 1:order
        order_points =[]
        for j in 2:length(pts)
            a = pts[j-1]
            b = pts[j]
            c, d = seg_points(a, b)
            e = polygon(c, d)
            # 不要推入b,因为这个b等于下一次循环的a:
            push!(order_points, a, c, e, d)
        end
        push!(order_points, pts[end]) # 最后的b
        pts = order_points
    end
    return pts
end
英文:

First thing is the midpoints aren't being computed correctly. A weighted average should be used if you want to find the points that equally divide a segment:

function seg_points(startPoint, endPoint, n=3)
    mids = []
    for i in 1:n-1
        push!(mids, (1 - i/n) * startPoint + i/n * endPoint)
    end
    return mids
end

Then you need to push the points in the correct order, since lines are defined by adjacent points:

function koch_curve(order::Int, startPoint = [0.0,0.0], endPoint = [1.0,0.0])
    pts=[startPoint, endPoint]
    for i in 1:order
        order_points =[]
        for j in 2:length(pts)
            a = pts[j-1]
            b = pts[j]
            c, d = seg_points(a, b)
            e = polygon(c, d)
            # don't push b, since this b = the next loop iter's a:
            push!(order_points, a, c, e, d)
        end
        push!(order_points, pts[end]) # the last b
        pts = order_points
    end
    return pts
end

huangapple
  • 本文由 发表于 2023年5月21日 00:44:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296310.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定