11#!/usr/bin/env python
2- from __future__ import division
3- #from numba import jit
2+ from typing import Union , Tuple
3+ # from numba import jit
44'''
55 The MIT License (MIT)
66 Copyright (c) 2014 Michael Hirsch
77 reference: http://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
8- * The best way to Numba JIT this would probably be in the function calling this,
8+ * The best way to Numba JIT this would probably be in the function calling this,
99 to include the loop itself inside the jit decoration.
1010'''
11- #@jit
12- def cohensutherland (xmin , ymax , xmax , ymin , x1 , y1 , x2 , y2 ):
11+
12+ # @jit
13+
14+
15+ def cohensutherland (xmin : float , ymax : float , xmax : float , ymin : float ,
16+ x1 : float , y1 : float , x2 : float , y2 : float ) -> Tuple [
17+ Union [None , float ], Union [None , float ], Union [None , float ], Union [None , float ]]:
1318 """Clips a line to a rectangular area.
1419
1520 This implements the Cohen-Sutherland line clipping algorithm. xmin,
@@ -21,11 +26,11 @@ def cohensutherland(xmin, ymax, xmax, ymin, x1, y1, x2, y2):
2126 four None values will be returned as tuple. Otherwise a tuple of the
2227 clipped line points will be returned in the form (cx1, cy1, cx2, cy2).
2328 """
24- INSIDE ,LEFT , RIGHT , LOWER , UPPER = 0 ,1 , 2 , 4 , 8
29+ INSIDE , LEFT , RIGHT , LOWER , UPPER = 0 , 1 , 2 , 4 , 8
2530
2631 def _getclip (xa , ya ):
27- #if dbglvl>1: print('point: '),; print(xa,ya)
28- p = INSIDE #default is inside
32+ # if dbglvl>1: print('point: '),; print(xa,ya)
33+ p = INSIDE # default is inside
2934
3035 # consider x
3136 if xa < xmin :
@@ -35,29 +40,29 @@ def _getclip(xa, ya):
3540
3641 # consider y
3742 if ya < ymin :
38- p |= LOWER # bitwise OR
43+ p |= LOWER # bitwise OR
3944 elif ya > ymax :
40- p |= UPPER # bitwise OR
45+ p |= UPPER # bitwise OR
4146 return p
4247
4348# check for trivially outside lines
4449 k1 = _getclip (x1 , y1 )
4550 k2 = _getclip (x2 , y2 )
4651
47- #%% examine non-trivially outside points
48- #bitwise OR |
49- while (k1 | k2 ) != 0 : # if both points are inside box (0000) , ACCEPT trivial whole line in box
52+ # %% examine non-trivially outside points
53+ # bitwise OR |
54+ while (k1 | k2 ) != 0 : # if both points are inside box (0000) , ACCEPT trivial whole line in box
5055
5156 # if line trivially outside window, REJECT
52- if (k1 & k2 ) != 0 : # bitwise AND &
53- #if dbglvl>1: print(' REJECT trivially outside box')
54- #return nan, nan, nan, nan
57+ if (k1 & k2 ) != 0 : # bitwise AND &
58+ # if dbglvl>1: print(' REJECT trivially outside box')
59+ # return nan, nan, nan, nan
5560 return None , None , None , None
5661
57- #non-trivial case, at least one point outside window
62+ # non-trivial case, at least one point outside window
5863 # this is not a bitwise or, it's the word "or"
59- opt = k1 or k2 # take first non-zero point, short circuit logic
60- if opt & UPPER : # these are bitwise ANDS
64+ opt = k1 or k2 # take first non-zero point, short circuit logic
65+ if opt & UPPER : # these are bitwise ANDS
6166 x = x1 + (x2 - x1 ) * (ymax - y1 ) / (y2 - y1 )
6267 y = ymax
6368 elif opt & LOWER :
@@ -75,11 +80,10 @@ def _getclip(xa, ya):
7580 if opt == k1 :
7681 x1 , y1 = x , y
7782 k1 = _getclip (x1 , y1 )
78- #if dbglvl>1: print('checking k1: ' + str(x) + ',' + str(y) + ' ' + str(k1))
83+ # if dbglvl>1: print('checking k1: ' + str(x) + ',' + str(y) + ' ' + str(k1))
7984 elif opt == k2 :
80- #if dbglvl>1: print('checking k2: ' + str(x) + ',' + str(y) + ' ' + str(k2))
85+ # if dbglvl>1: print('checking k2: ' + str(x) + ',' + str(y) + ' ' + str(k2))
8186 x2 , y2 = x , y
8287 k2 = _getclip (x2 , y2 )
83-
84- return x1 , y1 , x2 , y2
8588
89+ return x1 , y1 , x2 , y2
0 commit comments