File: sdl2ext_algorithms_test.py

package info (click to toggle)
pysdl2 0.9.9%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,276 kB
  • sloc: python: 18,592; makefile: 148; sh: 40
file content (64 lines) | stat: -rw-r--r-- 2,062 bytes parent folder | download | duplicates (2)
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
import pytest
from sdl2.ext import algorithms as alg


def test_cohensutherland():
    # Test a simple diagonal intersection
    ret = alg.cohensutherland(1, 2, 2, 1, 0, 0, 4, 4)
    assert ret == (1, 1, 2, 2)

    # Test handling of swapped top/bottom values
    ret = alg.cohensutherland(1, 1, 2, 2, 0, 0, 4, 4)
    assert ret == (1, 1, 2, 2)

    # Test partial intersection
    ret = alg.cohensutherland(1, 1, 2, 2, 1.5, 0, 1.5, 1.5)
    assert ret == (1.5, 1, 1.5, 1.5)

    # Test non-intersecting line
    ret = alg.cohensutherland(1, 2, 2, 1, 0, 0, 0, 4)
    assert ret == (None, None, None, None)


def test_liangbarsky():
    # Test a simple diagonal intersection
    ret = alg.liangbarsky(1, 2, 2, 1, 0, 0, 4, 4)
    assert ret == (1, 1, 2, 2)

    # Test handling of swapped top/bottom values
    ret = alg.liangbarsky(1, 1, 2, 2, 0, 0, 4, 4)
    assert ret == (1, 1, 2, 2)

    # Test partial intersection
    ret = alg.liangbarsky(1, 1, 2, 2, 1.5, 0, 1.5, 1.5)
    assert ret == (1.5, 1, 1.5, 1.5)

    # Test non-intersecting line
    ret = alg.liangbarsky(1, 2, 2, 1, 0, 0, 0, 4)
    assert ret == (None, None, None, None)


def test_clipline():
    # Test a simple diagonal intersection w/ default method
    ret = alg.clipline(1, 2, 2, 1, 0, 0, 4, 4)
    assert ret == (1, 1, 2, 2)

    # Test a simple diagonal intersection w/ Liang-Barsky
    ret = alg.clipline(1, 2, 2, 1, 0, 0, 4, 4, method='liangbarsky')
    assert ret == (1, 1, 2, 2)

    # Test a simple diagonal intersection w/ Cohen-Sutherland
    ret = alg.clipline(1, 2, 2, 1, 0, 0, 4, 4, method='cohensutherland')
    assert ret == (1, 1, 2, 2)

    # Test exception with unknown clipping method
    with pytest.raises(ValueError):
        alg.clipline(1, 2, 2, 1, 0, 0, 4, 4, method='cohenlebowski')


def test_point_on_line():
    line = [(1, 1), (3, 3)]
    assert alg.point_on_line(line[0], line[1], (2, 2))
    assert alg.point_on_line(line[0], line[1], (3, 3))
    assert alg.point_on_line(line[1], line[0], (2, 2))
    assert not alg.point_on_line(line[0], line[1], (3, 4))