## 2019年6月10日月曜日

### Python - Triangles - Creating a Triangle、Point、Edge、Normal Vector

The Ray Tracer Challenge: A Test-Driven Guide to Your First 3D Renderer (Jamis Buck(著)、Pragmatic Bookshelf)、Chapter 15(Triangles)のTriangles、Test #1(Creating a Triangle)を取り組んでみる。

コード

triangles_test.py

```#!/usr/bin/env python3
from unittest import TestCase, main
from tuples import Point, Vector
from triangles import Triangle

class TriangleTest(TestCase):
def setUp(self):
pass

def tearDown(self):
pass

def test_init(self):
point1 = Point(0, 1, 0)
point2 = Point(-1, 0, 0)
point3 = Point(1, 0, 0)
triangle = Triangle(point1, point2, point3)
self.assertEqual(triangle.point1, point1)
self.assertEqual(triangle.point2, point2)
self.assertEqual(triangle.point3, point3)
self.assertEqual(triangle.edge1, Vector(-1, -1, 0))
self.assertEqual(triangle.edge2, Vector(1, -1, 0))
self.assertEqual(triangle.normal, Vector(0, 0, -1))

if __name__ == '__main__':
main()
```

triangles.py

```class Triangle:
def __init__(self, point1, point2, point3):
self.point1 = point1
self.point2 = point2
self.point3 = point3
self.edge1 = point2 - point1
self.edge2 = point3 - point1
self.normal = self.edge2.cross(self.edge1).normalize()
```

```C:\Users\...>py triangles_test.py
....................
----------------------------------------------------------------------
Ran 20 tests in 0.001s

OK

C:\Users\...>
```