Created
March 16, 2015 14:56
-
-
Save jackyyf/676022159aa9050f04cf to your computer and use it in GitHub Desktop.
Gist by paste.py @ 2015-03-16 22:56:36.717214
This file contains 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 -*- | |
# Author: Yifu Yu <[email protected]> | |
import math | |
import re | |
import sys | |
deg2rad = lambda deg : deg / 180.0 * math.pi | |
class Dot(object): | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
def translate(self, dx, dy): | |
self.x += dx | |
self.y += dy | |
def len(self): | |
return math.sqrt(self.x * self.x + self.y * self.y) | |
def deg(self): | |
return math.atan2(self.y, self.x) | |
def rotate(self, deg): | |
clen = self.len() | |
if clen < 1e-8: | |
# AT DOT (0, 0), DO NOT ROTATE | |
return | |
cdeg = self.deg() + deg2rad(deg) | |
self.x = math.cos(cdeg) * clen | |
self.y = math.sin(cdeg) * clen | |
# Floating error confirmation. | |
assert abs(self.len() - clen) < 1e-8 | |
def xy(self): | |
return self.x, self.y | |
class DrawObjects(object): | |
def draw(self): | |
print('{:.13f} {:.13f} moveto'.format(*self.dots[0].xy())) | |
for dot in self.dots[1:]: | |
print('{:.13f} {:.13f} lineto'.format(*dot.xy())) | |
print('stroke') | |
def translate(self, dx, dy): | |
for d in self.dots: | |
d.translate(dx, dy) | |
def rotate(self, deg): | |
for d in self.dots: | |
d.rotate(deg) | |
class Line(DrawObjects): | |
def __init__(self, x0, y0, x1, y1): | |
self.dots = [Dot(x0, y0), Dot(x1, y1)] | |
class Rectangle(DrawObjects): | |
def __init__(self, x, y, w, h): | |
self.dots = [Dot(x, y), Dot(x + w, y), Dot(x + w, y + h), Dot(x, y + h), Dot(x, y)] | |
commands = map(lambda s: s.split(), re.compile('\((.*?)\)').findall(sys.stdin.read())) | |
ops = list() | |
print('%!PS-Adobe-3.1') | |
for command in commands: | |
if command[0] == 'line': | |
l = Line(*map(float, command[1:])) | |
while ops: | |
op = ops.pop() | |
if op[0] == 'translate': | |
x, y = float(op[1]), float(op[2]) | |
l.translate(x, y) | |
elif op[0] == 'rotate': | |
deg = float(op[1]) | |
l.rotate(deg) | |
else: | |
print('WTF', file=sys.stderr) | |
sys.exit(233) | |
l.draw() | |
elif command[0] == 'rect': | |
r = Rectangle(*map(float, command[1:])) | |
while ops: | |
op = ops.pop() | |
if op[0] == 'translate': | |
x, y = float(op[1]), float(op[2]) | |
r.translate(x, y) | |
elif op[0] == 'rotate': | |
deg = float(op[1]) | |
r.rotate(deg) | |
else: | |
print('WTF', file=sys.stderr) | |
sys.exit(233) | |
r.draw() | |
elif command[0] == 'translate': | |
ops.append(command) | |
elif command[0] == 'rotate': | |
ops.append(command) | |
elif command[0] == 'color': | |
r, g, b = map(float, command[1:]) | |
print('{:.13f} {:.13f} {:.13f} setrgbcolor'.format(r, g, b)) | |
elif command[0] == 'linewidth': | |
w = float(command[1]) | |
print('{:.13f} setlinewidth'.format(w)) | |
else: | |
print('Syntax Error: Unknown command {}'.format(command[0]), file=sys.stderr) | |
sys.exit(233) | |
print('showpage') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment