Skip to content

Commit f94c26f

Browse files
committed
adding koch example
1 parent b3340d1 commit f94c26f

File tree

6 files changed

+97
-10
lines changed

6 files changed

+97
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ _build
1414
.vscode
1515
*~
1616
mpy-cross
17+
.ipynb_checkpoints

circuitpython_uplot/plot.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def _draw_ticks(self, x: int, y: int, ticksx=None, ticksy=None) -> None:
346346
self.show_text(
347347
"{:.{}f}".format(ticksxnorm[i], self._decimal_points),
348348
tick,
349-
self._newymin,
349+
self._newymin + 5,
350350
(0.5, 0.0),
351351
)
352352
for i, tick in enumerate(ticksyrenorm):
@@ -361,7 +361,7 @@ def _draw_ticks(self, x: int, y: int, ticksx=None, ticksy=None) -> None:
361361
if self._showtext:
362362
self.show_text(
363363
"{:.{}f}".format(ticksynorm[i], self._decimal_points),
364-
self._newxmin,
364+
self._newxmin - 5,
365365
tick,
366366
(1.0, 0.5),
367367
)
@@ -546,3 +546,10 @@ class color:
546546
ORANGE = 0xFF9933
547547
TEAL = 0x158FAD
548548
GRAY = 0x808080
549+
PINK = 0xFF40C0
550+
LIGHT_GRAY = 0xAAAAAA
551+
BROWN = 0xCA801D
552+
DARK_GREEN = 0x008700
553+
TURQUOISE = 0x00C0C0
554+
DARK_BLUE = 0x0000AA
555+
DARK_RED = 0x800000

docs/examples.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,15 @@ Cartesian animation example
184184
.. image:: ../docs/cartesian_logging_data.gif
185185

186186

187+
Cartesian Koch Snowflake
188+
---------------------------
189+
190+
Cartesian koch snowflake example
187191

192+
.. literalinclude:: ../examples/cartesian_koch.py
193+
:caption: examples/cartesian_koch.py
194+
:lines: 5-
195+
.. image:: ../docs/koch.jpg
188196

189197
==================
190198
Bar Examples

docs/koch.jpg

20.1 KB
Loading

docs/quick_start.rst

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ This will allow you to use the colors in the list as color variable definitions
180180
* ORANGE
181181
* TEAL
182182
* GRAY
183+
* PINK
184+
* LIGHT_GRAY
185+
* BROWN
186+
* DARK_GREEN
187+
* TURQUOISE
188+
* DARK_BLUE
189+
* DARK_RED
190+
183191

184192
.. code-block:: python
185193
@@ -264,10 +272,10 @@ And needs to be defined in the first cartesian graph added to the plot.
264272
Line styles
265273
============
266274
You can select the line style of your Cartesian graph. The following line styles are available:
267-
* LineStyle.SOLID
268-
* LineStyle.DOTTED
269-
* LineStyle.DASHED
270-
* LineStyle.DASH_DOT
275+
* SOLID
276+
* DOTTED
277+
* DASHED
278+
* DASH_DOT
271279

272280
This can be done by importing the color class, from the cartesian library:
273281

@@ -345,10 +353,10 @@ There are some parameters that you can customize:
345353
Pointer styles
346354
==============
347355
You can select the pointer style of your Scatter graph. The following pointer styles are available:
348-
* Pointer.CIRCLE
349-
* Pointer.SQUARE
350-
* Pointer.TRIANGLE
351-
* Pointer.DIAMOND
356+
* CIRCLE
357+
* SQUARE
358+
* TRIANGLE
359+
* DIAMOND
352360

353361
This can be done by importing the color class, from the cartesian library:
354362

examples/cartesian_koch.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2023 Jose D. Montoya
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
# Example adapted to use in CircuitPython and Microplot from
6+
# https://github.com/TheAlgorithms/Python/blob/master/fractals/koch_snowflake.py
7+
# License MIT
8+
9+
import board
10+
from circuitpython_uplot.plot import Plot
11+
from circuitpython_uplot.cartesian import Cartesian
12+
from ulab import numpy
13+
14+
15+
def iterate(initial_vectors: list[numpy.ndarray], steps: int) -> list[numpy.ndarray]:
16+
vectors = initial_vectors
17+
for _ in range(steps):
18+
vectors = iteration_step(vectors)
19+
return vectors
20+
21+
22+
def iteration_step(vectors: list[numpy.ndarray]) -> list[numpy.ndarray]:
23+
new_vectors = []
24+
for i, start_vector in enumerate(vectors[:-1]):
25+
end_vector = vectors[i + 1]
26+
new_vectors.append(start_vector)
27+
difference_vector = end_vector - start_vector
28+
new_vectors.append(start_vector + difference_vector / 3)
29+
new_vectors.append(
30+
start_vector + difference_vector / 3 + rotate(difference_vector / 3, 60)
31+
)
32+
new_vectors.append(start_vector + difference_vector * 2 / 3)
33+
new_vectors.append(vectors[-1])
34+
35+
return new_vectors
36+
37+
38+
def rotate(vector: numpy.ndarray, angle_in_degrees: float) -> numpy.ndarray:
39+
theta = numpy.radians(angle_in_degrees)
40+
c, s = numpy.cos(theta), numpy.sin(theta)
41+
rotation_matrix = numpy.array(((c, -s), (s, c)))
42+
return numpy.dot(rotation_matrix, vector)
43+
44+
45+
# Setting up the display
46+
display = board.DISPLAY
47+
plot = Plot(0, 0, 200, 200, padding=0)
48+
49+
# initial triangle of Koch snowflake
50+
VECTOR_1 = numpy.array([0, 0])
51+
VECTOR_2 = numpy.array([0.5, 0.8660254])
52+
VECTOR_3 = numpy.array([1, 0])
53+
INITIAL_VECTORS = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1]
54+
# uncomment for simple Koch curve instead of Koch snowflake
55+
# INITIAL_VECTORS = [VECTOR_1, VECTOR_3]
56+
57+
# Due to memory restrictions the maximum number of iterations is 3.
58+
processed_vectors = iterate(INITIAL_VECTORS, 3)
59+
x_coordinates, y_coordinates = zip(*processed_vectors)
60+
61+
# Adding the Cartesian plot
62+
Cartesian(plot, x_coordinates, y_coordinates)
63+
display.show(plot)

0 commit comments

Comments
 (0)