-
Notifications
You must be signed in to change notification settings - Fork 0
/
newtonIteration_v0_0_0.py
161 lines (153 loc) · 5.09 KB
/
newtonIteration_v0_0_0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import array
from math import *
import cmath
from random import random
class bmp:
""" bmp data structure """
def __init__(self, w=1080, h=1920):
self.w = w
self.h = h
def calc_data_size (self):
if((self.w*3)%4 == 0):
self.dataSize = self.w * 3 * self.h
else:
self.dataSize = (((self.w * 3) // 4 + 1) * 4) * self.h
self.fileSize = self.dataSize + 54
def conv2byte(self, l, num, len):
tmp = num
for i in range(len):
l.append(tmp & 0x000000ff)
tmp >>= 8
def gen_bmp_header (self):
self.calc_data_size();
self.bmp_header = [0x42, 0x4d]
self.conv2byte(self.bmp_header, self.fileSize, 4) #file size
self.conv2byte(self.bmp_header, 0, 2)
self.conv2byte(self.bmp_header, 0, 2)
self.conv2byte(self.bmp_header, 54, 4) #rgb data offset
self.conv2byte(self.bmp_header, 40, 4) #info block size
self.conv2byte(self.bmp_header, self.w, 4)
self.conv2byte(self.bmp_header, self.h, 4)
self.conv2byte(self.bmp_header, 1, 2)
self.conv2byte(self.bmp_header, 24, 2) #888
self.conv2byte(self.bmp_header, 0, 4) #no compression
self.conv2byte(self.bmp_header, self.dataSize, 4) #rgb data size
self.conv2byte(self.bmp_header, 0, 4)
self.conv2byte(self.bmp_header, 0, 4)
self.conv2byte(self.bmp_header, 0, 4)
self.conv2byte(self.bmp_header, 0, 4)
def print_bmp_header (self):
length = len(self.bmp_header)
for i in range(length):
print("{:0>2x}".format(self.bmp_header[i]), end=' ')
if i%16 == 15:
print('')
print('')
def paint_bgcolor(self, color=0xffffff):
self.rgbData = []
for r in range(self.h):
self.rgbDataRow = []
for c in range(self.w):
self.rgbDataRow.append(color)
self.rgbData.append(self.rgbDataRow)
def set_at(self,x, y, color):
self.rgbData[y][x] = color
def paint_line(self, x1, y1, x2, y2, color):
k = (y2 - y1) / (x2 - x1)
for x in range(x1, x2+1):
y = int(k * (x - x1) + y1)
self.rgbData[y][x] = color
def paint_rect(self, x1, y1, w, h, color):
for x in range(x1, x1+w):
for y in range(y1, y1+h):
self.rgbData[y][x] = color
def save_image(self, name="save.bmp"):
f = open(name, 'wb')
#write bmp header
f.write(array.array('B', self.bmp_header).tobytes())
#write rgb data
zeroBytes = self.dataSize // self.h - self.w * 3
for r in range(self.h):
l = []
for i in range(len(self.rgbData[r])):
p = self.rgbData[r][i]
l.append(p & 0x0000ff)
p >>= 8
l.append(p & 0x0000ff)
p >>= 8
l.append(p & 0x0000ff)
f.write(array.array('B', l).tobytes())
for i in range(zeroBytes):
f.write(bytes(0x00))
f.close()
e = [-2, 0, 0, 0, 0, 1]
def f(x):
#return sum(e[i] * x ** i for i in range(6))
return cmath.sin(x)
def fprime(x):
#return sum(i * e[i] * x ** (i - 1) for i in range(6))
return cmath.cos(x)
def newton(x):
if fprime(x) == 0:
return None
return x - f(x) / fprime(x)
def calc_color(x):
if 0 <= x < 16:
return 0xffffff - x * 256 * 17
elif 16 <= x < 32:
return 0xff00ff - (x - 16) * 256 * 256 * 17
elif 32 <= x < 48:
return 0x0000ff + (x - 32) * 256 * 17
elif 48 <= x < 64:
return 0x00ffff - (x - 48) * 17
elif 64 <= x < 80:
return 0x00ff00 + (x - 64) * 256 * 256 * 17
elif 80 <= x < 96:
return 0xffff00 - (x - 80) * 256 * 17
elif 96 <= x < 112:
return 0xff0000 - (x - 96) * 256 * 256 * 17
else:
raise TypeError(x)
def calc_x(m, r, w, x):
return m + (x - w) / r
def calc_y(m, r, h, y):
return m + (y - h) / r
def rcalc_x(m, r, w, x):
return (r - m) * x + w
def rcalc_y(m, r, h, y):
return (r - m) * y + h
if __name__ == '__main__':
image = bmp(2048, 1536)
image.gen_bmp_header()
image.print_bmp_header()
image.paint_bgcolor(0xffffff)
for i in range(1000):
x = random() * 16 - 8
y = random() * 16 - 8
z0 = complex(x, y)
z1 = newton(z0)
for j in range(112):
z0 = z1
z1 = newton(z0)
#print(z1)
if z1 == None:
print('n')
break
i0 = rcalc_x(0, 384, 1024, z0.real)
j0 = rcalc_y(0, 384, 768, z0.imag)
i1 = rcalc_x(0, 384, 1024, z1.real)
j1 = rcalc_y(0, 384, 768, z1.imag)
i0 = int(i0)
j0 = int(j0)
i1 = int(i1)
j1 = int(j1)
try:
image.paint_line(i0, j0, i1, j1, calc_color(j))
#print(j)
#print(z0, z1)
except IndexError:
print('i')
break
except ZeroDivisionError:
pass
image.save_image("newton-4.bmp")