File: bresenham.py

package info (click to toggle)
lightyears 1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 1,324 kB
  • ctags: 454
  • sloc: python: 3,499; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,170 bytes parent folder | download | duplicates (4)
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
# 
# 20,000 Light Years Into Space
# This game is licensed under GPL v2, and copyright (C) Jack Whitham 2006-07.
# 



# Line drawing algorithm. Produces a list. This is useful for all sorts
# of linear interpolations.
#
# Page 78, Computer Graphics Principles and Practice (2nd. Ed), Foley et al.

def Line((x1,y1),(x2,y2)):
   
    x1 = int(x1)
    y1 = int(y1)
    x2 = int(x2)
    y2 = int(y2)
    dx = x2 - x1
    dy = y2 - y1

    if ( abs(dx) < abs(dy) ):
        # Gradient is steeper than 1. Correct this.
        return [ (x,y) for (y,x) in Line((y1,x1),(y2,x2)) ]

    if ( dx < 0 ):
        # Line is reversed. Correct this.
        l = Line((x2,y2),(x1,y1))
        l.reverse()
        return l

    if ( dy < 0 ):
        direction = -1
        dy = - dy
    else:
        direction = 1

    d = ( 2 * dy ) - dx
    incr_e = 2 * dy
    incr_ne = 2 * ( dy - dx )
    y = y1
    l = [(x1,y)]

    for x in range(x1, x2 + 1):
        if ( d <= 0 ):
            d += incr_e # move east
        else:
            d += incr_ne # move northeast
            y += direction
        l.append((x,y))
    
    if ( y != y2 ):
        l.append((x2,y2))

    return l