aboutsummaryrefslogtreecommitdiff
path: root/pic.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-05-03 16:13:31 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-05-03 16:13:31 +0200
commit05b47262e327607d5da1b3e8ff6336d0e5eaa77c (patch)
tree5c47b3217933306126971419f96673fe7b8d8e96 /pic.go
parent9f75aa9fe83e46a4bb3bea9d36ed78a41a4036e3 (diff)
Add pic parser
Diffstat (limited to 'pic.go')
-rw-r--r--pic.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/pic.go b/pic.go
new file mode 100644
index 0000000..7a0a690
--- /dev/null
+++ b/pic.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "log"
+ "os"
+ "strings"
+)
+
+type Page []Path
+
+const scale = 24.5 * 20
+
+func truncate(f float64) float64 {
+ return float64(int(f*100)) / 100
+}
+
+func parseLine(s string) (pa Path) {
+ if strings.HasPrefix(s, "line from ") {
+ for _, p := range strings.Split(s[10:], " to ") {
+ var po Point
+ fmt.Sscanf(p, "%v,%v", &po.Y, &po.X)
+ po.X = truncate(5440 - po.X*scale)
+ po.Y = truncate(po.Y * scale)
+ pa = append(pa, po)
+ }
+ }
+ return pa
+}
+
+func parsePage() (pa Page) {
+ scanner := bufio.NewScanner(os.Stdin)
+ for scanner.Scan() {
+ if p := parseLine(scanner.Text()); p != nil {
+ pa = append(pa, p)
+ }
+ }
+ if err := scanner.Err(); err != nil {
+ log.Fatal(err)
+ }
+ return pa
+}