Fractal Koch Curve
My project that involves antenna design, in which Python generates开发者_如何转开发 the .nec file that allows me to model fractal antennas in the software and optimize them for the best performance. The program is intended to work with multiple fractal antennas, including all of the following:
- Koch Curve
- Hilbert Curve
- Koch Snowflake
Don't worry, this isn't a homework assignment. I am giving a speech on Fractal Antennas and wanted to automate the design process, otherwise it is tedious.
Unfortunately, I'm having trouble with calculating the center point of the Koch Curve. Here is an image of what it looks like in software; note that I still geometry bugs that need to be solved.
Here are the coordinates of the resulting Python script using a iteration level of 3 and segment size of 0.305m.
The Python scripts that is currently being subjugated to my madness are pointed out below:
.NEC File
Coordinates
4NEC2_Generator.py Code
Complete Project
As you will notice in the image depiction of the Koch Curve, it is off center by a tiny amount. My equation to find the complete length is this:
Where:
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
Does anyone know why I'm not getting the center?
Thanks,
Austin
Perhaps you should try to reimplement your iterative calculation being more canonical.
An answer to a request for a good Koch Curve algorithm in Python is here:
Implementing the Koch Curve?
(and also the original code in the question could help you a lot)
EDIT: I created a script which uses code from the provided link, plus Cairo and Python Image Library (PIL) to render an image. Hope it helps:
#!/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()
Your problem is in the recursion to compute the new length of a side:
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)
You compute the newLengthSide without any reference to the current length side. The line should be this:
newLengthSide = lengthSide / 3.0
The reason your segments are 0.33333 is because you ignore the .305 passed in and start with 1/3.0.
I'm not sure exactly what the passed in value is supposed to represent, so this may not be the right new line to use, but this is why your segments are the wrong length.
I have decided to use the traditional code that I had, but that was only due to one preliminary factor: the fact that my kochCurve() code is based off of the total length underneath the Koch Curve, whereas before, I traditionally thought it determined the individual segment length. Thus, the center point was easily determined.
Here are images that my resulting script procured:
I appreciate the help!
精彩评论