Skip to content

Instantly share code, notes, and snippets.

View markphelps's full-sized avatar

Mark Phelps markphelps

View GitHub Profile
func TestListRepos(t *testing.T) {
teardown := setup()
defer teardown()
mux.HandleFunc("/orgs/octokit/repos", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
// ... return the JSON
})
package api
var (
mux *http.ServeMux
server *httptest.Server
client *Client
)
func setup() func() {
mux = http.NewServeMux()
@markphelps
markphelps / client.go
Last active September 24, 2017 14:58
// Option is a functional option for configuring the API client
type Option func(*Client) error
// BaseURL allows overriding of API client baseURL for testing
func BaseURL(baseURL string) Option {
return func(c *Client) error {
c.baseURL = baseURL
return nil
}
}
@markphelps
markphelps / client.go
Last active September 24, 2017 19:21
const apiURL = "https://api.github.com/"
// Client holds information necessary to make a request to your API
type Client struct {
baseURL string
httpClient *http.Client
}
// New creates a new API client
func New() (*Client, error) {
$ ./go-trace --help
Usage of ./go-trace:
-aperture float
camera aperture (default 0.01)
-fov float
vertical field of view (degrees) (default 75)
-height int
height of image (default 500)
-out string
output filename (default "out.ppm")
// inside the render function in main.go
// create the ticker to tick every 100 milliseconds
ticker := time.NewTicker(time.Millisecond * 100)
// ...
// create an anonymous goroutine to consume from the channel
// and print after each tick
go func() {
start := time.Now()
// .. some long running operation
fmt.Printf("\nDone.\nElapsed: %v\n", time.Since(start))
package primitives
type Metal struct {
C Vector
Fuzz float64
}
func (m Metal) Bounce(input Ray, hit Hit) (bool, Ray) {
direction := reflect(input.Direction, hit.Normal)
fuzzed := VectorInUnitSphere().MultiplyScalar(m.Fuzz))
package primitives
type Metal struct {
C Vector
}
func (m Metal) Bounce(input Ray, hit Hit) (bool, Ray) {
direction := reflect(input.Direction, hit.Normal)
bouncedRay := Ray{hit.Point, direction}
bounced := direction.Dot(hit.Normal) > 0
func color(r p.Ray, world p.Hitable, depth int) p.Vector {
hit, record := world.Hit(r, 0.001, math.MaxFloat64)
if hit {
if depth < 50 {
bounced, bouncedRay := record.Bounce(r, record)
if bounced {
newColor := color(bouncedRay, world, depth+1)
return record.Material.Color().Multiply(newColor)
}