Last active
July 12, 2016 08:44
-
-
Save robbintt/fc540f50284be3b7fdf3fd7dc5d1340a to your computer and use it in GitHub Desktop.
Very basic line segment raster generater
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def line_rasterizer(pt1, pt2): | |
| """ Normalize and rasterize a line based on two of its points. | |
| """ | |
| xlen = abs(pt1[0]-pt2[0]) | |
| ylen = abs(pt1[1]-pt2[1]) | |
| xlower = min(pt1[0], pt2[0]) | |
| xupper = max(pt1[0], pt2[0]) | |
| ylower = min(pt1[1], pt2[1]) | |
| yupper = max(pt1[1], pt2[1]) | |
| if ylen > xlen: | |
| # make a stretched xlen | |
| xyratio = float(xlen)/ylen | |
| xarray = [int(round(xlower+x*xyratio)) for x in range(ylen+1)] | |
| yarray = range(ylower, yupper+1) | |
| if xlen > ylen: | |
| # make a stretched ylen | |
| yxratio = float(ylen)/xlen | |
| yarray = [int(round(ylower+y*yxratio)) for y in range(xlen+1)] | |
| xarray = range(xlower, xupper+1) | |
| if ylen == xlen: | |
| yarray = range(ylower, yupper+1) | |
| xarray = range(xlower, xupper+1) | |
| return zip(xarray, yarray) | |
| if __name__ == "__main__": | |
| """ repack into tests | |
| """ | |
| p1 = (20, 19) | |
| p2 = (10, 15) | |
| print line_rasterizer(p2, p1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment