Решение на Пресичания от Антони Добренов

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

Към профила на Антони Добренов

Резултати

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

Код

package main
import (
"github.com/fmi/go-homework/geom"
"math"
)
type Quad struct {
A geom.Vector
B geom.Vector
C geom.Vector
D geom.Vector
}
type Sphere struct {
Origin geom.Vector
Radius float64
}
type Triangle struct {
A geom.Vector
B geom.Vector
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 {
return false
}
func NewQuad(a, b, c, d geom.Vector) Quad {
return Quad{
A: a,
B: b,
C: c,
D: d,
}
}
func (quad Quad) Intersect(ray geom.Ray) bool {
bSubA := geom.Sub(quad.B, quad.A)
cSubA := geom.Sub(quad.C, quad.A)
normalV := geom.Cross(geom.Sub(quad.B, quad.A), geom.Sub(quad.D, quad.A))
d := geom.Sub(ray.Origin, ray.Direction)
dotND := geom.Dot(normalV, d)
if math.Abs(dotND) < 0.0000001 {
return false
}
t := -(geom.Dot(normalV, geom.Sub(ray.Origin, quad.A)) / dotND)
interP := geom.Add(ray.Origin, geom.Mul(d, t))
p := geom.Sub(interP, quad.A)
x := geom.Dot(p, bSubA)
y := geom.Dot(p, cSubA)
return x >= 0.0 && x <= geom.Dot(bSubA, bSubA) &&
y >= 0.0 && y <= geom.Dot(cSubA, cSubA)
}
func NewSphere(origin geom.Vector, r float64) Sphere {
return Sphere{
Origin: origin,
Radius: r,
}
}
func (sphere Sphere) Intersect(ray geom.Ray) bool {
var t1, t2 float64
c := geom.Dot(ray.Origin, ray.Origin) - sphere.Radius*sphere.Radius
b := 2 * geom.Dot(ray.Origin, ray.Direction)
a := geom.Dot(ray.Direction, ray.Direction)
d := math.Sqrt(b*b - 4.0*a*c)
if d < 0 {
return false
}
if d == 0 {
t1 = -0.5 * (b / a)
t2 = t1
} else {
q := 1.0
if b > 0 {
q = -0.5 * (b - math.Sqrt(d))
} else {
q = -0.5 * (b + math.Sqrt(d))
}
t1 = q / a
t2 = c / q
}
if t1 < 0 && t2 < 0 {
return false
}
return true
}

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

# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]
# _/tmp/d20181122-57-1iug96d [_/tmp/d20181122-57-1iug96d.test]
./solution_test.go:10:14: undefined: NewTriangle
./solution_test.go:20:14: undefined: NewTriangle
./solution_test.go:30:14: undefined: NewTriangle
./solution_test.go:40:14: undefined: NewTriangle
./solution_test.go:50:14: undefined: NewTriangle
./solution_test.go:60:14: undefined: NewTriangle
FAIL	_/tmp/d20181122-57-1iug96d [build failed]

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

Антони обнови решението на 21.11.2018 14:25 (преди 9 месеца)

+package main
+
+import (
+ "github.com/fmi/go-homework/geom"
+
+ "math"
+)
+
+type Quad struct {
+ A geom.Vector
+ B geom.Vector
+ C geom.Vector
+ D geom.Vector
+}
+
+type Sphere struct {
+ Origin geom.Vector
+ Radius float64
+}
+
+type Triangle struct {
+ A geom.Vector
+ B geom.Vector
+ 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 {
+ return false
+}
+
+func NewQuad(a, b, c, d geom.Vector) Quad {
+ return Quad{
+ A: a,
+ B: b,
+ C: c,
+ D: d,
+ }
+}
+
+func (quad Quad) Intersect(ray geom.Ray) bool {
+ bSubA := geom.Sub(quad.B, quad.A)
+ cSubA := geom.Sub(quad.C, quad.A)
+ normalV := geom.Cross(geom.Sub(quad.B, quad.A), geom.Sub(quad.D, quad.A))
+ d := geom.Sub(ray.Origin, ray.Direction)
+ dotND := geom.Dot(normalV, d)
+ if math.Abs(dotND) < 0.0000001 {
+ return false
+ }
+
+ t := -(geom.Dot(normalV, geom.Sub(ray.Origin, quad.A)) / dotND)
+ interP := geom.Add(ray.Origin, geom.Mul(d, t))
+ p := geom.Sub(interP, quad.A)
+ x := geom.Dot(p, bSubA)
+ y := geom.Dot(p, cSubA)
+
+ return x >= 0.0 && x <= geom.Dot(bSubA, bSubA) &&
+ y >= 0.0 && y <= geom.Dot(cSubA, cSubA)
+}
+
+func NewSphere(origin geom.Vector, r float64) Sphere {
+ return Sphere{
+ Origin: origin,
+ Radius: r,
+ }
+}
+
+func (sphere Sphere) Intersect(ray geom.Ray) bool {
+ var t1, t2 float64
+
+ c := geom.Dot(ray.Origin, ray.Origin) - sphere.Radius*sphere.Radius
+ b := 2 * geom.Dot(ray.Origin, ray.Direction)
+ a := geom.Dot(ray.Direction, ray.Direction)
+ d := math.Sqrt(b*b - 4.0*a*c)
+
+ if d < 0 {
+ return false
+ }
+
+ if d == 0 {
+ t1 = -0.5 * (b / a)
+ t2 = t1
+ } else {
+ q := 1.0
+ if b > 0 {
+ q = -0.5 * (b - math.Sqrt(d))
+ } else {
+ q = -0.5 * (b + math.Sqrt(d))
+ }
+ t1 = q / a
+ t2 = c / q
+ }
+
+ if t1 < 0 && t2 < 0 {
+ return false
+ }
+
+ return true
+}
+
+func main() {
+}

Антони обнови решението на 21.11.2018 14:27 (преди 9 месеца)

package main
import (
"github.com/fmi/go-homework/geom"
"math"
)
type Quad struct {
A geom.Vector
B geom.Vector
C geom.Vector
D geom.Vector
}
type Sphere struct {
Origin geom.Vector
Radius float64
}
type Triangle struct {
A geom.Vector
B geom.Vector
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 {
return false
}
func NewQuad(a, b, c, d geom.Vector) Quad {
return Quad{
A: a,
B: b,
C: c,
D: d,
}
}
func (quad Quad) Intersect(ray geom.Ray) bool {
bSubA := geom.Sub(quad.B, quad.A)
cSubA := geom.Sub(quad.C, quad.A)
normalV := geom.Cross(geom.Sub(quad.B, quad.A), geom.Sub(quad.D, quad.A))
d := geom.Sub(ray.Origin, ray.Direction)
dotND := geom.Dot(normalV, d)
if math.Abs(dotND) < 0.0000001 {
return false
}
t := -(geom.Dot(normalV, geom.Sub(ray.Origin, quad.A)) / dotND)
interP := geom.Add(ray.Origin, geom.Mul(d, t))
p := geom.Sub(interP, quad.A)
x := geom.Dot(p, bSubA)
y := geom.Dot(p, cSubA)
return x >= 0.0 && x <= geom.Dot(bSubA, bSubA) &&
y >= 0.0 && y <= geom.Dot(cSubA, cSubA)
}
func NewSphere(origin geom.Vector, r float64) Sphere {
return Sphere{
Origin: origin,
Radius: r,
}
}
func (sphere Sphere) Intersect(ray geom.Ray) bool {
var t1, t2 float64
c := geom.Dot(ray.Origin, ray.Origin) - sphere.Radius*sphere.Radius
b := 2 * geom.Dot(ray.Origin, ray.Direction)
a := geom.Dot(ray.Direction, ray.Direction)
d := math.Sqrt(b*b - 4.0*a*c)
if d < 0 {
return false
}
if d == 0 {
t1 = -0.5 * (b / a)
t2 = t1
} else {
q := 1.0
if b > 0 {
q = -0.5 * (b - math.Sqrt(d))
} else {
q = -0.5 * (b + math.Sqrt(d))
}
t1 = q / a
t2 = c / q
}
if t1 < 0 && t2 < 0 {
return false
}
return true
}
-
-func main() {
-}