src/reportlab/pdfgen/pdfgeom.py
author robin <robin@reportlab.com>
Tue, 07 Mar 2017 10:00:34 +0000
changeset 4330 617ffa6bbdc8
parent 4252 fe660f227cac
child 4370 823a8c33ce43
permissions -rwxr-xr-x
changes for release 3.4.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4330
617ffa6bbdc8 changes for release 3.4.0
robin <robin@reportlab.com>
parents: 4252
diff changeset
     1
#Copyright ReportLab Europe Ltd. 2000-2017
494
54257447cfe9 Changed to indirect copyright
rgbecker
parents: 47
diff changeset
     2
#see license.txt for license details
2332
2a7ab4405e18 Remove $Header:, fix CopyRight & history
rgbecker
parents: 1683
diff changeset
     3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/pdfgen/pdfgeom.py
4252
fe660f227cac changes for release 3.3.0
robin
parents: 3617
diff changeset
     4
__version__='3.3.0'
16
f9c7525619fb Docstring & other fixes
rgbecker
parents: 7
diff changeset
     5
__doc__="""
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     6
This module includes any mathematical methods needed for PIDDLE.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     7
It should have no dependencies beyond the Python library.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     8
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     9
So far, just Robert Kern's bezierArc.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    10
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    11
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    12
from math import sin, cos, pi, ceil
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    13
562
6c9408ec3302 Minor neglectable changes.
dinu_gherman
parents: 494
diff changeset
    14
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    15
def bezierArc(x1,y1, x2,y2, startAng=0, extent=90):
47
0505ed9b16d9 8bit character fixes
rgbecker
parents: 16
diff changeset
    16
    """bezierArc(x1,y1, x2,y2, startAng=0, extent=90) --> List of Bezier
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    17
curve control points.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    18
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    19
(x1, y1) and (x2, y2) are the corners of the enclosing rectangle.  The
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    20
coordinate system has coordinates that increase to the right and down.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    21
Angles, measured in degress, start with 0 to the right (the positive X
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    22
axis) and increase counter-clockwise.  The arc extends from startAng
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    23
to startAng+extent.  I.e. startAng=0 and extent=180 yields an openside-down
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    24
semi-circle.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    25
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    26
The resulting coordinates are of the form (x1,y1, x2,y2, x3,y3, x4,y4)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    27
such that the curve goes from (x1, y1) to (x4, y4) with (x2, y2) and
47
0505ed9b16d9 8bit character fixes
rgbecker
parents: 16
diff changeset
    28
(x3, y3) as their respective Bezier control points."""
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    29
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    30
    x1,y1, x2,y2 = min(x1,x2), max(y1,y2), max(x1,x2), min(y1,y2)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    31
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    32
    if abs(extent) <= 90:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    33
        arcList = [startAng]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    34
        fragAngle = float(extent)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    35
        Nfrag = 1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    36
    else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    37
        arcList = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    38
        Nfrag = int(ceil(abs(extent)/90.))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    39
        fragAngle = float(extent) / Nfrag
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    40
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    41
    x_cen = (x1+x2)/2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    42
    y_cen = (y1+y2)/2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    43
    rx = (x2-x1)/2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    44
    ry = (y2-y1)/2.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    45
    halfAng = fragAngle * pi / 360.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    46
    kappa = abs(4. / 3. * (1. - cos(halfAng)) / sin(halfAng))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    47
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    48
    if fragAngle < 0:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    49
        sign = -1
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    50
    else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    51
        sign = 1
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 562
diff changeset
    52
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    53
    pointList = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    54
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    55
    for i in range(Nfrag):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    56
        theta0 = (startAng + i*fragAngle) * pi / 180.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    57
        theta1 = (startAng + (i+1)*fragAngle) *pi / 180.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    58
        if fragAngle > 0:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    59
            pointList.append((x_cen + rx * cos(theta0),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    60
                              y_cen - ry * sin(theta0),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    61
                              x_cen + rx * (cos(theta0) - kappa * sin(theta0)),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    62
                              y_cen - ry * (sin(theta0) + kappa * cos(theta0)),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    63
                              x_cen + rx * (cos(theta1) + kappa * sin(theta1)),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    64
                              y_cen - ry * (sin(theta1) - kappa * cos(theta1)),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    65
                              x_cen + rx * cos(theta1),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    66
                              y_cen - ry * sin(theta1)))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    67
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    68
            pointList.append((x_cen + rx * cos(theta0),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    69
                              y_cen - ry * sin(theta0),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    70
                              x_cen + rx * (cos(theta0) + kappa * sin(theta0)),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    71
                              y_cen - ry * (sin(theta0) - kappa * cos(theta0)),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    72
                              x_cen + rx * (cos(theta1) - kappa * sin(theta1)),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    73
                              y_cen - ry * (sin(theta1) + kappa * cos(theta1)),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    74
                              x_cen + rx * cos(theta1),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    75
                              y_cen - ry * sin(theta1)))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    76
1683
7fa753e4420a Removed all trailing whitespace
andy_robinson
parents: 562
diff changeset
    77
    return pointList