Йоан обнови решението на 18.11.2018 12:26 (преди 9 месеца)
+package main
+
+import (
+ "github.com/fmi/go-homework/geom"
+ "math"
+)
+
+type Triangle struct {
+ A, B, C geom.Vector
+}
+
+func NewTriangle(a, b, c geom.Vector) Triangle {
+ return Triangle{A: a, B: b, C: c}
+}
+
+func (tr Triangle) Intersect(ray geom.Ray) bool {
+ if (ray.Origin.X <= math.Min(tr.A.X, math.Min(tr.B.X, tr.C.X)) && ray.Direction.X >= math.Max(tr.A.X, math.Max(tr.B.X, tr.C.X)) ||
+ ray.Origin.X >= math.Min(tr.A.X, math.Min(tr.B.X, tr.C.X)) && ray.Direction.X <= math.Max(tr.A.X, math.Max(tr.B.X, tr.C.X))) &&
+ (ray.Origin.X <= math.Min(tr.A.Y, math.Min(tr.B.Y, tr.C.Y)) && ray.Direction.X >= math.Max(tr.A.Y, math.Max(tr.B.Y, tr.C.Y)) ||
+ ray.Origin.X >= math.Min(tr.A.Y, math.Min(tr.B.Y, tr.C.Y)) && ray.Direction.X <= math.Max(tr.A.Y, math.Max(tr.B.Y, tr.C.Y))) &&
+ (ray.Origin.X <= math.Min(tr.A.Z, math.Min(tr.B.Z, tr.C.Z)) && ray.Direction.X >= math.Max(tr.A.Z, math.Max(tr.B.Z, tr.C.Z)) ||
+ ray.Origin.X >= math.Min(tr.A.Z, math.Min(tr.B.Z, tr.C.Z)) && ray.Direction.X <= math.Max(tr.A.Z, math.Max(tr.B.Z, tr.C.Z))) {
+ return true
+ }
+ return false
+}
+
+type Quad struct {
+ A, B, C, D geom.Vector
+}
+
+func NewQuad(a, b, c, d geom.Vector) Quad {
+ return Quad{A: a, B: b, C: c, D: d}
+}
+
+func (qa Quad) Intersect(ray geom.Ray) bool {
+ if (ray.Origin.X <= math.Min(qa.A.X, math.Min(qa.B.X, math.Min(qa.C.X, qa.D.X))) && ray.Direction.X >= math.Max(qa.A.X, math.Max(qa.B.X, math.Max(qa.C.X, qa.D.X))) ||
+ ray.Origin.X >= math.Min(qa.A.X, math.Min(qa.B.X, math.Min(qa.C.X, qa.D.X))) && ray.Direction.X <= math.Max(qa.A.X, math.Max(qa.B.X, math.Max(qa.C.X, qa.D.X)))) &&
+ (ray.Origin.X <= math.Min(qa.A.Y, math.Min(qa.B.Y, math.Min(qa.C.Y, qa.D.Y))) && ray.Direction.X >= math.Max(qa.A.Y, math.Max(qa.B.Y, math.Max(qa.C.Y, qa.D.Y))) ||
+ ray.Origin.X >= math.Min(qa.A.Y, math.Min(qa.B.Y, math.Min(qa.C.Y, qa.D.Y))) && ray.Direction.X <= math.Max(qa.A.Y, math.Max(qa.B.Y, math.Max(qa.C.Y, qa.D.Y)))) &&
+ (ray.Origin.X <= math.Min(qa.A.Z, math.Min(qa.B.Z, math.Min(qa.C.Z, qa.D.Z))) && ray.Direction.X >= math.Max(qa.A.Z, math.Max(qa.B.Z, math.Max(qa.C.Z, qa.D.Z))) ||
+ ray.Origin.X >= math.Min(qa.A.Z, math.Min(qa.B.Z, math.Min(qa.C.Z, qa.D.Z))) && ray.Direction.X <= math.Max(qa.A.Z, math.Max(qa.B.Z, math.Max(qa.C.Z, qa.D.Z)))) {
+ return true
+ }
+ return false
+}
+
+type Sphere struct {
+ O geom.Vector
+ R float64
+}
+
+func NewSphere(origin geom.Vector, r float64) Sphere {
+ return Sphere{O: origin, R: r}
+}
+
+func (sp Sphere) Intersect(ray geom.Ray) bool {
+ if (ray.Origin.X <= sp.O.X-sp.R && ray.Direction.X >= sp.O.X-sp.R || ray.Origin.X >= sp.O.X-sp.R && ray.Direction.X <= sp.O.X-sp.R) &&
+ (ray.Origin.Y <= sp.O.Y-sp.R && ray.Direction.Y >= sp.O.Y-sp.R || ray.Origin.Y >= sp.O.Y-sp.R && ray.Direction.Y <= sp.O.Y-sp.R) &&
+ (ray.Origin.Z <= sp.O.Z-sp.R && ray.Direction.Z >= sp.O.Z-sp.R || ray.Origin.Z >= sp.O.Z-sp.R && ray.Direction.Z <= sp.O.Z-sp.R) {
+ return true
+ }
+ return false
+}