Решение на Пресичания от Здравко Георгиев

Обратно към всички решения

Към профила на Здравко Георгиев

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 13 успешни тест(а)
  • 7 неуспешни тест(а)

Код

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() {
}

Лог от изпълнението

PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
--- FAIL: TestTriangleRayOppositeDirection (0.00s)
    solution_test.go:206: Expected intersection to be false but it was not
FAIL
exit status 1
FAIL	_/tmp/d20181122-57-1t7gcfz	0.002s
--- FAIL: TestTriangleRayNearMiss (0.00s)
    solution_test.go:206: Expected intersection to be false but it was not
FAIL
exit status 1
FAIL	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
--- FAIL: TestTriangleRayOriginReallyCloseToObject (0.00s)
    solution_test.go:206: Expected intersection to be false but it was not
FAIL
exit status 1
FAIL	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
--- FAIL: TestQuadRayOppositeDirection (0.00s)
    solution_test.go:206: Expected intersection to be false but it was not
FAIL
exit status 1
FAIL	_/tmp/d20181122-57-1t7gcfz	0.002s
--- FAIL: TestQuadNearMiss (0.00s)
    solution_test.go:206: Expected intersection to be false but it was not
FAIL
exit status 1
FAIL	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
--- FAIL: TestQuadOriginReallyClosedToObject (0.00s)
    solution_test.go:206: Expected intersection to be false but it was not
FAIL
exit status 1
FAIL	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
--- FAIL: TestQuadIrregularMiss (0.00s)
    solution_test.go:206: Expected intersection to be false but it was not
FAIL
exit status 1
FAIL	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s
PASS
ok  	_/tmp/d20181122-57-1t7gcfz	0.002s

История (1 версия и 0 коментара)

Здравко обнови решението на 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() {
+}