Last active
December 25, 2015 03:09
-
-
Save magnunleno/6907833 to your computer and use it in GitHub Desktop.
Script that draws Hexagons with Python and Cairo
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
| #!/usr/bin/env python | |
| # encoding: utf-8 | |
| import cairo | |
| import rsvg | |
| import math | |
| WIDTH, HEIGHT = (500, 500) | |
| HEX_W, HEX_H = (50, 50) | |
| def draw_hex(ctx, x0, y0): | |
| ctx.move_to(x0, y0) | |
| ctx.rel_move_to(HEX_W/3.0, 0) | |
| ctx.rel_line_to(HEX_W/3.0, 0) | |
| ctx.rel_line_to(HEX_W/3.0, HEX_H/3.0) | |
| ctx.rel_line_to(0, HEX_H/3.0) | |
| ctx.rel_line_to(-HEX_W/3.0, HEX_H/3.0) | |
| ctx.rel_line_to(-HEX_W/3.0, 0) | |
| ctx.rel_line_to(-HEX_W/3.0, -HEX_H/3.0) | |
| ctx.rel_line_to(0, -HEX_H/3.0) | |
| ctx.rel_line_to(HEX_W/3.0, -HEX_H/3.0) | |
| ctx.close_path () | |
| ctx.stroke () | |
| if __name__ == '__main__': | |
| fo = file('hex.svg', 'w') | |
| surface = cairo.SVGSurface(fo, WIDTH, HEIGHT) | |
| ctx = cairo.Context(surface) | |
| ctx.set_line_width(3) | |
| ctx.set_source_rgb(0, 0, 0) | |
| p = [0,0] | |
| for i in range(10): | |
| for n in range(10): | |
| draw_hex(ctx, *p) | |
| p[1] += HEX_H | |
| p[1] = 0 | |
| p[0] += HEX_W | |
| surface.finish() | |
| fo.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment