Skip to content

Commit 44feb31

Browse files
committed
Duffing Atttractor + docstring fix + docs
1 parent dd13348 commit 44feb31

7 files changed

Lines changed: 194 additions & 1 deletion

File tree

Hello_DuffingAttractor.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
notice = """
2+
Duffing Attractor Demo
3+
-----------------------------------
4+
| Copyright 2022 by Joel C. Alcarez |
5+
| [joelalcarez1975@gmail.com] |
6+
|-----------------------------------|
7+
| We make absolutely no warranty |
8+
| of any kind, expressed or implied |
9+
|-----------------------------------|
10+
| This graphics library outputs |
11+
| to a bitmap file. |
12+
-----------------------------------
13+
"""
14+
from Python_BMP.BITMAPlib import(
15+
saveduffingattractor2file as f,
16+
getfuncmetastr as meta)
17+
import subprocess as proc
18+
from os import path
19+
20+
21+
def main():
22+
print(f'{notice}\n{meta(f)}')
23+
imgedt = 'mspaint' # replace with another editor if Unix
24+
rootdir = path.dirname(__file__) #get path of running script
25+
file = f'hello{f.__name__}.bmp' # random file name
26+
f(file, # path to new file
27+
256, 256, # size of file
28+
24, # bit depth
29+
0.25, 0.3, 1.0, 0.05,# constants (float)
30+
20000 # number of iterations
31+
)
32+
print('Saved to %s in %s\nAll done close %s to finish' % \
33+
(file, rootdir, imgedt)) # tell user something happened
34+
ret = proc.call([imgedt, file])
35+
36+
if __name__=="__main__":
37+
main()

Python_BMP/BITMAPlib.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@
314314
kochsnowflakevert,
315315
nattractorlist,
316316
peterdejongattractorlist,
317-
symmetriciconattractorlist
317+
symmetriciconattractorlist,
318+
duffingattractorlist
318319
)
319320

