如何用pygame画一条虚线?

2024-05-30

我需要在坐标系上绘制正弦波和余弦波,就像在this https://i.stack.imgur.com/DGI8g.png图片。除了没能代表以外,我所有的工作都做得很好虚线和曲线与 pygame 一致。我有与我需要的类似的东西,但我怎样才能让它弯曲呢?或者我该如何改进它以使其像pygame.draw.lines, not pygame.draw.line?

    import pygame
    import math
    
    class Point:
        # constructed using a normal tupple
        def __init__(self, point_t = (0,0)):
            self.x = float(point_t[0])
            self.y = float(point_t[1])
        # define all useful operators
        def __add__(self, other):
            return Point((self.x + other.x, self.y + other.y))
        def __sub__(self, other):
            return Point((self.x - other.x, self.y - other.y))
        def __mul__(self, scalar):
            return Point((self.x*scalar, self.y*scalar))
        def __div__(self, scalar):
            return Point((self.x/scalar, self.y/scalar))
        def __len__(self):
            return int(math.sqrt(self.x**2 + self.y**2))
        # get back values in original tuple format
        def get(self):
            return (self.x, self.y)
    def draw_dashed_line(surf, color, start_pos, end_pos, width=1, dash_length=4):
        origin = Point(start_pos)
        target = Point(end_pos)
        displacement = target - origin
        length = len(displacement)
        slope = displacement.__div__(length)
        for index in range(0, int(length/dash_length), 2):
            start = origin + (slope *    index    * dash_length)
            end   = origin + (slope * (index + 1) * dash_length)
            pygame.draw.line(surf, color, start.get(), end.get(), width)
    
    pygame.init()
    screen = pygame.display.set_mode((400, 300))
    done = False
    
    while not done:
        
        draw_dashed_line(screen,(0,255,0),(0,0),(110,110))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        pygame.display.flip()

编写一个操作类似的函数pygame.draw.line() http://www.pygame.org/docs/ref/draw.html#pygame.draw.lines但画一条虚直线。该函数有一个附加参数prev_line_len它指示线段在连续曲线内的位置。计算欧氏距离 https://en.wikipedia.org/wiki/Euclidean_distance点与点之间单位向量 https://en.wikipedia.org/wiki/Unit_vector从线段的起点指向线段的终点。沿线分布笔画:

def draw_dashed_line(surf, color, p1, p2, prev_line_len, dash_length=8):
    dx, dy = p2[0]-p1[0], p2[1]-p1[1]
    if dx == 0 and dy == 0:
        return 
    dist = math.hypot(dx, dy)
    dx /= dist
    dy /= dist

    step = dash_length*2
    start = (int(prev_line_len) // step) * step
    end = (int(prev_line_len + dist) // step + 1) * step
    for i in range(start, end, dash_length*2):
        s = max(0, start - prev_line_len)
        e = min(start - prev_line_len + dash_length, dist)
        if s < e:
            ps = p1[0] + dx * s, p1[1] + dy * s 
            pe = p1[0] + dx * e, p1[1] + dy * e 
            pygame.draw.line(surf, color, pe, ps

编写另一个功能类似于pygame.draw.lines() https://www.pygame.org/docs/ref/draw.html#pygame.draw.lines,但使用前一个函数(draw_dashed_line) 绘制虚线。计算从曲线起点到每条线段起点的长度并将其传递给函数:

def draw_dashed_lines(surf, color, points, dash_length=8):
    line_len = 0
    for i in range(1, len(points)):
        p1, p2 = points[i-1], points[i]
        dist = math.hypot(p2[0]-p1[0], p2[1]-p1[1])
        draw_dashed_line(surf, color, p1, p2, line_len, dash_length)
        line_len += dist

最小的例子:

repl.it/@Rabbid76/DashedLine

import pygame
import math

def draw_dashed_line(surf, color, p1, p2, prev_line_len, dash_length=8):
    dx, dy = p2[0]-p1[0], p2[1]-p1[1]
    if dx == 0 and dy == 0:
        return 
    dist = math.hypot(dx, dy)
    dx /= dist
    dy /= dist

    step = dash_length*2
    start = (int(prev_line_len) // step) * step
    end = (int(prev_line_len + dist) // step + 1) * step
    for i in range(start, end, dash_length*2):
        s = max(0, start - prev_line_len)
        e = min(start - prev_line_len + dash_length, dist)
        if s < e:
            ps = p1[0] + dx * s, p1[1] + dy * s 
            pe = p1[0] + dx * e, p1[1] + dy * e 
            pygame.draw.line(surf, color, pe, ps)

def draw_dashed_lines(surf, color, points, dash_length=8):
    line_len = 0
    for i in range(1, len(points)):
        p1, p2 = points[i-1], points[i]
        dist = math.hypot(p2[0]-p1[0], p2[1]-p1[1])
        draw_dashed_line(surf, color, p1, p2, line_len, dash_length)
        line_len += dist

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False

line = [(i, 150 + math.sin(math.radians(i*2)) * 100) for i in range(400)]

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    draw_dashed_lines(screen, (255, 255, 255), line)
    pygame.display.flip()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何用pygame画一条虚线? 的相关文章

随机推荐