Beispiel #1
0
 def __init__(self, seconds: float, freq: float, points: List[List[float]],
              **kwargs):
     Source.__init__(self)
     self.seconds = seconds
     self.freq = float(freq)
     self.points = np.array(points)
     self.curve = Curve.from_nodes(points)
Beispiel #2
0
def _interp_bezier(curve: bezier.Curve, detail: float) -> np.ndarray:
    """Strategy: interpolate at 1/5th target resolution and generate s param based
    on resulting segment length.


    """
    s = np.linspace(0, 1, max(3, math.ceil(curve.length / detail / 5)))
    x, y = curve.evaluate_multi(s)

    curv_absc = np.cumsum(np.hstack([0, np.hypot(np.diff(x), np.diff(y))]))

    new_s = np.interp(
        np.linspace(0, curv_absc[-1],
                    max(3, math.ceil(1.15 * curve.length / detail))),
        curv_absc,
        s,
    )

    x, y = curve.evaluate_multi(new_s)
    return x + 1j * y
Beispiel #3
0
 def __init__(self,
              screen: pygame.Surface,
              size: int = 50,
              pos=(0, 0),
              thick=None,
              twoPoint=False):
     """
     进度条加载
     :param screen:
     :param size:
     :param pos:
     :param thick: 默认随着size变化而变化
     :param twoPoint: 弧线头尾都会动
     """
     self.__p = 0
     self.screen = screen
     self.size = (size, size)
     self.surfaceFrame = pygame.Surface(self.size)
     self.pos = pos
     self.font = Base.getFont(Con.Font, size // 5)
     self.thick = thick or size // 20
     self.twoPoint = twoPoint
     self.bezier_line = Curve(((.0, .15, .31, 1.0), (.0, .75, .86, 1.0)), 3)
Beispiel #4
0
import matplotlib.pyplot as plt

from bezier import Curve

# line
c = Curve((0, 0), (1, 1))
plt.plot(*zip(*c.curve))
plt.scatter(*zip(*c.points))
plt.savefig("line_0-0-1-1.png")
plt.show()

c = Curve((0, 0), (0, 1), (1, 1))
plt.plot(*zip(*c.curve))
plt.scatter(*zip(*c.points))
plt.savefig("curve_0-0-0-1-1-1.png")
plt.show()

# integral
c = Curve((0.4, 0), (0.5, 0), (0.5, 1), (0.6, 1))
plt.plot(*zip(*c.curve))
plt.scatter(*zip(*c.points))
plt.xlim([0, 1])
plt.savefig("curve_0_4-0-0_5-0-0_5-1-0_6-1.png")
plt.show()

# loop
c = Curve((0, 0), (1, 0.5), (0, 1), (0.5, 0))
plt.plot(*zip(*c.curve))
plt.scatter(*zip(*c.points))
plt.savefig("curve_0-0-1-0_5-0-1-0_5-0.png")
plt.show()
Beispiel #5
0
    return -1


pygame.init()
pygame.display.set_caption("Bezier Curve Editor")
display = pygame.display.set_mode((600, 600))
display.fill((255, 255, 255))

font = pygame.font.Font('freesansbold.ttf', 15)
controls_text = "Left click: add point | Right click: remove point | Drag: Move point"

controls = font.render(controls_text, True, (0, 0, 0), (255, 255, 255))
textRect = controls.get_rect()
display.blit(controls, textRect)

c = Curve(auto_generate=False)
moving_point = -1
modified = False
mouse_down = False

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_down = pygame.mouse.get_pressed()[0]
            modified = True
            coord = pygame.mouse.get_pos()
            moving_point = nearest(c, coord)
            if pygame.mouse.get_pressed()[0]:
Beispiel #6
0
class Loader:
    def __init__(self,
                 screen: pygame.Surface,
                 size: int = 50,
                 pos=(0, 0),
                 thick=None,
                 twoPoint=False):
        """
        进度条加载
        :param screen:
        :param size:
        :param pos:
        :param thick: 默认随着size变化而变化
        :param twoPoint: 弧线头尾都会动
        """
        self.__p = 0
        self.screen = screen
        self.size = (size, size)
        self.surfaceFrame = pygame.Surface(self.size)
        self.pos = pos
        self.font = Base.getFont(Con.Font, size // 5)
        self.thick = thick or size // 20
        self.twoPoint = twoPoint
        self.bezier_line = Curve(((.0, .15, .31, 1.0), (.0, .75, .86, 1.0)), 3)

    @property
    def percent(self):
        self.__p = self.__p % 1
        return self.__p

    @percent.setter
    def percent(self, p):
        self.__p = p % 1

    def generateAngle(self):
        if self.twoPoint:
            x, y = self.bezier_line.evaluate(float(self.__p))
            startAngle, endAngle = Base.angleToFloat(
                (-(y[0]) * 360) + 90), Base.angleToFloat(-self.__p * 360 + 90)
            startAngle, endAngle = min(startAngle,
                                       endAngle), max(startAngle, endAngle)
            return startAngle, endAngle
        else:
            return Base.angleToFloat((-self.__p) * 360 +
                                     90), Base.angleToFloat(90)

    def flush(self, screen=None):
        if screen:
            self.screen = screen
        self.surfaceFrame.fill(Con.BackGround)
        start, end = self.generateAngle()
        pygame.draw.arc(self.surfaceFrame,
                        Con.ProgressBarColor, (*(0, 0), *self.size),
                        start,
                        end,
                        width=self.thick)
        text = self.font.render(f'{self.percent:.2%}', True, Con.TextColor)
        Base.printToCenter(self.surfaceFrame, text)
        if self.pos == Const.CENTER_POSITION:
            Base.printToCenter(self.screen, self.surfaceFrame)
        else:
            self.screen.blit(self.surfaceFrame, self.pos)