Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleanup mandelbrot

  • Loading branch information...
commit a0c88180614b4c2d1d1cae207d49fd8a3db5d265 1 parent 2f44251
@robertwb robertwb authored
Showing with 23 additions and 32 deletions.
  1. +23 −32 mandelbrot_cython.pyx
View
55 mandelbrot_cython.pyx
@@ -3,61 +3,52 @@
#
# contributed by Robert Bradshaw
-import sys
+import sys
def main(int size, outfile=sys.stdout):
- cdef int iter = 50
- cdef int i, xi, yi
+ cdef int i, x, y
cdef double step = 2.0 / size
cdef double Cx, Cy, Zx, Zy, Tx, Ty
-
+
cdef line = ' ' * ((size+7) // 8)
- cdef char* buf = line
- cdef unsigned char byte_accumulate
-
+ cdef char *buf = line
+ cdef unsigned char byte_acc
+
write = outfile.write
write("P4\n%s %s\n" % (size, size))
-
- for yi in range(size):
- byte_accumulate = 0
+ for y in range(size):
+
+ byte_acc = 0
- for xi in range(size):
+ for x in range(size):
+
+ i = 50
+ Zx = Cx = step*x - 1.5
+ Zy = Cy = step*y - 1.0
- i = iter
- Zx = Cx = step*xi - 1.5
- Zy = Cy = step*yi - 1.0
-
Tx = Zx * Zx
Ty = Zy * Zy
while True:
# Z = Z^2 + C
- Zx, Zy = Tx - Ty + Cx , Zx * Zy + Zx * Zy + Cy
+ Zx, Zy = Tx - Ty + Cx, Zx * Zy + Zx * Zy + Cy
Tx = Zx * Zx
Ty = Zy * Zy
i -= 1
if (i == 0) | (Tx + Ty > 4.0):
break
-
- byte_accumulate = (byte_accumulate << 1) | (i == 0)
- if xi & 7 == 7:
- buf[xi >> 3] = byte_accumulate
- byte_accumulate = 0 # TESTING
-
+
+ byte_acc = (byte_acc << 1) | (i == 0)
+ if x & 7 == 7:
+ buf[x >> 3] = byte_acc
+
if size & 7 != 0:
# line ending on non-byte boundary
- byte_accumulate <<= 8 - (size & 7)
- buf[size >> 3] = byte_accumulate
+ byte_acc <<= 8 - (size & 7)
+ buf[size >> 3] = byte_acc
write(line)
if __name__ == '__main__':
- n = 16000
- out = sys.stdout
- try:
- n = int(sys.argv[1])
- file = open(sys.argv[2])
- except IndexError:
- pass
- main(n, out)
+ main(int(sys.argv[1]), sys.stdout)
Please sign in to comment.
Something went wrong with that request. Please try again.