2019年6月12日水曜日

開発環境

The Ray Tracer Challenge: A Test-Driven Guide to Your First 3D Renderer (Jamis Buck(著)、Pragmatic Bookshelf)、Chapter 15(Triangles)のTriangles、Test #2(Normal Vector for 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))

    def test_finding_normal_vector(self):
        triangle = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0))
        normal_vector1 = triangle.local_normal_at(Point(0, 0.5, 0))
        normal_vector2 = triangle.local_normal_at(Point(-0.5, 0.75, 0))
        normal_vector3 = triangle.local_normal_at(Point(0.5, 0.25, 0))
        self.assertEqual(normal_vector1, triangle.normal)
        self.assertEqual(normal_vector2, triangle.normal)
        self.assertEqual(normal_vector3, triangle.normal)


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()

    def local_normal_at(self, point):
        return self.normal

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal、Jupyter(IPython))

C:\Users\...>py triangles_test.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

C:\Users\...>

0 コメント:

コメントを投稿

関連コンテンツ