| 1 |
from __future__ import division |
| 2 |
import math |
| 3 |
import sys |
| 4 |
|
| 5 |
__all__ = [ |
| 6 |
"add", |
| 7 |
"addPt", |
| 8 |
"sub", |
| 9 |
"subPt", |
| 10 |
"mul", |
| 11 |
"mulPt", |
| 12 |
"div", |
| 13 |
"divPt", |
| 14 |
"factorAngle", |
| 15 |
"_roundNumber", |
| 16 |
] |
| 17 |
|
| 18 |
def add(v1, v2): |
| 19 |
return v1 + v2 |
| 20 |
|
| 21 |
def addPt(pt1, pt2): |
| 22 |
return pt1[0] + pt2[0], pt1[1] + pt2[1] |
| 23 |
|
| 24 |
def sub(v1, v2): |
| 25 |
return v1 - v2 |
| 26 |
|
| 27 |
def subPt(pt1, pt2): |
| 28 |
return pt1[0] - pt2[0], pt1[1] - pt2[1] |
| 29 |
|
| 30 |
def mul(v, f): |
| 31 |
return v * f |
| 32 |
|
| 33 |
def mulPt(pt1, f): |
| 34 |
(f1, f2) = f |
| 35 |
return pt1[0] * f1, pt1[1] * f2 |
| 36 |
|
| 37 |
def div(v, f): |
| 38 |
return v / f |
| 39 |
|
| 40 |
def divPt(pt, f): |
| 41 |
(f1, f2) = f |
| 42 |
return pt[0] / f1, pt[1] / f2 |
| 43 |
|
| 44 |
def factorAngle(angle, f, func): |
| 45 |
(f1, f2) = f |
| 46 |
|
| 47 |
if f1 == f2: |
| 48 |
return func(angle, f1) |
| 49 |
|
| 50 |
rangle = math.radians(angle) |
| 51 |
x = math.cos(rangle) |
| 52 |
y = math.sin(rangle) |
| 53 |
return math.degrees( |
| 54 |
math.atan2( |
| 55 |
func(y, f2), func(x, f1) |
| 56 |
) |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
def round2(number, ndigits=None): |
| 61 |
""" |
| 62 |
Implementation of Python 2 built-in round() function. |
| 63 |
Rounds a number to a given precision in decimal digits (default |
| 64 |
0 digits). The result is a floating point number. Values are rounded |
| 65 |
to the closest multiple of 10 to the power minus ndigits; if two |
| 66 |
multiples are equally close, rounding is done away from 0. |
| 67 |
ndigits may be negative. |
| 68 |
See Python 2 documentation: |
| 69 |
https://docs.python.org/2/library/functions.html?highlight=round#round |
| 70 |
|
| 71 |
This is taken from the to-be-deprecated py23 fuction of fonttools |
| 72 |
""" |
| 73 |
import decimal as _decimal |
| 74 |
|
| 75 |
if ndigits is None: |
| 76 |
ndigits = 0 |
| 77 |
|
| 78 |
if ndigits < 0: |
| 79 |
exponent = 10 ** (-ndigits) |
| 80 |
quotient, remainder = divmod(number, exponent) |
| 81 |
if remainder >= exponent // 2 and number >= 0: |
| 82 |
quotient += 1 |
| 83 |
return float(quotient * exponent) |
| 84 |
else: |
| 85 |
exponent = _decimal.Decimal("10") ** (-ndigits) |
| 86 |
|
| 87 |
d = _decimal.Decimal.from_float(number).quantize( |
| 88 |
exponent, rounding=_decimal.ROUND_HALF_UP |
| 89 |
) |
| 90 |
|
| 91 |
return float(d) |
| 92 |
|
| 93 |
|
| 94 |
def setRoundIntegerFunction(func): |
| 95 |
""" Globally set function for rounding floats to integers. |
| 96 |
|
| 97 |
The function signature must be: |
| 98 |
|
| 99 |
def func(value: float) -> int |
| 100 |
""" |
| 101 |
global _ROUND_INTEGER_FUNC |
| 102 |
_ROUND_INTEGER_FUNC = func |
| 103 |
|
| 104 |
|
| 105 |
def setRoundFloatFunction(func): |
| 106 |
""" Globally set function for rounding floats within given precision. |
| 107 |
|
| 108 |
The function signature must be: |
| 109 |
|
| 110 |
def func(value: float, ndigits: int) -> float |
| 111 |
""" |
| 112 |
global _ROUND_FLOAT_FUNC |
| 113 |
_ROUND_FLOAT_FUNC = func |
| 114 |
|
| 115 |
|
| 116 |
_ROUND_INTEGER_FUNC = round |
| 117 |
_ROUND_FLOAT_FUNC = round |
| 118 |
|
| 119 |
|
| 120 |
def _roundNumber(value, ndigits=None): |
| 121 |
"""Round number using the Python 3 built-in round function. |
| 122 |
|
| 123 |
You can change the default rounding functions using setRoundIntegerFunction |
| 124 |
and/or setRoundFloatFunction. |
| 125 |
""" |
| 126 |
if ndigits is not None: |
| 127 |
return _ROUND_FLOAT_FUNC(value, ndigits) |
| 128 |
return _ROUND_INTEGER_FUNC(value) |
| 129 |
|
| 130 |
|
| 131 |
if __name__ == "__main__": |
| 132 |
import sys |
| 133 |
import doctest |
| 134 |
sys.exit(doctest.testmod().failed) |