This repository has been archived by the owner on Aug 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw
executable file
·70 lines (54 loc) · 1.6 KB
/
draw
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
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import sys
import math
import cairo
import PSDraw
from parser import parse
from util import intersect
REGION_EXTEND = 500
RANGE = 500
def main():
if len(sys.argv) < 2:
print "Usage: draw.py FILE"
sys.exit()
filename = sys.argv[1]
out_filename = filename + '.svg'
f = open(filename)
draw(f, out_filename)
f.close()
def draw(f, out):
points, roads, regions = parse(f)
mX = max([p.x for p in points])
mY = max([p.y for p in points])
surface = cairo.SVGSurface(out, mX, mY)
context = cairo.Context(surface)
context.set_line_width(2)
context.set_source_rgb(1, 1, 1)
context.rectangle(0, 0, mX, mY)
context.fill()
for region in regions:
context.set_source_rgba(0, 1, 0, 0.5)
context.rectangle(region['x'] - REGION_EXTEND, region['y'], REGION_EXTEND, region['height'])
context.fill()
context.rectangle(region['x'] + region['width'], region['y'], REGION_EXTEND, region['height'])
context.fill()
context.set_source_rgba(1, 0, 0, 0.5)
context.rectangle(region['x'], region['y'], region['width'], region['height'])
context.fill()
context.set_source_rgb(0, 0, 0)
for road in roads:
context.move_to(road.start.x, road.start.y)
context.line_to(road.end.x, road.end.y)
context.stroke()
context.set_source_rgb(0, 0, 1)
for region in regions:
for road in roads:
intersection = intersect(road, region)
if intersection != None:
context.arc(intersection[0], intersection[1], RANGE, 0, 2 * math.pi)
context.stroke()
context.arc(intersection[0], intersection[1], 2, 0, 2 * math.pi)
context.fill()
surface.finish()
if __name__ == '__main__':
main()