Created
June 3, 2014 07:27
-
-
Save mattfitzgerald/895fbd09aaf9a61ba705 to your computer and use it in GitHub Desktop.
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 "math" | |
func main() { | |
s := Sphere{center: Point{0, 0, 0}, radius: 1} | |
p := Point{0.1, 0.1, 0.1} | |
println(s.contains(p)) | |
} | |
type Point struct { | |
x, y, z float64 | |
} | |
func (p *Point) distance_from_origin() float64 { | |
return p.distance_to(Point{0, 0, 0}) | |
} | |
func (p *Point) distance_to(p2 Point) float64 { | |
return math.Sqrt(math.Pow(p2.x-p.x, 2) + math.Pow(p2.y-p.y, 2) + math.Pow(p2.z-p.z, 2)) | |
} | |
type Sphere struct { | |
center Point | |
radius float64 | |
} | |
func (s *Sphere) volume() float64 { | |
return 4.0 / 3.0 * math.Pi * math.Pow(s.radius, 3) | |
} | |
func (s *Sphere) contains(p Point) bool { | |
return p.distance_to(s.center) < s.radius | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment