File: bezier.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (64 lines) | stat: -rw-r--r-- 2,012 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Name:         bezier.py 
# Package:      wx.lib.pdfviewer
#
# Purpose:      Compute Bezier curves for PDF rendered using wx.DC
#               Adapted from the original source code, see below.
#
# Author:       David Hughes     dfh@forestfield.co.uk
# Copyright:    Forestfield Software Ltd
# Licence:      Public domain

# History:      Created 17 Jun 2009
#
#----------------------------------------------------------------------------
 
import wx
from vec2d import *
 
def calculate_bezier(p, steps = 30):
    """
    Calculate a bezier curve from 4 control points and return a 
    list of the resulting points.
    Depends on the 2d vector class from http://www.pygame.org/wiki/2DVectorClass
     
    2007 Victor Blomqvist
    Released to the Public Domain    
    The function uses the forward differencing algorithm described at 
    http://www.niksula.cs.hut.fi/~hkankaan/Homepages/bezierfast.html
    """
    
    t = 1.0 / steps
    temp = t*t
    
    f = p[0]
    fd = 3 * (p[1] - p[0]) * t
    fdd_per_2 = 3 * (p[0] - 2 * p[1] + p[2]) * temp
    fddd_per_2 = 3 * (3 * (p[1] - p[2]) + p[3] - p[0]) * temp * t
    
    fddd = fddd_per_2 + fddd_per_2
    fdd = fdd_per_2 + fdd_per_2
    fddd_per_6 = fddd_per_2 * (1.0 / 3)
    
    points = []
    for x in range(steps):
        points.append(f)
        f = f + fd + fdd_per_2 + fddd_per_6
        fd = fd + fdd + fddd_per_2
        fdd = fdd + fddd
        fdd_per_2 = fdd_per_2 + fddd_per_2
    points.append(f)
    return points

def compute_points(controlpoints, nsteps=30):
    """ Input 4 control points as wxRealPoints and convert to vec2d instances.
        compute the nsteps points on the resulting curve and return them
        as a list of wxPoints """
    controlvectors = []
    for p in controlpoints:
        controlvectors.append(vec2d(p.x, p.y))
    pointvectors = calculate_bezier(controlvectors, nsteps)
    curvepoints = []
    for v in pointvectors:
        curvepoints.append(wx.Point(v[0], v[1]))
    return curvepoints