Created
April 29, 2014 18:54
-
-
Save mstine/11408788 to your computer and use it in GitHub Desktop.
Broken...but why?
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
package main | |
import ( | |
"fmt" | |
"private/point/point" | |
) | |
func main() { | |
//tag::NewPoint[] | |
p := *new(point.Point) | |
p.X = 1 | |
p.Y = 2 | |
//end::NewPoint[] | |
//tag::PointLiteral[] | |
p = point.Point{1, 2} | |
//end::PointLiteral[] | |
//tag::TranslatingPoints[] | |
q := p.Translate(5, 5) | |
fmt.Printf("Translated %v to %v\n", p, q) | |
//end::TranslatingPoints[] | |
//tag::TranslatingPointers[] | |
qP := &point.Point{1, 2} | |
qP.TranslatePointer(5, 5) | |
fmt.Printf("Translated using pointer to %v\n", qP) | |
//end::TranslatingPointers[] | |
//tag::PrintingPointAndColorPoint[] | |
r := point.ColorPoint{point.Point{1, 4}, point.BLUE} | |
fmt.Printf("Point: %v\n", p) | |
fmt.Printf("Color Point: %v\n", r) | |
//end::PrintingPointAndColorPoint[] | |
//tag::CalculatingDistance[] | |
fmt.Printf("Dist b/w p and q = %v\n", p.DistanceTo(r)) | |
fmt.Printf("Dist b/w q and p = %v\n", r.DistanceTo(p)) | |
//end::CalculatingDistance[] | |
} |
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
package point | |
import "math" | |
//tag::PointStruct[] | |
type Point struct { | |
X, Y float64 | |
} | |
//end::PointStruct[] | |
//tag::ColorPoint[] | |
const ( | |
BLUE = iota | |
RED = iota | |
GREEN = iota | |
) | |
type ColorPoint struct { | |
Point Point | |
Color int | |
} | |
//end::ColorPoint[] | |
//tag::Translate[] | |
func (p Point) Translate(xDist float64, yDist float64) Point { | |
return Point{p.X + xDist, p.Y + yDist} | |
} | |
//end::Translate[] | |
//tag::TranslatePointer[] | |
func (p *Point) TranslatePointer(xDist float64, yDist float64) { | |
p.X = p.X + xDist | |
p.Y = p.Y + yDist | |
} | |
//end::TranslatePointer[] | |
//tag::DistanceCalculator[] | |
type DistanceCalculator interface { | |
Coordinates() Point | |
DistanceTo(d DistanceCalculator) float64 | |
} | |
//end::DistanceCalculator[] | |
//tag::PointSatisfiesDC[] | |
func (p Point) Coordinates() Point { | |
return p | |
} | |
func (p Point) DistanceTo(d DistanceCalculator) float64 { | |
q := d.Coordinates() | |
sqOfXDist := math.Pow(p.X-q.X, 2) | |
sqOfYDist := math.Pow(p.Y-q.Y, 2) | |
return math.Sqrt(sqOfXDist + sqOfYDist) | |
} | |
//end::PointSatisfiesDC[] | |
//tag::ColorPointSatisfiesDC[] | |
func (cp ColorPoint) Coordinates() Point { | |
return cp.Point | |
} | |
func (cp ColorPoint) DistanceTo(d DistanceCalculator) float64 { | |
p := cp.Point | |
q := d.Coordinates() | |
sqOfXDist := math.Pow(p.X-q.X, 2) | |
sqOfYDist := math.Pow(p.Y-q.Y, 2) | |
return math.Sqrt(sqOfXDist + sqOfYDist) | |
} | |
//end::ColorPointSatisfiesDC[] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment