kaxe 1.9.1__tar.gz → 1.10.1.dev0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {kaxe-1.9.1/src/kaxe.egg-info → kaxe-1.10.1.dev0}/PKG-INFO +1 -1
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/pyproject.toml +1 -1
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/axis.py +5 -3
- kaxe-1.10.1.dev0/src/kaxe/core/line.py +207 -0
- kaxe-1.10.1.dev0/src/kaxe/core/round.py +226 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/shapes.py +44 -22
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/window.py +20 -5
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/contour.py +1 -1
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/equation.py +19 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/map.py +4 -2
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/point.py +16 -17
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/grid.py +33 -36
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/log.py +2 -2
- {kaxe-1.9.1 → kaxe-1.10.1.dev0/src/kaxe.egg-info}/PKG-INFO +1 -1
- kaxe-1.9.1/src/kaxe/core/line.py +0 -86
- kaxe-1.9.1/src/kaxe/core/round.py +0 -85
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/LICENSE +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/MANIFEST.in +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/README.md +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/setup.cfg +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/__main__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/_require_3d.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/_require_viewer.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/chart/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/chart/bar.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/chart/box.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/chart/pie.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/chart/qqplot.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/cli.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/bounds.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/color.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/backend.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/camera.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/helper.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/hud.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/objects/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/objects/color.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/objects/line.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/objects/point.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/objects/pointer.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/objects/triangle.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/openglrender.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/d3/translator.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/draw.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/fileloader.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/helper.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/ipython_display.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/legend.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/marker.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/palette.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/profiler.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/progress.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/styles.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/svg.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/svg_pdf.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/symbol.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/text.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/core/thin.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/data/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/data/excel.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/_lazy.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/arrow.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/bubble.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/fill.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/function.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/guide_grid.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/inequality.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/parameter.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d2/pillar.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d3/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d3/base.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d3/function.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d3/mesh.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d3/point.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/d3/potato.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/function.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/legend.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/mapdata.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/point.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/objects/text.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/_lazy.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/box.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/constants.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/d3/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/d3/axes.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/d3/geometry.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/d3/plot3d.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/d3/variants.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/double.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/empty.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/polar.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/standard.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/themes.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/zoom.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/plot/zoom_connector.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/codec.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/context.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/document.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/registry.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/sample.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/sampled_curve.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/serializers.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/project/window.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.bright-oblique.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.bright-roman.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.bright-semibold.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.bright-semiboldoblique.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.classical-serif-italic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.concrete-bold.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.concrete-bolditalic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.concrete-italic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.concrete-roman.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.sans-serif-bold.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.sans-serif-boldoblique.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.sans-serif-demi-condensed-demicondensed.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.sans-serif-medium.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.sans-serif-oblique.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.serif-bold.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.serif-bolditalic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.serif-extra-boldslanted.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.serif-extra-romanslanted.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.serif-italic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.serif-roman.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.serif-upright-italic-uprightitalic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.typewriter-text-bold.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.typewriter-text-bolditalic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.typewriter-text-italic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.typewriter-text-light.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.typewriter-text-lightoblique.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.typewriter-text-regular.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.typewriter-text-variable-width-italic.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/cmu.typewriter-text-variable-width-medium.ttf +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/computer-modern-family/readme.txt +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/resources/logo-small.png +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/viewer/__init__.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/viewer/cli.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/viewer/client.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/viewer/server.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe/viewer/window.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe.egg-info/SOURCES.txt +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe.egg-info/dependency_links.txt +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe.egg-info/entry_points.txt +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe.egg-info/requires.txt +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/src/kaxe.egg-info/top_level.txt +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/tests/test.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/tests/test2.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/tests/test3.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/tests/test_4.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/tests/test_5.py +0 -0
- {kaxe-1.9.1 → kaxe-1.10.1.dev0}/tests/test_viewer.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: kaxe
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.10.1.dev0
|
|
4
4
|
Summary: A small graphing tool for functions, points, equations and more
|
|
5
5
|
Author-email: Valter Yde Daugberg <valteryde@hotmail.com>
|
|
6
6
|
Project-URL: Homepage, https://github.com/valteryde/kaxe
|
|
@@ -189,7 +189,7 @@ class Axis(AttrObject):
|
|
|
189
189
|
for i in range(0, ticksBeforeNull+1):
|
|
190
190
|
p = -step*i
|
|
191
191
|
markers.append({
|
|
192
|
-
"text":
|
|
192
|
+
"text": koundTeX(p),
|
|
193
193
|
"pos" : p,
|
|
194
194
|
"style": []
|
|
195
195
|
})
|
|
@@ -197,7 +197,7 @@ class Axis(AttrObject):
|
|
|
197
197
|
for i in range(1, ticksAfterNull+1):
|
|
198
198
|
p = step*i
|
|
199
199
|
markers.append({
|
|
200
|
-
"text":
|
|
200
|
+
"text": koundTeX(p),
|
|
201
201
|
"pos" : p,
|
|
202
202
|
"style": []
|
|
203
203
|
})
|
|
@@ -214,8 +214,10 @@ class Axis(AttrObject):
|
|
|
214
214
|
|
|
215
215
|
for i in range(math.floor(lengthOverStep)+2):
|
|
216
216
|
p = direction*step*i + startPos
|
|
217
|
+
if abs(p) <= 1e-12 * step:
|
|
218
|
+
p = 0
|
|
217
219
|
markers.append({
|
|
218
|
-
"text":
|
|
220
|
+
"text": koundTeX(p),
|
|
219
221
|
"pos" : p,
|
|
220
222
|
"style": []
|
|
221
223
|
})
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import math
|
|
2
|
+
import numpy as np
|
|
3
|
+
from PIL import Image
|
|
4
|
+
from .color import to_rgba
|
|
5
|
+
|
|
6
|
+
# DRAW LINES
|
|
7
|
+
def trapez(y,y0,w):
|
|
8
|
+
return np.clip(np.minimum(y+1+w/2-y0, -y+1+w/2+y0),0,1)
|
|
9
|
+
|
|
10
|
+
def weighted_line(r0, c0, r1, c1, w, rmin=0, rmax=np.inf):
|
|
11
|
+
|
|
12
|
+
if r0 == r1 and c0 == c1:
|
|
13
|
+
return [], [], []
|
|
14
|
+
|
|
15
|
+
# The algorithm below works fine if c1 >= c0 and c1-c0 >= abs(r1-r0).
|
|
16
|
+
# If either of these cases are violated, do some switches.
|
|
17
|
+
if abs(c1-c0) < abs(r1-r0):
|
|
18
|
+
# Switch x and y, and switch again when returning.
|
|
19
|
+
xx, yy, val = weighted_line(c0, r0, c1, r1, w, rmin=rmin, rmax=rmax)
|
|
20
|
+
return (yy, xx, val)
|
|
21
|
+
|
|
22
|
+
# At this point we know that the distance in columns (x) is greater
|
|
23
|
+
# than that in rows (y). Possibly one more switch if c0 > c1.
|
|
24
|
+
if c0 > c1:
|
|
25
|
+
return weighted_line(r1, c1, r0, c0, w, rmin=rmin, rmax=rmax)
|
|
26
|
+
|
|
27
|
+
# The following is now always < 1 in abs
|
|
28
|
+
slope = (r1-r0) / (c1-c0)
|
|
29
|
+
|
|
30
|
+
# Adjust weight by the slope
|
|
31
|
+
w *= np.sqrt(1+np.abs(slope)) / 2
|
|
32
|
+
|
|
33
|
+
# We write y as a function of x, because the slope is always <= 1
|
|
34
|
+
# (in absolute value)
|
|
35
|
+
x = np.arange(c0, c1+1, dtype=float)
|
|
36
|
+
y = x * slope + (c1*r0-c0*r1) / (c1-c0)
|
|
37
|
+
|
|
38
|
+
# Now instead of 2 values for y, we have 2*np.ceil(w/2).
|
|
39
|
+
# All values are 1 except the upmost and bottommost.
|
|
40
|
+
thickness = np.ceil(w/2)
|
|
41
|
+
yy = (np.floor(y).reshape(-1,1) + np.arange(-thickness-1,thickness+2).reshape(1,-1))
|
|
42
|
+
xx = np.repeat(x, yy.shape[1])
|
|
43
|
+
vals = trapez(yy, y.reshape(-1,1), w).flatten()
|
|
44
|
+
|
|
45
|
+
yy = yy.flatten()
|
|
46
|
+
|
|
47
|
+
# Exclude useless parts and those outside of the interval
|
|
48
|
+
# to avoid parts outside of the picture
|
|
49
|
+
mask = np.logical_and.reduce((yy >= rmin, yy < rmax, vals > 0))
|
|
50
|
+
|
|
51
|
+
return (yy[mask].astype(int), xx[mask].astype(int), vals[mask])
|
|
52
|
+
|
|
53
|
+
def colorBlend(ar, ag, ab, aa, br, bg, bb, ba):
|
|
54
|
+
# even faster but only works when all values are in range 0 to 255
|
|
55
|
+
|
|
56
|
+
alpha = (255 - (255 - ba)*(255 - aa))
|
|
57
|
+
red = (br * (255 - aa) + ar * aa) / 255
|
|
58
|
+
green = (bg * (255 - aa) + ag * aa) / 255
|
|
59
|
+
blue = (bb * (255 - aa) + ab * aa) / 255
|
|
60
|
+
|
|
61
|
+
return int(red), int(green), int(blue), int(alpha)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _blend_slice_rgba(region: np.ndarray, r: int, g: int, b: int, aa: np.ndarray) -> None:
|
|
65
|
+
"""Alpha-blend solid RGB with per-pixel alpha ``aa`` (0..255) onto RGBA uint8 ``region`` in-place."""
|
|
66
|
+
inv = 255 - aa
|
|
67
|
+
dst = region.astype(np.uint16)
|
|
68
|
+
region[..., 0] = (dst[..., 0] * inv + r * aa) // 255
|
|
69
|
+
region[..., 1] = (dst[..., 1] * inv + g * aa) // 255
|
|
70
|
+
region[..., 2] = (dst[..., 2] * inv + b * aa) // 255
|
|
71
|
+
region[..., 3] = 255 - ((255 - dst[..., 3]) * inv) // 255
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _axis_aligned_profile(pos: float, thickness: float, limit: int):
|
|
75
|
+
"""Return in-bounds pixel indices and trapez weights perpendicular to an axis-aligned line."""
|
|
76
|
+
w_eff = float(thickness) * 0.5
|
|
77
|
+
t_ceil = math.ceil(w_eff / 2.0)
|
|
78
|
+
coords = (math.floor(pos) + np.arange(-t_ceil - 1, t_ceil + 2)).astype(int)
|
|
79
|
+
vals = trapez(coords, pos, w_eff)
|
|
80
|
+
mask = (coords >= 0) & (coords < limit) & (vals > 0)
|
|
81
|
+
return coords[mask], vals[mask]
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _axis_aligned_span(p0: float, p1: float, limit: int):
|
|
85
|
+
"""Return clamped inclusive-exclusive integer span ``(lo, hi)`` along an axis-aligned line."""
|
|
86
|
+
lo_f, hi_f = (p0, p1) if p0 <= p1 else (p1, p0)
|
|
87
|
+
if hi_f < 0 or lo_f >= limit:
|
|
88
|
+
return None
|
|
89
|
+
clamped_lo = max(lo_f, -1.0)
|
|
90
|
+
clamped_hi = min(hi_f, float(limit))
|
|
91
|
+
pts = np.arange(clamped_lo, clamped_hi + 1.0, dtype=float).astype(int)
|
|
92
|
+
pts = pts[(pts >= 0) & (pts < limit)]
|
|
93
|
+
if len(pts) == 0:
|
|
94
|
+
return None
|
|
95
|
+
return int(pts[0]), int(pts[-1]) + 1
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _blend_into_surface_crop(
|
|
99
|
+
surface: Image.Image,
|
|
100
|
+
x_lo: int,
|
|
101
|
+
y_lo: int,
|
|
102
|
+
x_hi: int,
|
|
103
|
+
y_hi: int,
|
|
104
|
+
rows,
|
|
105
|
+
cols,
|
|
106
|
+
r: int,
|
|
107
|
+
g: int,
|
|
108
|
+
b: int,
|
|
109
|
+
aa: np.ndarray,
|
|
110
|
+
) -> None:
|
|
111
|
+
"""Crop ``surface`` to the line bounding box, alpha-blend, and paste back."""
|
|
112
|
+
crop = surface.crop((x_lo, y_lo, x_hi, y_hi))
|
|
113
|
+
if crop.mode != "RGBA":
|
|
114
|
+
crop = crop.convert("RGBA")
|
|
115
|
+
patch = np.array(crop, dtype=np.uint8)
|
|
116
|
+
sub = patch[rows, cols]
|
|
117
|
+
_blend_slice_rgba(sub, r, g, b, aa)
|
|
118
|
+
patch[rows, cols] = sub
|
|
119
|
+
surface.paste(Image.fromarray(patch, "RGBA"), (x_lo, y_lo))
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def drawLineOnPillowImage(
|
|
123
|
+
surface,
|
|
124
|
+
x0,
|
|
125
|
+
y0,
|
|
126
|
+
x1,
|
|
127
|
+
y1,
|
|
128
|
+
color,
|
|
129
|
+
thickness
|
|
130
|
+
):
|
|
131
|
+
if thickness <= 0:
|
|
132
|
+
return
|
|
133
|
+
if not (math.isfinite(x0) and math.isfinite(y0) and math.isfinite(x1) and math.isfinite(y1)):
|
|
134
|
+
return
|
|
135
|
+
|
|
136
|
+
r, g, b, alpha = to_rgba(color)
|
|
137
|
+
if alpha <= 0 or (x0 == x1 and y0 == y1):
|
|
138
|
+
return
|
|
139
|
+
|
|
140
|
+
w, h = surface.size
|
|
141
|
+
if w <= 0 or h <= 0:
|
|
142
|
+
return
|
|
143
|
+
|
|
144
|
+
buf = getattr(surface, "_kaxe_buf", None)
|
|
145
|
+
|
|
146
|
+
if y0 == y1:
|
|
147
|
+
ys, vals = _axis_aligned_profile(y0, thickness, h)
|
|
148
|
+
if len(ys) == 0:
|
|
149
|
+
return
|
|
150
|
+
span = _axis_aligned_span(x0, x1, w)
|
|
151
|
+
if span is None:
|
|
152
|
+
return
|
|
153
|
+
x_lo, x_hi = span
|
|
154
|
+
aa = (alpha * vals).astype(np.uint16)[:, None]
|
|
155
|
+
if buf is not None:
|
|
156
|
+
patch = buf[ys, x_lo:x_hi]
|
|
157
|
+
_blend_slice_rgba(patch, r, g, b, aa)
|
|
158
|
+
buf[ys, x_lo:x_hi] = patch
|
|
159
|
+
else:
|
|
160
|
+
y_lo, y_hi = int(ys.min()), int(ys.max()) + 1
|
|
161
|
+
_blend_into_surface_crop(
|
|
162
|
+
surface, x_lo, y_lo, x_hi, y_hi,
|
|
163
|
+
ys - y_lo, slice(None), r, g, b, aa,
|
|
164
|
+
)
|
|
165
|
+
return
|
|
166
|
+
|
|
167
|
+
if x0 == x1:
|
|
168
|
+
xs, vals = _axis_aligned_profile(x0, thickness, w)
|
|
169
|
+
if len(xs) == 0:
|
|
170
|
+
return
|
|
171
|
+
span = _axis_aligned_span(y0, y1, h)
|
|
172
|
+
if span is None:
|
|
173
|
+
return
|
|
174
|
+
y_lo, y_hi = span
|
|
175
|
+
aa = (alpha * vals).astype(np.uint16)[None, :]
|
|
176
|
+
if buf is not None:
|
|
177
|
+
patch = buf[y_lo:y_hi, xs]
|
|
178
|
+
_blend_slice_rgba(patch, r, g, b, aa)
|
|
179
|
+
buf[y_lo:y_hi, xs] = patch
|
|
180
|
+
else:
|
|
181
|
+
x_lo, x_hi = int(xs.min()), int(xs.max()) + 1
|
|
182
|
+
_blend_into_surface_crop(
|
|
183
|
+
surface, x_lo, y_lo, x_hi, y_hi,
|
|
184
|
+
slice(None), xs - x_lo, r, g, b, aa,
|
|
185
|
+
)
|
|
186
|
+
return
|
|
187
|
+
|
|
188
|
+
a, b_idx, c = weighted_line(x0, y0, x1, y1, float(thickness))
|
|
189
|
+
if len(a) == 0:
|
|
190
|
+
return
|
|
191
|
+
mask = (a >= 0) & (a < w) & (b_idx >= 0) & (b_idx < h)
|
|
192
|
+
a, b_idx, c = a[mask], b_idx[mask], c[mask]
|
|
193
|
+
if len(a) == 0:
|
|
194
|
+
return
|
|
195
|
+
|
|
196
|
+
aa = (alpha * c).astype(np.uint16)
|
|
197
|
+
if buf is not None:
|
|
198
|
+
sub = buf[b_idx, a]
|
|
199
|
+
_blend_slice_rgba(sub, r, g, b, aa)
|
|
200
|
+
buf[b_idx, a] = sub
|
|
201
|
+
else:
|
|
202
|
+
x_lo, x_hi = int(a.min()), int(a.max()) + 1
|
|
203
|
+
y_lo, y_hi = int(b_idx.min()), int(b_idx.max()) + 1
|
|
204
|
+
_blend_into_surface_crop(
|
|
205
|
+
surface, x_lo, y_lo, x_hi, y_hi,
|
|
206
|
+
b_idx - y_lo, a - x_lo, r, g, b, aa,
|
|
207
|
+
)
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
from decimal import Decimal, InvalidOperation, ROUND_HALF_UP, localcontext
|
|
2
|
+
from fractions import Fraction
|
|
3
|
+
import math
|
|
4
|
+
import numbers
|
|
5
|
+
from typing import Optional, Tuple, Union
|
|
6
|
+
|
|
7
|
+
_SCIENTIFIC_LOW_EXP = -2 # |x| < 10^-2 (0.01) uses scientific notation
|
|
8
|
+
_SCIENTIFIC_HIGH_EXP = 5 # |x| >= 10^5 (100 000) uses scientific notation
|
|
9
|
+
_GROUPING_EXP = 4 # 10 000 <= |x| < 100 000 uses \smallSpace grouping
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def forceround(n: float) -> float:
|
|
13
|
+
"""Round a float to 13 decimal places."""
|
|
14
|
+
return round(n, 13)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _is_bool(value) -> bool:
|
|
18
|
+
return isinstance(value, bool) or type(value).__name__ == "bool_"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _to_decimal(num, sig_figs: Optional[int]) -> Decimal:
|
|
22
|
+
"""Convert a real numeric value to an exact Decimal."""
|
|
23
|
+
if _is_bool(num):
|
|
24
|
+
raise TypeError(f"Expected a numeric value, got {type(num).__name__}")
|
|
25
|
+
|
|
26
|
+
if isinstance(num, Decimal):
|
|
27
|
+
return num
|
|
28
|
+
|
|
29
|
+
if isinstance(num, numbers.Integral):
|
|
30
|
+
return Decimal(int(num))
|
|
31
|
+
|
|
32
|
+
if isinstance(num, Fraction):
|
|
33
|
+
with localcontext() as ctx:
|
|
34
|
+
ctx.prec = max(64, (sig_figs or 10) + 20)
|
|
35
|
+
return Decimal(num.numerator) / Decimal(num.denominator)
|
|
36
|
+
|
|
37
|
+
if isinstance(num, float):
|
|
38
|
+
if math.isnan(num):
|
|
39
|
+
return Decimal("NaN")
|
|
40
|
+
if math.isinf(num):
|
|
41
|
+
return Decimal("-Infinity") if num < 0 else Decimal("Infinity")
|
|
42
|
+
return Decimal(str(num))
|
|
43
|
+
|
|
44
|
+
if isinstance(num, numbers.Real):
|
|
45
|
+
# Covers NumPy floats (including float16/float32/longdouble) and other Real types
|
|
46
|
+
try:
|
|
47
|
+
return Decimal(str(num))
|
|
48
|
+
except InvalidOperation:
|
|
49
|
+
f = float(num)
|
|
50
|
+
if math.isnan(f):
|
|
51
|
+
return Decimal("NaN")
|
|
52
|
+
if math.isinf(f):
|
|
53
|
+
return Decimal("-Infinity") if f < 0 else Decimal("Infinity")
|
|
54
|
+
return Decimal(str(f))
|
|
55
|
+
|
|
56
|
+
raise TypeError(f"Expected a numeric value, got {type(num).__name__}")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _round_decimal(
|
|
60
|
+
d: Decimal,
|
|
61
|
+
sig_figs: Optional[int],
|
|
62
|
+
decimals: Optional[int],
|
|
63
|
+
) -> Decimal:
|
|
64
|
+
"""Apply decimal-place or significant-figure rounding to a finite Decimal."""
|
|
65
|
+
if not d.is_finite() or d == 0:
|
|
66
|
+
return d
|
|
67
|
+
|
|
68
|
+
with localcontext() as ctx:
|
|
69
|
+
ctx.rounding = ROUND_HALF_UP
|
|
70
|
+
if decimals is not None:
|
|
71
|
+
exp_digits = abs(decimals)
|
|
72
|
+
ctx.prec = max(64, len(d.as_tuple().digits) + exp_digits + 16)
|
|
73
|
+
return d.quantize(Decimal(1).scaleb(-decimals))
|
|
74
|
+
if sig_figs is not None:
|
|
75
|
+
ctx.prec = sig_figs
|
|
76
|
+
return +d
|
|
77
|
+
return d
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _decimal_to_plain_str(d: Decimal) -> str:
|
|
81
|
+
"""Format a finite Decimal as a plain decimal string without trailing zeros."""
|
|
82
|
+
with localcontext() as ctx:
|
|
83
|
+
exp = d.as_tuple().exponent
|
|
84
|
+
extra = abs(exp) if isinstance(exp, int) else 0
|
|
85
|
+
ctx.prec = max(64, len(d.as_tuple().digits) + extra + 16)
|
|
86
|
+
normalized = d.normalize()
|
|
87
|
+
text = format(normalized, "f")
|
|
88
|
+
|
|
89
|
+
if "." in text:
|
|
90
|
+
text = text.rstrip("0").rstrip(".")
|
|
91
|
+
return text
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _group_thousands(int_part: str) -> str:
|
|
95
|
+
"""Insert ``\\smallSpace`` separators every three digits from the right."""
|
|
96
|
+
if len(int_part) <= 3:
|
|
97
|
+
return int_part
|
|
98
|
+
groups = [int_part[max(0, i - 3) : i] for i in range(len(int_part), 0, -3)]
|
|
99
|
+
groups.reverse()
|
|
100
|
+
return "\\smallSpace".join(groups)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _format_magnitude(mag: Decimal) -> str:
|
|
104
|
+
"""Format a positive finite Decimal magnitude without its enclosing ``$...$``."""
|
|
105
|
+
exp = mag.adjusted()
|
|
106
|
+
|
|
107
|
+
if exp < _SCIENTIFIC_LOW_EXP or exp >= _SCIENTIFIC_HIGH_EXP:
|
|
108
|
+
with localcontext() as ctx:
|
|
109
|
+
ctx.prec = max(64, len(mag.as_tuple().digits) + 16)
|
|
110
|
+
mantissa = mag.scaleb(-exp)
|
|
111
|
+
mantissa_str = _decimal_to_plain_str(mantissa)
|
|
112
|
+
if mantissa_str == "1":
|
|
113
|
+
return f"10^{{{exp}}}"
|
|
114
|
+
return f"{mantissa_str}\\cdot 10^{{{exp}}}"
|
|
115
|
+
|
|
116
|
+
plain = _decimal_to_plain_str(mag)
|
|
117
|
+
if exp == _GROUPING_EXP:
|
|
118
|
+
int_part, *frac_parts = plain.split(".")
|
|
119
|
+
grouped = _group_thousands(int_part)
|
|
120
|
+
if frac_parts:
|
|
121
|
+
return f"{grouped}.{frac_parts[0]}"
|
|
122
|
+
return grouped
|
|
123
|
+
|
|
124
|
+
return plain
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _format_real_signed(
|
|
128
|
+
num,
|
|
129
|
+
sig_figs: Optional[int],
|
|
130
|
+
decimals: Optional[int],
|
|
131
|
+
) -> Tuple[str, str]:
|
|
132
|
+
"""Return ``(sign, unsigned_body)`` for a real value."""
|
|
133
|
+
d = _to_decimal(num, sig_figs)
|
|
134
|
+
|
|
135
|
+
if d.is_nan():
|
|
136
|
+
return "", "\\text{NaN}"
|
|
137
|
+
if d.is_infinite():
|
|
138
|
+
return ("-" if d.is_signed() else ""), "∞"
|
|
139
|
+
|
|
140
|
+
d = _round_decimal(d, sig_figs=sig_figs, decimals=decimals)
|
|
141
|
+
if d == 0:
|
|
142
|
+
return "", "0"
|
|
143
|
+
|
|
144
|
+
sign = "-" if d.is_signed() else ""
|
|
145
|
+
return sign, _format_magnitude(abs(d))
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _format_complex_body(
|
|
149
|
+
num: complex,
|
|
150
|
+
sig_figs: Optional[int],
|
|
151
|
+
decimals: Optional[int],
|
|
152
|
+
) -> str:
|
|
153
|
+
"""Format a complex number as a LaTeX math body (without outer ``$``)."""
|
|
154
|
+
re_sign, re_body = _format_real_signed(num.real, sig_figs, decimals)
|
|
155
|
+
im_sign, im_body = _format_real_signed(num.imag, sig_figs, decimals)
|
|
156
|
+
|
|
157
|
+
if im_body == "0":
|
|
158
|
+
return f"{re_sign}{re_body}"
|
|
159
|
+
|
|
160
|
+
im_term = "i" if im_body == "1" else f"{im_body}i"
|
|
161
|
+
if re_body == "0":
|
|
162
|
+
return f"{im_sign}{im_term}"
|
|
163
|
+
|
|
164
|
+
op = "-" if im_sign == "-" else "+"
|
|
165
|
+
return f"{re_sign}{re_body}{op}{im_term}"
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def koundTeX(
|
|
169
|
+
num: Union[int, float, Decimal, Fraction, complex],
|
|
170
|
+
*,
|
|
171
|
+
sig_figs: Optional[int] = 10,
|
|
172
|
+
decimals: Optional[int] = None,
|
|
173
|
+
) -> str:
|
|
174
|
+
"""Format any number as a LaTeX math string suitable for ``fondi`` and kaxe labels.
|
|
175
|
+
|
|
176
|
+
- Zero and ``-0.0`` return ``"$0$"``.
|
|
177
|
+
- Values with ``0 < |x| < 0.01`` or ``|x| >= 100 000`` use normalized
|
|
178
|
+
scientific notation (e.g. ``"$5\\cdot 10^{-3}$"``, ``"$10^{5}$"``).
|
|
179
|
+
- Values with ``10 000 <= |x| < 100 000`` use ``\\smallSpace`` thousands
|
|
180
|
+
separators while preserving fractional digits.
|
|
181
|
+
- Non-finite values render as ``"$\\text{NaN}$"``, ``"$∞$"``, or ``"$-∞$"``.
|
|
182
|
+
- IEEE-754 floating-point noise is cleaned by default via ``sig_figs=10``.
|
|
183
|
+
|
|
184
|
+
Parameters
|
|
185
|
+
----------
|
|
186
|
+
num : int | float | Decimal | Fraction | complex
|
|
187
|
+
Numeric value to format.
|
|
188
|
+
sig_figs : int | None, optional
|
|
189
|
+
Maximum significant digits used to clean float noise when ``decimals``
|
|
190
|
+
is not given. Default is ``10``. Pass ``None`` to keep all digits.
|
|
191
|
+
decimals : int | None, optional
|
|
192
|
+
Fixed decimal-place rounding (matching ``round(x, decimals)`` semantics).
|
|
193
|
+
Overrides ``sig_figs`` when provided.
|
|
194
|
+
|
|
195
|
+
Returns
|
|
196
|
+
-------
|
|
197
|
+
str
|
|
198
|
+
LaTeX math string wrapped in ``$...$``, e.g. ``"$5\\cdot 10^{-3}$"``
|
|
199
|
+
or ``"$314$"``.
|
|
200
|
+
|
|
201
|
+
Examples
|
|
202
|
+
--------
|
|
203
|
+
>>> koundTeX(0.005)
|
|
204
|
+
'$5\\cdot 10^{-3}$'
|
|
205
|
+
>>> koundTeX(314)
|
|
206
|
+
'$314$'
|
|
207
|
+
>>> koundTeX(12345.67)
|
|
208
|
+
'$12\\smallSpace345.67$'
|
|
209
|
+
>>> koundTeX(732000000)
|
|
210
|
+
'$7.32\\cdot 10^{8}$'
|
|
211
|
+
"""
|
|
212
|
+
if sig_figs is not None:
|
|
213
|
+
if _is_bool(sig_figs) or not isinstance(sig_figs, int):
|
|
214
|
+
raise TypeError(f"sig_figs must be an int or None, got {type(sig_figs).__name__}")
|
|
215
|
+
if sig_figs < 1:
|
|
216
|
+
raise ValueError("sig_figs must be >= 1")
|
|
217
|
+
|
|
218
|
+
if decimals is not None and (_is_bool(decimals) or not isinstance(decimals, int)):
|
|
219
|
+
raise TypeError(f"decimals must be an int or None, got {type(decimals).__name__}")
|
|
220
|
+
|
|
221
|
+
if not _is_bool(num) and isinstance(num, numbers.Complex) and not isinstance(num, numbers.Real):
|
|
222
|
+
body = _format_complex_body(num, sig_figs, decimals)
|
|
223
|
+
return f"${body}$"
|
|
224
|
+
|
|
225
|
+
sign, body = _format_real_signed(num, sig_figs, decimals)
|
|
226
|
+
return f"${sign}{body}$"
|
|
@@ -237,14 +237,13 @@ class Line(Shape):
|
|
|
237
237
|
if self.thickness == 0:
|
|
238
238
|
return
|
|
239
239
|
|
|
240
|
-
|
|
241
|
-
|
|
240
|
+
h = surface.height
|
|
242
241
|
drawLineOnPillowImage(
|
|
243
242
|
surface,
|
|
244
243
|
self.x0,
|
|
245
|
-
y0,
|
|
244
|
+
h - self.y0,
|
|
246
245
|
self.x1,
|
|
247
|
-
y1,
|
|
246
|
+
h - self.y1,
|
|
248
247
|
self.color,
|
|
249
248
|
self.thickness
|
|
250
249
|
)
|
|
@@ -261,6 +260,10 @@ class Line(Shape):
|
|
|
261
260
|
doc.add_line(self.x0, self.y0, self.x1, self.y1, self.color, self.thickness)
|
|
262
261
|
|
|
263
262
|
|
|
263
|
+
_CIRCLE_SPRITE_CACHE = {}
|
|
264
|
+
_MAX_CIRCLE_SPRITE_CACHE = 512
|
|
265
|
+
|
|
266
|
+
|
|
264
267
|
class Circle(Shape):
|
|
265
268
|
|
|
266
269
|
def __init__(self, x:int, y:int, radius:int=5, color:tuple=BLACK, batch:Batch=None, cornerAlign:bool=False, fill:bool=True, width:int=5):
|
|
@@ -278,30 +281,47 @@ class Circle(Shape):
|
|
|
278
281
|
def centerAlign(self):
|
|
279
282
|
self.cornerAlign = False
|
|
280
283
|
|
|
284
|
+
@staticmethod
|
|
285
|
+
def _get_sprite(radius, color: tuple, fill: bool, width: int):
|
|
286
|
+
"""Return a cached RGBA circle sprite for the given geometry and style."""
|
|
287
|
+
doubleradius = int(2 * radius)
|
|
288
|
+
if doubleradius <= 0:
|
|
289
|
+
return None
|
|
290
|
+
key = (doubleradius, radius, color, fill, width if not fill else 0)
|
|
291
|
+
sprite = _CIRCLE_SPRITE_CACHE.get(key)
|
|
292
|
+
if sprite is not None:
|
|
293
|
+
return sprite
|
|
294
|
+
|
|
295
|
+
sprite = Image.new("RGBA", (doubleradius, doubleradius), (0, 0, 0, 0))
|
|
296
|
+
draw = ImageDraw.Draw(sprite)
|
|
297
|
+
# PIL treats the bbox as inclusive on both ends; shrink by half a pixel so
|
|
298
|
+
# the rendered circle is symmetric and not clipped at the bottom edge.
|
|
299
|
+
pos = (-0.5, -0.5, 2 * radius - 0.5, 2 * radius - 0.5)
|
|
300
|
+
if fill:
|
|
301
|
+
draw.ellipse(pos, fill=color)
|
|
302
|
+
else:
|
|
303
|
+
draw.ellipse(pos, outline=color, width=width)
|
|
304
|
+
|
|
305
|
+
if len(_CIRCLE_SPRITE_CACHE) >= _MAX_CIRCLE_SPRITE_CACHE:
|
|
306
|
+
_CIRCLE_SPRITE_CACHE.pop(next(iter(_CIRCLE_SPRITE_CACHE)))
|
|
307
|
+
_CIRCLE_SPRITE_CACHE[key] = sprite
|
|
308
|
+
return sprite
|
|
281
309
|
|
|
282
310
|
def drawPillow(self, surface):
|
|
283
|
-
|
|
311
|
+
sprite = self._get_sprite(self.radius, self.color, self.fill, self.width)
|
|
312
|
+
if sprite is None:
|
|
313
|
+
return
|
|
284
314
|
|
|
315
|
+
y = surface.height - self.y
|
|
285
316
|
if not self.cornerAlign:
|
|
317
|
+
x = self.x - self.radius
|
|
286
318
|
y -= self.radius
|
|
287
319
|
else:
|
|
288
|
-
|
|
320
|
+
x = self.x
|
|
289
321
|
y -= 2 * self.radius
|
|
290
|
-
|
|
291
|
-
# Create a small RGBA circle image
|
|
292
|
-
doubleradius = int(2*self.radius)
|
|
293
|
-
circle = Image.new("RGBA", (doubleradius, doubleradius), (0, 0, 0, 0))
|
|
294
|
-
draw = ImageDraw.Draw(circle)
|
|
295
|
-
# PIL treats the bbox as inclusive on both ends; shrink by half a pixel so
|
|
296
|
-
# the rendered circle is symmetric and not clipped at the bottom edge.
|
|
297
|
-
pos = (-0.5, -0.5, 2 * self.radius - 0.5, 2 * self.radius - 0.5)
|
|
298
|
-
if self.fill:
|
|
299
|
-
draw.ellipse(pos, fill=self.color)
|
|
300
|
-
else:
|
|
301
|
-
draw.ellipse(pos, outline=self.color, width=self.width)
|
|
302
322
|
|
|
303
323
|
# Paste the circle onto the main image using itself as the mask
|
|
304
|
-
surface.paste(
|
|
324
|
+
surface.paste(sprite, (int(x), int(y)), mask=sprite)
|
|
305
325
|
|
|
306
326
|
|
|
307
327
|
|
|
@@ -543,10 +563,12 @@ class LineSegment(Shape):
|
|
|
543
563
|
|
|
544
564
|
draw = ImageDraw.Draw(surface)
|
|
545
565
|
|
|
566
|
+
h = surface.height
|
|
567
|
+
ox, oy = self.offset
|
|
546
568
|
newpos = []
|
|
547
|
-
for x,y in self.points:
|
|
548
|
-
newpos.append(x+
|
|
549
|
-
newpos.append(
|
|
569
|
+
for x, y in self.points:
|
|
570
|
+
newpos.append(x + ox)
|
|
571
|
+
newpos.append(h - (y + oy))
|
|
550
572
|
|
|
551
573
|
if self.dotted:
|
|
552
574
|
self.__drawDottedLines__(draw, newpos)
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
from io import BytesIO
|
|
3
3
|
import time
|
|
4
4
|
from typing import Union, Optional
|
|
5
|
+
import numpy as np
|
|
5
6
|
from .helper import *
|
|
6
7
|
import logging
|
|
7
8
|
from .styles import *
|
|
9
|
+
from .color import to_rgba
|
|
8
10
|
from .legend import LegendBox
|
|
9
11
|
from .shapes import shapes
|
|
10
12
|
from .svg import SvgDocument, infer_format, is_file_path
|
|
@@ -508,11 +510,24 @@ class Window(AttrObject):
|
|
|
508
510
|
pbar = make_progress_bar(self.showProgressBar, len(self.shapes), 'Decorating')
|
|
509
511
|
|
|
510
512
|
winSize = self.width+self.padding[0]+self.padding[2], self.height+self.padding[1]+self.padding[3]
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
513
|
+
bg = to_rgba(self.getAttr('backgroundColor'))
|
|
514
|
+
if winSize[0] > 0 and winSize[1] > 0:
|
|
515
|
+
buf = np.empty((winSize[1], winSize[0], 4), dtype=np.uint8)
|
|
516
|
+
if bg[3] == 255:
|
|
517
|
+
buf[:, :] = bg
|
|
518
|
+
else:
|
|
519
|
+
buf.fill(0)
|
|
520
|
+
surface = Image.frombuffer('RGBA', winSize, buf, 'raw', 'RGBA', 0, 1)
|
|
521
|
+
surface.readonly = 0
|
|
522
|
+
surface._kaxe_buf = buf
|
|
523
|
+
if 0 < bg[3] < 255:
|
|
524
|
+
background = shapes.Rectangle(0, 0, winSize[0], winSize[1], color=bg)
|
|
525
|
+
background.draw(surface)
|
|
526
|
+
else:
|
|
527
|
+
surface = Image.new('RGBA', winSize)
|
|
528
|
+
if bg[3] != 0:
|
|
529
|
+
background = shapes.Rectangle(0, 0, winSize[0], winSize[1], color=bg) # 10 ms
|
|
530
|
+
background.draw(surface) # ? ms
|
|
516
531
|
|
|
517
532
|
for shape in self.shapes:
|
|
518
533
|
shape.draw(surface)
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
from ...core.shapes import shapes
|
|
3
|
+
from ...core.svg import SvgDocument
|
|
3
4
|
from ...core.color import to_rgba
|
|
4
5
|
from ...core.symbol import symbol
|
|
5
6
|
from ...plot import identities
|
|
@@ -314,6 +315,24 @@ class Equation:
|
|
|
314
315
|
|
|
315
316
|
|
|
316
317
|
def draw(self, *args, **kwargs):
|
|
318
|
+
if (
|
|
319
|
+
self.dots
|
|
320
|
+
and args
|
|
321
|
+
and not isinstance(args[0], SvgDocument)
|
|
322
|
+
and len(self.dots) == len(self.batch.objects)
|
|
323
|
+
):
|
|
324
|
+
surface = args[0]
|
|
325
|
+
first = self.dots[0]
|
|
326
|
+
sprite = shapes.Circle._get_sprite(first.radius, first.color, first.fill, first.width)
|
|
327
|
+
if sprite is None:
|
|
328
|
+
return
|
|
329
|
+
h = surface.height
|
|
330
|
+
r = first.radius
|
|
331
|
+
paste = surface.paste
|
|
332
|
+
for dot in self.dots:
|
|
333
|
+
if not dot.__hidden__:
|
|
334
|
+
paste(sprite, (int(dot.x - r), int(h - dot.y - r)), mask=sprite)
|
|
335
|
+
return
|
|
317
336
|
self.batch.draw(*args, **kwargs)
|
|
318
337
|
|
|
319
338
|
|