Pygame中Marching Squares可视化无输出问题求助
Pygame实现Marching Squares绘制心形无图形显示排查
我尝试在Pygame中使用Marching Squares算法绘制心形的隐式方程,已实现该算法但未绘制出任何图形,希望排查原因。
代码实现如下:
import pygame w = 500 h = 500 grid = [] for i in range(-h//2, h//2): tempo = [] for j in range(-w//2, w//2): tempo.append((i**2 + j**2 - 1)**3 - i**2 * j**3) grid.append(tempo) print(i) def Bisection_Method(a, b, t, state, thresh = 0.001): if state: a -= w/2 b -= w/2 else: a -= h/2 b -= h/2 mid = (a + b)/2 while abs(b - a) >= thresh: a1 = 0 k1 = 0 if state: i = a r = mid j = t a1 = (i**2 + j**2 - 1)**3 - i**2 * j**3 k1 = (r**2 + j**2 - 1)**3 - r**2 * j**3 else: i = t r = mid j = a a1 = (i**2 + j**2 - 1)**3 - i**2 * j**3 k1 = (i**2 + r**2 - 1)**3 - i**2 * r**3) if(k1 == 0): return mid mid = (a + b)/2 if(a1 * k1 < 0): b = mid else: a = mid if state: mid += w/2 else: mid += h/2 return mid def MarchingSquares(i1, j1, i2, j2, i3, j3, i4, j4, plane): finalP1 = pygame.math.Vector2(float('inf'), float('inf')) finalP2 = pygame.math.Vector2(float('inf'), float('inf')) finalP3 = pygame.math.Vector2(float('inf'), float('inf')) finalP4 = pygame.math.Vector2(float('inf'), float('inf')) if((plane[i1][j1] < 0 and plane[i2][j2] > 0 and plane[i3][j3] > 0 and plane[i4][j4] > 0) or (plane[i1][j1] > 0 and plane[i2][j2] < 0 and plane[i3][j3] < 0 and plane[i4][j4] < 0)): finalP1 = pygame.math.Vector2(Bisection_Method(j1, j2, i1, True), i1) finalP2 = pygame.math.Vector2(j1, Bisection_Method(i1, i3, j1, False)) elif((plane[i1][j1] > 0 and plane[i2][j2] < 0 and plane[i3][j3] > 0 and plane[i4][j4] > 0) or (plane[i1][j1] < 0 and plane[i2][j2] > 0 and plane[i3][j3] < 0 and plane[i4][j4] < 0)): finalP1 = pygame.math.Vector2(Bisection_Method(j1, j2, i1, True), i1) finalP2 = pygame.math.Vector2(j2, Bisection_Method(i4, i2, j2, False)) elif((plane[i1][j1] > 0 and plane[i2][j2] > 0 and plane[i3][j3] < 0 and plane[i4][j4] > 0) or (plane[i1][j1] < 0 and plane[i2][j2] < 0 and plane[i3][j3] > 0 and plane[i4][j4] < 0)): finalP1 = pygame.math.Vector2(Bisection_Method(j3, j4, i3, True), i3) finalP2 = pygame.math.Vector2(j3, Bisection_Method(i1, i3, j3, False)) elif((plane[i1][j1] > 0 and plane[i2][j2] > 0 and plane[i3][j3] > 0 and plane[i4][j4] < 0) or (plane[i1][j1] < 0 and plane[i2][j2] < 0 and plane[i3][j3] < 0 and plane[i4][j4] > 0)): finalP1 = pygame.math.Vector2(Bisection_Method(j3, j4, i4, True), i4) finalP2 = pygame.math.Vector2(j4, Bisection_Method(i4, i2, j4, False)) elif((plane[i1][j1] > 0 and plane[i2][j2] > 0 and plane[i3][j3] < 0 and plane[i4][j4] < 0) or (plane[i1][j1] < 0 and plane[i2][j2] < 0 and plane[i3][j3] > 0 and plane[i4][j4] > 0)): finalP1 = pygame.math.Vector2(j1, Bisection_Method(i1, i3, j1, False)) finalP2 = pygame.math.Vector2(j2, Bisection_Method(i4, i2, j2, False)) elif((plane[i1][j1] > 0 and plane[i2][j2] > 0 and plane[i3][j3] > 0 and plane[i4][j4] < 0) or (plane[i1][j1] < 0 and plane[i2][j2] > 0 and plane[i3][j3] < 0 and plane[i4][j4] > 0)): finalP1 = pygame.math.Vector2(Bisection_Method(j1, j2, i1, True), i1) finalP2 = pygame.math.Vector2(Bisection_Method(j3, j4, i3, True), i3) elif((plane[i1][j1] < 0 and plane[i2][j2] > 0 and plane[i3][j3] > 0 and plane[i4][j4] < 0) or (plane[i1][j1] > 0 and plane[i2][j2] < 0 and plane[i3][j3] < 0 and plane[i4][j4] > 0)): i = (i1 + i3 - h)/2 j = (j1 + j3 - w)/2 state = (i**2 + j**2 - 1)**3 - i**2 * j**3 if((state < 0 and plane[i1][j1] < 0) or (state > 0 and plane[i1][j1] > 0)): finalP1 = pygame.math.Vector2(j1, Bisection_Method(i1, i3, j1, False)) finalP2 = pygame.math.Vector2(Bisection_Method(j3, j4, i3, True), i3) finalP3 = pygame.math.Vector2(Bisection_Method(j1, j2, i1, True), i1) finalP4 = pygame.math.Vector2(j4, Bisection_Method(i2, i4, j4, False)) else: finalP2 = pygame.math.Vector2(j1, Bisection_Method(i1, i3, j1, False)) finalP3 = pygame.math.Vector2(Bisection_Method(j3, j4, i3, True), i3) finalP1 = pygame.math.Vector2(Bisection_Method(j1, j2, i1, True), i1) finalP4 = pygame.math.Vector2(j4, Bisection_Method(i2, i4, j4, False)) return finalP1, finalP2, finalP3, finalP4 t = [] for i in range(h - 1): for j in range(w - 1): P1, P2, P3, P4 = MarchingSquares(i, j, i, j + 1, i + 1, j, i + 1, j + 1, grid) t.append([P1, P2, P3, P4]) print("marching {i}") # pygame setup pygame.init() screen = pygame.display.set_mode((w, h)) clock = pygame.time.Clock() running = True dt = 0 while running: # poll for events # pygame.QUIT event means the user clicked X to close your window for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # fill the screen with a color to wipe away anything from last frame screen.fill("white") for x in t: pygame.draw.line(screen, "purple", x[0], x[1]) if(x[2].x != float('inf')): pygame.draw.line(screen, "purple", x[2], x[3]) # flip() the display to put your work on screen pygame.display.flip() # limits FPS to 60 # dt is delta time in seconds since last frame, used for framerate- # independent physics. dt = clock.tick(60) / 1000 pygame.quit()
代码说明
- 先生成全屏幕网格,存储心形隐式方程在每个网格点的计算值
- 通过
if-else判断Marching Squares的16种情况(部分合并判断) - 使用二分法(
Bisection_Method)作为根查找算法,state参数区分X轴/Y轴的插值计算 - 当前运行结果为空白窗口
内容的提问来源于stack exchange,提问作者NikoMolecule
相关产品推荐
相关产品推荐

