diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-05-17 22:15:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-17 22:15:22 +0100 |
| commit | 1be3cc268a99c8e93b404145df89b0e48bd7501a (patch) | |
| tree | 60d77d52455d9b0b81dcb581c466c139d980345a | |
| parent | ec14309856757743e0cb4929a65800015dd6493d (diff) | |
| parent | 4d5447118da51f918c1a1f3347f676a83957039b (diff) | |
| download | perlweeklychallenge-club-1be3cc268a99c8e93b404145df89b0e48bd7501a.tar.gz perlweeklychallenge-club-1be3cc268a99c8e93b404145df89b0e48bd7501a.tar.bz2 perlweeklychallenge-club-1be3cc268a99c8e93b404145df89b0e48bd7501a.zip | |
Merge pull request #6121 from pokgopun/pwc165
pwc165 solution
| -rw-r--r-- | challenge-165/pokgopun/go/ch-1.go | 107 | ||||
| -rw-r--r-- | challenge-165/pokgopun/go/ch-2.go | 66 |
2 files changed, 173 insertions, 0 deletions
diff --git a/challenge-165/pokgopun/go/ch-1.go b/challenge-165/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..cace8187e2 --- /dev/null +++ b/challenge-165/pokgopun/go/ch-1.go @@ -0,0 +1,107 @@ +/* +### Interactively enter points and lines and output to STDOUT: + +go run ch-1.go + +Enter 'x1,y1' for a point or 'x1,y1,x2,y2' for a line, or '0' to end this +53,10 +53,10,23,30 +23,30 +0 +<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> +<svg height="60.000000" width="106.000000" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> + <g id="lines" stroke="#369" stroke-width="4"><line x1="53.000000" x2="23.000000" y1="10.000000" y2="30.000000" /></g><g fill="#f73" id="points"><circle cx="53.000000" cy="10.000000" r="3" /><circle cx="23.000000" cy="30.000000" r="3" /></g></svg> + +### Pass points and line data via STDIN and output to file ch-1.svg: + +echo -e "53,10\n53,10,23,30\n23,30\n0" | go run ch-1.go ch-1.svg +*/ +package main + +import ( + "bufio" + "fmt" + "io" + "log" + "os" +) + +type point struct{ x, y float64 } +type line struct { + start point + end point +} + +type Svg struct { + points []point + lines []line + height, width float64 +} + +func (svg *Svg) updateHW() { + var points []point + for _, l := range svg.lines { + points = append(points, l.start, l.end) + } + for _, p := range append(points, svg.points...) { + if p.x > svg.width { + svg.width = p.x + } + if p.y > svg.height { + svg.height = p.y + } + } +} + +func (svg *Svg) input() { + var x1, y1, x2, y2 float64 + guide := "Enter 'x1,y1' for a point or 'x1,y1,x2,y2' for a line, or '0' to end this" + //fmt.Println(guide) + for { + n, err := fmt.Scanf("%f,%f,%f,%f\n", &x1, &y1, &x2, &y2) + //fmt.Println("n =>", n, "err =>", err) + switch n { + case 2: + svg.points = append(svg.points, point{x1, y1}) + case 4: + svg.lines = append(svg.lines, line{point{x1, y1}, point{x2, y2}}) + default: + if err == io.EOF || n == 1 && x1 == 0 { + goto done + } + fmt.Fprintln(os.Stderr, guide) + continue + } + //fmt.Println("svg =>", svg) + } +done: +} +func main() { + w := bufio.NewWriter(os.Stdout) + if len(os.Args) > 1 { + f, err := os.Create(os.Args[1]) + if err != nil { + log.Fatal(err) + } + defer f.Close() + w = bufio.NewWriter(f) + } + var data Svg + data.input() + data.updateHW() + //fmt.Println(data.height, data.width) + fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8" standalone="yes"?> +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"> +<svg height="%f" width="%f" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> + <g id="lines" stroke="#369" stroke-width="4">`, data.height*2, data.width*2) + for _, l := range data.lines { + fmt.Fprintf(w, `<line x1="%f" x2="%f" y1="%f" y2="%f" />`, l.start.x, l.end.x, l.start.y, l.end.y) + } + w.WriteString(`</g><g fill="#f73" id="points">`) + for _, p := range data.points { + fmt.Fprintf(w, `<circle cx="%f" cy="%f" r="3" />`, p.x, p.y) + } + w.WriteString(`</g></svg>`) + w.Flush() +} diff --git a/challenge-165/pokgopun/go/ch-2.go b/challenge-165/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..a3d0d9c451 --- /dev/null +++ b/challenge-165/pokgopun/go/ch-2.go @@ -0,0 +1,66 @@ +/* +### To pipe output to ch1.go to create svg file + +go run ch-2.go | go run ch-1.go ch-2.svg + +*/ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "regexp" +) + +func main() { + //rawin := `2,4 3,5 5,7 7,10 9,15` + /**/ + rawin := `333,129 39,189 140,156 292,134 393,52 160,166 362,122 13,193 + 341,104 320,113 109,177 203,152 343,100 225,110 23,186 282,102 + 284,98 205,133 297,114 292,126 339,112 327,79 253,136 61,169 + 128,176 346,72 316,103 124,162 65,181 159,137 212,116 337,86 + 215,136 153,137 390,104 100,180 76,188 77,181 69,195 92,186 + 275,96 250,147 34,174 213,134 186,129 189,154 361,82 363,89` + /**/ + var ds struct{ count, x, y, sumx, sumy, sumxx, sumxy, m, b, x1, x2, y1, y2 float64 } + _, err := fmt.Sscanf(rawin, "%f,%f %f,%f", &ds.x1, &ds.y1, &ds.x2, &ds.y2) + if err != nil { + log.Fatal(err) + } + w := bufio.NewWriter(os.Stdout) + for _, v := range regexp.MustCompile(`\d+,\d+`).FindAllString(rawin, -1) { + _, err := fmt.Sscanf(v, "%f,%f", &ds.x, &ds.y) + if err != nil { + log.Fatal(err) + } + fmt.Fprintln(w, v) + ds.count++ + ds.sumx += ds.x + ds.sumy += ds.y + ds.sumxx += ds.x * ds.x + ds.sumxy += ds.x * ds.y + if ds.x1 > ds.x { + ds.x1 = ds.x + } + if ds.x2 < ds.x { + ds.x2 = ds.x + } + /* + if ds.y1 > ds.y { + ds.y1 = ds.y + } + if ds.y2 < ds.y { + ds.y2 = ds.y + } + */ + } + ds.m = (ds.count*ds.sumxy - ds.sumx*ds.sumy) / (ds.count*ds.sumxx - ds.sumx*ds.sumx) + ds.b = (ds.sumy - ds.m*ds.sumx) / ds.count + ds.y1 = ds.m*ds.x1 + ds.b + ds.y2 = ds.m*ds.x2 + ds.b + fmt.Fprintf(w, "%f,%f,%f,%f\n", ds.x1, ds.y1, ds.x2, ds.y2) + w.Flush() + //fmt.Printf("%+v\n", ds) +} |
