【问题标题】:How can I connect two points with a series of circles?如何用一系列圆圈连接两个点?
【发布时间】:2022-10-13 04:19:29
【问题描述】:

我正在尝试在 pygame 中制作逼真的水: 到目前为止,这是我的代码:

from random import randint
import pygame

WIDTH = 700
HEIGHT = 500

win = pygame.display.set_mode((WIDTH, HEIGHT))

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
AQUA = 'aqua'

RADIUS = 1

x, y = 0, HEIGHT//2

K = 1

FORCE = 100

VELOCITY = 0.5

run = True

class Molecule:
    def __init__(self, x, y, radius, force, k):
        self.x = x
        self.y = y
        self.radius = radius
        self.force = force
        self.k = k
        self.max_amplitude = y + force/k
        self.min_amplitude = y - force/k
        self.up = False
        self.down = True
        self.restore = False

    def draw(self, win):
        pygame.draw.circle(win, BLACK, (self.x, self.y), self.radius)

    def oscillate(self):
        if self.y <= self.max_amplitude and self.down == True:
            self.y += VELOCITY

        if self.y == self.max_amplitude or self.up:
            self.up = True
            self.down = False
            self.y -= VELOCITY
        
        if self.y == self.min_amplitude:
            self.up = False 
            self.down = True
        

molecules = []

for i in range(100):
    FORCE = randint(10, 20)
    molecules.append(Molecule(x, y, RADIUS, FORCE, K))
    x += 10

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    win.fill(WHITE)

    for molecule in molecules:
        molecule.draw(win)
        molecule.oscillate()

    for i in range(len(molecules)):
        try:
            pygame.draw.line(win, BLACK, (molecules[i].x, molecules[i].y), (molecules[i+1].x, molecules[i+1].y))
            pygame.draw.line(win, AQUA, (molecules[i].x, molecules[i].y), (molecules[i+1].x, HEIGHT))
        except:
            pass

    pygame.display.flip()

pygame.quit()

但正如预期的那样,水曲线并不平滑: 看它: 样品图1

