## 2017年1月30日月曜日

### Python - 数学 - 体 - C(複素数)と遊ぼう - 複素数に i を掛けること: 90度の回転

コード(Emacs)

```#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from plotting import plot

s = {2+2j, 3+2j, 1.75+1j, 2+1j, 2.25+1j, 2.5+1j, 2.75+1j, 3+1j, 3.25+1j}

plot({z * 1j * 1/2 for z in s})

input()
```

```\$ ./sample4_8.py
q
\$
```

コード(Emacs)

```#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from plotting import plot

s = {2+2j, 3+2j, 1.75+1j, 2+1j, 2.25+1j, 2.5+1j, 2.75+1j, 3+1j, 3.25+1j}

plot({z * 1j * 1/2 + (2-1j) for z in s})

input()
```

```\$ ./sample4_9.py
q
\$
```

コード(Emacs)

```#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from plotting import plot
import image

filename = 'img01.png'

data = image.file2image(filename)
height = len(data)
width = len(data[0])

pts = [ x + (height - y) * 1j
for y, l in enumerate(data)
for x, (pt, _, _) in enumerate(l)
if pt < 120]

plot({pt for pt in pts}, max(width, height))

input()
```

```\$ ./sample4_10.py
q
\$
```

コード(Emacs)

```#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from plotting import plot
import image

s = {2+2j, 3+2j, 1.75+1j, 2+1j, 2.25+1j, 2.5+1j, 2.75+1j, 3+1j, 3.25+1j}

def f(z):
reals = {z0.real for z0 in z}
imags = {z0.imag for z0 in z}
real_c = (max(reals) + min(reals)) / 2
imag_c = (max(imags) + min(imags)) / 2
return {z0 - real_c - imag_c * 1j for z0 in z}

plot(f(s))

input()
```

```\$ ./sample4_11.py
q
\$
```

コード(Emacs)

```#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from plotting import plot
import image

filename = 'img01.png'

data = image.file2image(filename)
height = len(data)
width = len(data[0])

pts = [ x + (height - y) * 1j
for y, l in enumerate(data)
for x, (pt, _, _) in enumerate(l)
if pt < 120]

def f(z):
reals = {z0.real for z0 in z}
imags = {z0.imag for z0 in z}
real_c = (max(reals) + min(reals)) / 2
imag_c = (max(imags) + min(imags)) / 2
return {z0 - real_c - imag_c * 1j for z0 in z}

plot(f(pts), max(width, height))

input()
```

```\$ ./sample4_12.py
q
\$
```