我正在尝试编写一个python脚本,它执行以下计算:
输入: (1)列表L:一些二维点的列表(2)列表V:三角形(3)正整数n的顶点:由该三角形创建的Koch雪花的阶数
输出:列表O,L的子集,包含位于区域Kn上或区域内的L的点,该区域是由n阶雪花定义的区域。
我的尝试:首先,我想我应该实现一个标准的算法来绘制一个给定顺序的雪花(和边长)。下面是我写的代码:
import turtle
from test import test
world= turtle.Screen()
t= turtle.Turtle()
def koch(t, order, size):
if order == 0:
t.forward(size)
else:
for angle in [60, -120, 60, 0]:
koch(t, order-1, size/3)
t.left(angle)
def koch_fractal(t, order, size, main_polygon_sides= 3):
for i in range(main_polygon_sides):
koch(t, order, size)
t.right(360/main_polygon_sides)
koch_fractal(t, 2, 100)
world.mainloop()但是因为它没有提到雪花的区域,所以我不能再动了。接下来,我认为雪花的区域可能有一些洞察力,所以我写了这个函数:
from math import sqrt
koch_cache={}
def koch_fractal_area(n, side):
original_area = (sqrt(3)/4) * side**2 #Area of the original triangle
koch_cache[0] = original_area
for i in range(n+1):
if i not in koch_cache:
koch_cache[i] = koch_cache[i-1] + (3*4**(i-1))*(sqrt(3)/4) * (side/(3**i))**2
return koch_cache[n]它实现了一个计算面积的显式公式。再说一遍,这似乎与我想要做的事情无关。
我如何处理这个问题?提前感谢!
发布于 2017-01-16 17:28:55

为了提高效率,当您将点与侧比较时,请使用以下规则:
这看起来可能有点不同,但它可以节省大量资金。实际上,在n-th世代中,片有3 x 4^n边(即第十代的3145728 );如果您递归到一个子侧,您将只进行12测试!
@cdlane的版本是最糟糕的,因为它每次都会执行一个详尽的测试。@ante的版本介于两者之间,因为它有时会提前停止,但仍然可以执行指数级的测试。
实现的一种简单方法是假设要检查的侧总是(0,0)-(1,0)。然后,测试测试点属于哪个三角形是一个简单的问题,因为顶点的坐标是固定的和已知的。这可以通过与一条直线的四个比较来实现。
当你需要回复到一个子边时,你会将它移动到原点,缩放3,旋转60°(如果必要的话);将同样的转换应用到测试点上。
发布于 2017-01-11 22:53:33
可以用同样的方式检查点位置,即如何递归地创建Koch雪花。步骤如下:
这种方法速度更快,因为它不会创建整个多边形并对其进行检查。
下面是对点使用numpy的实现:
import numpy
def on_negative_side(p, v1, v2):
d = v2 - v1
return numpy.dot(numpy.array([-d[1], d[0]]), p - v1) < 0
def in_side(p, v1, v2, n):
if n <= 0:
return False
d = v2 - v1
l = numpy.linalg.norm(d)
s = numpy.dot(d / l, p - v1)
if s < 0 or s > l: # No need for a check if point is outside edge 'boundaries'
return False
# Yves's check
nd = numpy.array([-d[1], d[0]])
m_v = nd * numpy.sqrt(3) / 6
if numpy.dot(nd / l, v1 - p) > numpy.linalg.norm(m_v):
return False
# Create next points
p1 = v1 + d/3
p2 = v1 + d/2 - m_v
p3 = v1 + 2*d/3
# Check with two inner edges
if on_negative_side(p, p1, p2):
return in_side(p, v1, p1, n-1) or in_side(p, p1, p2, n-1)
if on_negative_side(p, p2, p3):
return in_side(p, p2, p3, n-1) or in_side(p, p3, v2, n-1)
return True
def _in_koch(p, V, n):
V_next = numpy.concatenate((V[1:], V[:1]))
return all(not on_negative_side(p, v1, v2) or in_side(p, v1, v2, n)
for v1, v2 in zip(V, V_next))
def in_koch(L, V, n):
# Triangle points (V) are positive oriented
return [p for p in L if _in_koch(p, V, n)]
L = numpy.array([(16, -16), (90, 90), (40, -40), (40, -95), (50, 10), (40, 15)])
V = numpy.array([(0, 0), (50, -50*numpy.sqrt(3)), (100, 0)])
for n in xrange(3):
print n, in_koch(L, V, n)
print in_koch(L, V, 100)发布于 2017-01-11 20:46:50
找到一个具有一个执行“多边形点”包含测试的例程的Python模块;使用turtle的begin_poly()、end_poly()和get_poly()来捕获代码生成的顶点,然后应用缠绕号测试:
from turtle import Turtle, Screen
from point_in_polygon import wn_PnPoly
points = [(16, -16), (90, 90), (40, -40), (40, -95)]
screen = Screen()
yertle = Turtle()
yertle.speed("fastest")
def koch(turtle, order, size):
if order == 0:
turtle.forward(size)
else:
for angle in [60, -120, 60, 0]:
koch(turtle, order - 1, size / 3)
turtle.left(angle)
def koch_fractal(turtle, order, size, main_polygon_sides=3):
for _ in range(main_polygon_sides):
koch(turtle, order, size)
turtle.right(360 / main_polygon_sides)
yertle.begin_poly()
koch_fractal(yertle, 2, 100)
yertle.end_poly()
polygon = yertle.get_poly()
yertle.penup()
inside_points = []
for n, point in enumerate(points):
yertle.goto(point)
yertle.write(str(n), align="center")
winding_number = wn_PnPoly(point, polygon)
if winding_number:
print(n, "is inside snowflake")
inside_points.append(point)
else:
print(n, "is outside snowflake")
print(inside_points)
yertle.hideturtle()
screen.exitonclick()

% python3 test.py
0 is inside snowflake
1 is outside snowflake
2 is inside snowflake
3 is outside snowflake
[(16, -16), (40, -40)]https://stackoverflow.com/questions/41594136
复制相似问题