我想使用一组圆连接两个随机添加的波点,而不是像这个一样的线,这样就可以产生平滑的曲线。 通过这种方式,我可以将水的颜色添加到它,这样它就会从点到屏幕末端绘制水色线或我想要的颜色线,所有这一切都会以流畅的水流模拟结束。 现在的问题是如何通过在相对点处绘制点圆来使这些点平滑地连接在一起形成平滑的曲线?

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    我建议用贝塞尔曲线粘贴这些部分。可以使用pygame.gfxdraw.bezier 绘制贝塞尔曲线

    计算沿波浪水线的点的切线斜率:

    ts = []
    for i in range(len(molecules)):
        pa = molecules[max(0, i-1)]
        pb = molecules[min(len(molecules)-1, i+1)]
        ts.append((pb.y-pa.y) / (pb.x-pa.x))
    

    使用切线为每个段定义 4 个控制点,并使用pygame.gfxdraw.bezier 绘制曲线:

    for i in range(len(molecules)-1):
        p0 = molecules[i].x, molecules[i].y
        p3 = molecules[i+1].x, molecules[i+1].y
        p1 = p0[0] + 10, p0[1] + 10 * ts[i]
        p2 = p3[0] - 10, p3[1] - 10 * ts[i+1]
        pygame.gfxdraw.bezier(win, [p0, p1, p2, p3], 4, BLACK)
    

    完整示例:

    from random import randint
    import pygame
    import pygame.gfxdraw
    
    WIDTH = 700
    HEIGHT = 500
    
    win = pygame.display.set_mode((WIDTH, HEIGHT))
    
    WHITE = (255, 255, 255)
    BLACK = (0, 0, 0)
    AQUA = 'aqua'
    RADIUS = 1
    x, y = 0, HEIGHT//2
    K = 1
    FORCE = 100
    VELOCITY = 0.5
    
    class Molecule:
        def __init__(self, x, y, radius, force, k):
            self.x = x
            self.y = y
            self.radius = radius
            self.force = force
            self.k = k
            self.max_amplitude = y + force/k
            self.min_amplitude = y - force/k
            self.up = False
            self.down = True
            self.restore = False
    
        def draw(self, win):
            pygame.draw.circle(win, BLACK, (self.x, self.y), self.radius)
    
        def oscillate(self):
            if self.y <= self.max_amplitude and self.down == True:
                self.y += VELOCITY
    
            if self.y == self.max_amplitude or self.up:
                self.up = True
                self.down = False
                self.y -= VELOCITY
            
            if self.y == self.min_amplitude:
                self.up = False 
                self.down = True
            
    
    molecules = []
    
    for i in range(50):
        FORCE = randint(10, 20)
        molecules.append(Molecule(x, y, RADIUS, FORCE, K))
        x += 20
    
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(100)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        
        win.fill(WHITE)
    
        for molecule in molecules:
            molecule.draw(win)
            molecule.oscillate()
    
        ts = []
        for i in range(len(molecules)):
            pa = molecules[max(0, i-1)]
            pb = molecules[min(len(molecules)-1, i+1)]
            ts.append((pb.y-pa.y) / (pb.x-pa.x))
    
        for i in range(len(molecules)-1):
            p0 = molecules[i].x, molecules[i].y
            p3 = molecules[i+1].x, molecules[i+1].y
            p1 = p0[0] + 10, p0[1] + 10 * ts[i]
            p2 = p3[0] - 10, p3[1] - 10 * ts[i+1]
            pygame.gfxdraw.bezier(win, [p0, p1, p2, p3], 4, BLACK)
    
        for i in range(len(molecules)-1):
            pygame.draw.line(win, AQUA, (molecules[i].x, molecules[i].y), (molecules[i].x, HEIGHT))
    
        pygame.display.flip()
    
    pygame.quit()
    

    如果要“填充”水,则必须沿贝塞尔线计算点并绘制填充多边形。 Trying to make a Bezier Curve on PyGame library How Can I Make a Thicker Bezier in Pygame? 和“X”中解释了如何计算贝塞尔曲线。您可以使用以下功能:

    def ptOnCurve(b, t):
        q = b.copy()
        for k in range(1, len(b)):
            for i in range(len(b) - k):
                q[i] = (1-t) * q[i][0] + t * q[i+1][0], (1-t) * q[i][1] + t * q[i+1][1]
        return round(q[0][0]), round(q[0][1])
    
    def bezier(b, samples):
        return [ptOnCurve(b, i/samples) for i in range(samples+1)]
    

    使用bezier 缝合波浪水多边形:

    ts = []
    for i in range(len(molecules)):
        pa = molecules[max(0, i-1)]
        pb = molecules[min(len(molecules)-1, i+1)]
        ts.append((pb.y-pa.y) / (pb.x-pa.x))
    
    pts = [(WIDTH, HEIGHT), (0, HEIGHT)]
    for i in range(len(molecules)-1):
        p0 = molecules[i].x, molecules[i].y
        p3 = molecules[i+1].x, molecules[i+1].y
        p1 = p0[0] + 10, p0[1] + 10 * ts[i]
        p2 = p3[0] - 10, p3[1] - 10 * ts[i+1]
        pts += bezier([p0, p1, p2, p3], 4)
    

    使用pygame.draw.polygon() 绘制多边形:

    pygame.draw.polygon(win, AQUA, pts)
    

    完整示例:

    from random import randint
    import pygame
    
    class Node:
        def __init__(self, x, y, force, k, v):
            self.x = x
            self.y = y
            self.y0 = y
            self.force = force
            self.k = k
            self.v = v
            self.direction = 1
    
        def oscillate(self):
            self.y += self.v * self.direction
            if self.y0 - self.force / self.k > self.y or self.y0 + self.force / self.k < self.y:
                self.direction *= -1
    
        def draw(self, surf):
            pygame.draw.circle(surf, "black", (self.x, self.y), 3)
    
    window = pygame.display.set_mode((700, 500))
    clock = pygame.time.Clock()
    
    width, height = window.get_size()  
    no_of_nodes = 25  
    dx = width / no_of_nodes
    nodes = [Node(i*dx, height//2, randint(15, 30), 1, 0.5) for i in range(no_of_nodes+1)]
    
    def ptOnCurve(b, t):
        q = b.copy()
        for k in range(1, len(b)):
            for i in range(len(b) - k):
                q[i] = (1-t) * q[i][0] + t * q[i+1][0], (1-t) * q[i][1] + t * q[i+1][1]
        return round(q[0][0]), round(q[0][1])
    
    def bezier(b, samples):
        return [ptOnCurve(b, i/samples) for i in range(samples+1)]
    
    run = True
    while run:
        clock.tick(100)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        for molecule in nodes:
            molecule.oscillate()
    
        ts = []
        for i in range(len(nodes)):
            pa = nodes[max(0, i-1)]
            pb = nodes[min(len(nodes)-1, i+1)]
            ts.append((pb.y-pa.y) / (pb.x-pa.x))
    
        pts = [(width, height), (0, height)]
        for i in range(len(nodes)-1):
            p0 = nodes[i].x, nodes[i].y
            p3 = nodes[i+1].x, nodes[i+1].y
            p1 = p0[0] + 10, p0[1] + 10 * ts[i]
            p2 = p3[0] - 10, p3[1] - 10 * ts[i+1]
            pts += bezier([p0, p1, p2, p3], 4)
    
        window.fill("white")
        pygame.draw.polygon(window, 'aqua', pts)
        for molecule in nodes:
            molecule.draw(window)
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-16
      • 2021-06-21
      • 1970-01-01
      • 2012-11-12
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多