Здравко обнови решението на 21.11.2018 01:08 (преди 9 месеца)
+package main
+
+import "math"
+import "github.com/fmi/go-homework/geom"
+
+func quadraticEquation(a, b, c float64) (float64, float64) {
+ discriminant := b*b - 4*a*c
+ var root1, root2 float64
+ if discriminant == 0 {
+ root1 = -b / (2 * a)
+ root2 = root1
+ } else if discriminant > 0 {
+ sqRootD := math.Sqrt(discriminant)
+ root1 = (-b + sqRootD) / (2 * a)
+ root2 = (-b - sqRootD) / (2 * a)
+ } else {
+ // primitive error handling: in this case - obviously no intersection
+ root1 = -1
+ root2 = -1
+ }
+ return root1, root2
+}
+
+type Sphere struct {
+ Center geom.Vector
+ Radius float64
+}
+
+type Triangle struct {
+ A geom.Vector
+ B geom.Vector
+ C geom.Vector
+}
+
+type Quad struct {
+ A geom.Vector
+ B geom.Vector
+ C geom.Vector
+ D geom.Vector
+}
+
+func NewSphere(origin geom.Vector, r float64) Sphere {
+ return Sphere{Center: origin, Radius: r}
+}
+
+func NewTriangle(a, b, c geom.Vector) Triangle {
+ return Triangle{A: a, B: b, C: c}
+}
+
+func NewQuad(a, b, c, d geom.Vector) Quad {
+ return Quad{A: a, B: b, C: c, D: d}
+}
+
+func (sphere Sphere) Intersect(ray geom.Ray) bool {
+ l := geom.Sub(ray.Origin, sphere.Center)
+ x := geom.Dot(ray.Direction, ray.Direction)
+ y := 2 * geom.Dot(l, ray.Direction)
+ z := geom.Dot(l, l) - (sphere.Radius * sphere.Radius)
+ intersectX, intersectY := quadraticEquation(x, y, z)
+ return !(intersectX < 0 && intersectY < 0)
+}
+
+func (triangle Triangle) Intersect(ray geom.Ray) bool {
+ // here and below: obviously no-op
+ return true
+}
+
+func (quad Quad) Intersect(ray geom.Ray) bool {
+ return true
+}
+
+func main() {
+}