Permalink
Browse files

use a buffer instead of writing directly to os.Stdout (thanks to tip …

…from Robert Milton/Nigel Tao)
  • Loading branch information...
kidoman committed Sep 24, 2013
1 parent 5f16e41 commit e9c418ec3a77d014ced05bcbd52f38aa3ef7c2af
Showing with 12 additions and 4 deletions.
  1. +12 −4 main.go
View
16 main.go
@@ -1,6 +1,7 @@
package main
import (
"bytes"
"flag"
"fmt"
"github.com/kid0m4n/gorays/vector"
@@ -46,7 +47,12 @@ func main() {
defer pprof.StopCPUProfile()
}
fmt.Printf("P6 %v %v 255 ", *width, *height)
header := fmt.Sprintf("P6 %v %v 255 ", *width, *height)
headerLen := len([]byte(header))
fileLen := headerLen + 3**width**height // Lenght of header + 3 bytes per pixel
buf := &bytes.Buffer{}
buf.Grow(fileLen)
g := vector.Vector{X: -5.5, Y: -16, Z: 0}.Normalize()
a := vector.Vector{X: 0, Y: 0, Z: 1}.CrossProduct(g).Normalize().Scale(0.002)
@@ -64,11 +70,13 @@ func main() {
p = sampler(orig, dir).Scale(3.5).Add(p)
}
if n, err := os.Stdout.Write([]byte{byte(p.X), byte(p.Y), byte(p.Z)}); n != 3 || err != nil {
panic(err)
}
buf.Write([]byte{byte(p.X), byte(p.Y), byte(p.Z)})
}
}
if n, err := os.Stdout.Write(buf.Bytes()); n != buf.Len() || err != nil {
log.Panic(err)
}
}
func sampler(orig, dir vector.Vector) vector.Vector {

0 comments on commit e9c418e

Please sign in to comment.