320321
from .inttools import(
@@ -12738,6 +12739,25 @@ def peterdejongattractor(
1273812739
return plotattractor(peterdejongattractorlist(a, b, c, d, n), x, y, bits)
1273912740

1274012741

12742+
def duffingattractor(
12743+
x: int, y: int, bits: int,
12744+
a: float, b: float,
12745+
w: float, dt: float, n: int):
12746+
"""Draws a Peter de Jong Attractor
12747+
12748+
Args:
12749+
x, y : int dimensions of bmp
12750+
bits : int bit depth
12751+
a, b, w : float coefficients
12752+
dt : time interval
12753+
n: number of terms to compute
12754+
12755+
Returns:
12756+
byref unsigned byte array
12757+
"""
12758+
return plotattractor(duffingattractorlist(0, 0, 0, dt, a, b, w, n), x, y, bits)
12759+
12760+
1274112761
def hopalongattractor(
1274212762
x: int, y: int, bits: int,
1274312763
a: float, b: float,
@@ -12854,6 +12874,7 @@ def saveikedaattractor2file(
1285412874
"""Draws an Ikeda Attractor to file
1285512875
1285612876
Args:
12877+
file : full path to new file
1285712878
x, y : int dimensions of bmp
1285812879
bits : int bit depth
1285912880
a, b, k, p: float coefficients
@@ -12874,6 +12895,7 @@ def savegumowskimiraattractor2file(
1287412895
"""Draws a Gumowski-Mira attractor to file
1287512896
1287612897
Args:
12898+
file: full path to new file
1287712899
x, y: int dimensions of bmp
1287812900
bits: int bit depth
1287912901
ox, oy: starting coordinates
@@ -12901,6 +12923,7 @@ def savenattractor2file(
1290112923
"""Draws a N Attractor to file
1290212924
1290312925
Args:
12926+
file : full path to new file
1290412927
x, y : int dimensions of bmp
1290512928
bits : int bit depth
1290612929
a, b, c, d: float coefficients
@@ -12921,6 +12944,7 @@ def savepeterdejongattractor2file(
1292112944
"""Draws a Peter de Jong Attractor to file
1292212945
1292312946
Args:
12947+
file : full path to new file
1292412948
x, y : int dimensions of bmp
1292512949
bits : int bit depth
1292612950
a, b, c, d: float coefficients
@@ -12932,6 +12956,29 @@ def savepeterdejongattractor2file(
1293212956
saveBMP(file, peterdejongattractor(x, y, bits, a, b, c, d, n))
1293312957

1293412958

12959+
@functimer
12960+
def saveduffingattractor2file(
12961+
file: str,
12962+
x: int, y: int, bits: int,
12963+
a: float, b: float,
12964+
w: float, dt: float, n: int):
12965+
"""Draws a Duffing Attractor to file
12966+
12967+
Args:
12968+
file : full path to new file
12969+
x, y : int dimensions of bmp
12970+
bits : int bit depth
12971+
a, b, c: float coefficients
12972+
dt : time interval
12973+
n: number of terms to compute
12974+
12975+
12976+
Returns:
12977+
a bitmap file
12978+
"""
12979+
saveBMP(file, duffingattractor(x, y, bits, a, b, w, dt, n))
12980+
12981+
1293512982
@functimer
1293612983
def savehopalongattractor2file(
1293712984
file: str,

Python_BMP/fractals.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,3 +2169,33 @@ def symmetriciconattractorlist(
21692169
z.imag * p.imag)) * z + \
21702170
(g * p).conjugate() - o * c * 1j
21712171
return v
2172+
2173+
2174+
def duffingattractorlist(
2175+
x: float = 0, y: float = 0, t: float = 0,
2176+
dt: float = 0.01,
2177+
a: float = 0.25 , b: float = 0.3, w: float = 1.0,
2178+
n: int = 25000) -> list[list[float, float]]:
2179+
"""Returns list of [x,y] coordinate pairs for a
2180+
Duffing Attractor
2181+
2182+
Args:
2183+
x, y: float starting coordinates
2184+
t: float start time
2185+
dt: float time interval
2186+
a, b, w: float coefficients
2187+
n: number of terms to compute
2188+
2189+
Returns:
2190+
list of x,y pairs for a Duffing Attractor
2191+
[x: float, y: float]
2192+
"""
2193+
v = [(0,0)] * n
2194+
for i in range(n):
2195+
dx = y * dt
2196+
dy = (x - x ** 3 - a * y + b * cosine(w * t)) * dt
2197+
x += dx
2198+
y += dy
2199+
t += dt
2200+
v[i] = (x, y)
2201+
return v
192 KB
Binary file not shown.

docs/BitmapLib_Doc.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,46 @@ Draws a vector (line segment with arrow head)
16761676
byref modified unsigned byte array
16771677

16781678

1679+
### [`duffingattractor`](#duffingattractor)
1680+
1681+
```py
1682+
def duffingattractor(x: int, y: int, bits: int, a: float, b: float, w: float, dt: float, n: int):
1683+
```
1684+
1685+
Draws a Peter de Jong Attractor
1686+
1687+
Args:
1688+
x, y : int dimensions of bmp
1689+
bits : int bit depth
1690+
a, b, w : float coefficients
1691+
dt : time interval
1692+
n: number of terms to compute
1693+
1694+
Returns:
1695+
byref unsigned byte array
1696+
1697+
1698+
### [`duffingattractorlist`](#duffingattractorlist)
1699+
1700+
```py
1701+
def duffingattractorlist(x: float = 0, y: float = 0, t: float = 0, dt: float = 0.01, a: float = 0.25, b: float = 0.3, w: float = 1.0, n: int = 25000) -> list[list[float, float]]:
1702+
```
1703+
1704+
Returns list of [x,y] coordinate pairs for a
1705+
Duffing Attractor
1706+
1707+
Args:
1708+
x, y: float starting coordinates
1709+
t: float start time
1710+
dt: float time interval
1711+
a, b, w: float coefficients
1712+
n: number of terms to compute
1713+
1714+
Returns:
1715+
list of x,y pairs for a Duffing Attractor
1716+
[x: float, y: float]
1717+
1718+
16791719
### [`eggcurvevert`](#eggcurvevert)
16801720

16811721
```py
@@ -12372,6 +12412,27 @@ Saves a Cos(z) Julia Set to a file
1237212412
a bitmap file
1237312413

1237412414

12415+
### [`saveduffingattractor2file`](#saveduffingattractor2file)
12416+
12417+
```py
12418+
def saveduffingattractor2file(file: str, x: int, y: int, bits: int, a: float, b: float, w: float, dt: float, n: int):
12419+
```
12420+
12421+
Draws a Duffing Attractor to file
12422+
12423+
Args:
12424+
file : full path to new file
12425+
x, y : int dimensions of bmp
12426+
bits : int bit depth
12427+
a, b, c: float coefficients
12428+
dt : time interval
12429+
n: number of terms to compute
12430+
12431+
12432+
Returns:
12433+
a bitmap file
12434+
12435+
1237512436
### [`savefractal2file`](#savefractal2file)
1237612437

1237712438
```py
@@ -12506,6 +12567,7 @@ def savegumowskimiraattractor2file(file: str, x: int, y: int, bits: int, ox: flo
1250612567
Draws a Gumowski-Mira attractor to file
1250712568

1250812569
Args:
12570+
file: full path to new file
1250912571
x, y: int dimensions of bmp
1251012572
bits: int bit depth
1251112573
ox, oy: starting coordinates
@@ -12578,6 +12640,7 @@ def saveikedaattractor2file(file: str, x: int, y: int, bits: int, a: float, b: f
1257812640
Draws an Ikeda Attractor to file
1257912641

1258012642
Args:
12643+
file : full path to new file
1258112644
x, y : int dimensions of bmp
1258212645
bits : int bit depth
1258312646
a, b, k, p: float coefficients
@@ -13426,6 +13489,7 @@ def savenattractor2file(file: str, x: int, y: int, bits: int, a: float, b: float
1342613489
Draws a N Attractor to file
1342713490

1342813491
Args:
13492+
file : full path to new file
1342913493
x, y : int dimensions of bmp
1343013494
bits : int bit depth
1343113495
a, b, c, d: float coefficients
@@ -13512,6 +13576,7 @@ def savepeterdejongattractor2file(file: str, x: int, y: int, bits: int, a: float
1351213576
Draws a Peter de Jong Attractor to file
1351313577

1351413578
Args:
13579+
file : full path to new file
1351513580
x, y : int dimensions of bmp
1351613581
bits : int bit depth
1351713582
a, b, c, d: float coefficients

docs/Hello_Graphics.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ If there is demand for it I could in theory write a book based on this project l
318318

319319
[![cliffordattractor](../assets/fractals/cliffordattractor.bmp)](../Hello_CliffordAttractor.py)
320320

321+
[![duffingattractor](../assets/fractals/duffingattractor.bmp)](../Hello_DuffingAttractor.py)
322+
321323
[![fractaldream](../assets/fractals/fractaldreamattractor.bmp)](../Hello_FractalDreamAttractor.py)
322324

323325
[![Hopalongattractor](../assets/fractals/hopalongattractor.bmp)](../Hello_HopalongAttractor.py)

test_fractals.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
savehopalongattractor2file as hopalong,
7777
savefractaldreamattractor2file as fractaldream,
7878
savesymmetriciconattractor2file as symicon,
79+
saveduffingattractor2file as duffing,
7980
palshift2file
8081
)
8182

@@ -567,6 +568,17 @@ def testsymmetriciconattractor(self):
567568
self.filecmp(*p)
568569

569570

571+
def testduffingattractor(self):
572+
p = self._filepaths("duffingattractor.bmp")
573+
duffing(p[0], # path to new file
574+
256, 256, # size of file
575+
24, # bit depth
576+
0.25, 0.3, 1.0, 0.05, # constants (float)
577+
20000 # number of iterations
578+
)
579+
self.filecmp(*p)
580+
581+
570582
def testastroid(self):
571583
p = self._filepaths("astroid.bmp")
572584
astroid(p[0], # path to new file

0 commit comments

Comments
 (0)