分形科赫盒子

2024-04-30 04:39:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我的项目涉及天线设计,其中Python生成.nec文件,允许我在软件中对分形天线建模并优化它们以获得最佳性能。该程序旨在使用多个分形天线,包括以下所有内容:

  • 科赫曲线
  • 希尔伯特曲线
  • 科赫雪花

别担心,这不是家庭作业。我在分形天线上发表演讲,想让设计过程自动化,否则会很乏味。

不幸的是,我在计算科赫曲线的中心点时遇到了困难。这是它在软件中的样子;请注意,我仍然需要解决几何错误。

Koch Fractal in 4NEC2

Here是使用迭代级别3和段大小0.305m生成的Python脚本的坐标

下面指出了目前被我疯狂征服的Python脚本:

.NEC File

Coordinates

4NEC2_Generator.py Code

Complete Project

正如你将在科赫曲线的图像描述中注意到的,它偏离了一个很小的中心。我的公式是:

Center Point Equation

其中:

 l = total side-length (referenced from the bottom) of the Koch Curve
 s = segment size (my segment size was 0.305m, they should all be equal)
 n = number of iterations

有人知道我为什么不去中心吗?

谢谢

奥斯汀


Tags: 文件ofthe项目脚本size软件segment
3条回答

你的问题在于递归计算边的新长度:

def kochCurve(level, lengthSide):

    if(level == 0):
        ut.fd(lengthSide)
    else:
        newLengthSide = level/3.0     ## <-- Wrong.
        newLevel = level - 1

        kochCurve(newLevel, newLengthSide)
        ut.lt(60)
        kochCurve(newLevel, newLengthSide)
        ut.rt(120)
        kochCurve(newLevel, newLengthSide)
        ut.lt(60)
        kochCurve(newLevel, newLengthSide)

计算newLengthSide时不需要引用当前的length side。行应该是这样的:

        newLengthSide = lengthSide / 3.0

分段数为0.33333的原因是忽略传入的.305并以1/3.0开头。

我不确定传入的值应该代表什么,所以这可能不是要使用的正确新行,但这就是为什么段的长度是错误的。

我决定使用我拥有的传统代码,但这仅仅是由于一个初步因素:事实上,我的Koch Curve()代码是基于Koch曲线下的总长度,而在以前,我传统上认为它决定了单个段的长度。因此,中心点很容易确定。

以下是我的结果脚本获得的图像:

Main Panel

2D Radiation Slice

3D Radiation Pattern

我很感激你的帮助!

也许您应该尝试重新实现更规范的迭代计算。

下面是对Python中使用良好的Koch曲线算法的请求的答复:

Implementing the Koch Curve?

(而且问题中的原始代码可以帮助您很多)

编辑: 我创建了一个脚本,它使用来自提供的链接的代码,加上Cairo和Python图像库(PIL)来呈现图像。希望有帮助:

#!/bin/env python
# coding: utf-8

import math

angles = [math.radians(60*x) for x in range(6)]
sines = [math.sin(x) for x in angles]
cosin = [math.cos(x) for x in angles]

def L(angle, coords, jump):
    return (angle + 1) % 6
def R(angle, coords, jump):
    return (angle + 4) % 6
def F(angle, coords, jump):
    coords.append(
        (coords[-1][0] + jump * cosin[angle],
         coords[-1][1] + jump * sines[angle]))
    return angle

decode = dict(L=L, R=R, F=F)

def koch(steps, length=200, startPos=(0,0)):
    pathcodes="F"
    for i in xrange(steps):
        pathcodes = pathcodes.replace("F", "FLFRFLF")

    jump = float(length) / (3 ** steps)
    coords = [startPos]
    angle = 0

    for move in pathcodes:
        angle = decode[move](angle, coords, jump)

    return coords


TOTALWIDTH = 1000

points = koch(3,TOTALWIDTH,(-TOTALWIDTH/2,0))
print points


# optional part, shows an image with Y axis(good for debugging)
import cairo, Image

width = TOTALWIDTH
height = int(TOTALWIDTH*0.32)
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(surface)
cr.set_source_rgb(1,1,1)
cr.rectangle(0, 0, width, height)
cr.fill()
cr.translate(width*0.5, height*0.95)
cr.scale(1, -1)

# red Y axis:
cr.set_source_rgb(1,0,0)
cr.move_to(0,0)
cr.line_to(0,300)
cr.stroke()

cr.set_source_rgb(0,0,0)
cr.set_line_width(0.5)
cr.move_to(*points[0])
for n in range(len(points)):
    cr.line_to(*points[n])
cr.stroke()

im = Image.frombuffer("RGBA", (width, height), surface.get_data(), "raw", "BGRA", 0,1)
im.show()

相关问题 更多 >