Last active
July 12, 2016 08:59
-
-
Save robbintt/ed0861d02805c6e00011c26dbb45a2ef to your computer and use it in GitHub Desktop.
A simple example of the line_rasterizer used with the flaschen.py library.
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
| import flaschen | |
| UDP_IP = 'localhost' | |
| UDP_PORT = 1337 | |
| ft = flaschen.Flaschen(UDP_IP, UDP_PORT, 45, 35) | |
| 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: | |
| xyratio = float(xlen)/ylen | |
| # make a stretched xlen | |
| xarray = [int(round(xlower+x*xyratio)) for x in range(ylen+1)] | |
| yarray = range(ylower, yupper+1) | |
| if xlen > ylen: | |
| yxratio = float(ylen)/xlen | |
| # make a stretched ylen | |
| 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 | |
| """ | |
| # two points describe a line | |
| # we will use the bounding pixels for our line | |
| print "Please input two points. (x1, y1) and (x2, y2)." | |
| x1 = int(raw_input("x1=")) | |
| y1 = int(raw_input("y1=")) | |
| x2 = int(raw_input("x2=")) | |
| y2 = int(raw_input("y2=")) | |
| p1 = (x1, y1) | |
| p2 = (x2, y2) | |
| color = (255, 255, 255) | |
| # get all the points in the line segment | |
| points = line_rasterizer(p2, p1) | |
| for x,y in points: | |
| ft.set(x, y, color) | |
| ft.send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment