Решение на Пресичания от Иво Димитров

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

Към профила на Иво Димитров

Резултати

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

Код

package main
import (
"fmt"
"math"
"github.com/fmi/go-homework/geom"
)
func equalsForFloats(a, b float64) bool {
const epsilotn float64 = 1e-7
if math.Abs((a - b)) < a*epsilotn {
return true
}
return false
}
type Triangle struct {
firstPoint geom.Vector
secondPoint geom.Vector
thirdPoint geom.Vector
}
func NewTriangle(a, b, c geom.Vector) Triangle {
var t Triangle
t.firstPoint = a
t.secondPoint = b
t.thirdPoint = c
return t
}
func (t *Triangle) Intersect(ray geom.Ray) bool {
firstVector := geom.Vector{
X: t.secondPoint.X - t.firstPoint.X,
Y: t.secondPoint.Y - t.firstPoint.Y,
Z: t.secondPoint.Z - t.firstPoint.Z}
secondVector := geom.Vector{
X: t.thirdPoint.X - t.firstPoint.X,
Y: t.thirdPoint.Y - t.firstPoint.Y,
Z: t.thirdPoint.Z - t.firstPoint.Z}
normalVectorToPlaneOfTriangle := geom.Cross(firstVector, secondVector)
if equalsForFloats(geom.Dot(normalVectorToPlaneOfTriangle, ray.Direction), 0) {
return false
}
freeCoefOFPlane := geom.Dot(normalVectorToPlaneOfTriangle, t.thirdPoint)
tCoef := (freeCoefOFPlane - geom.Dot(normalVectorToPlaneOfTriangle, t.firstPoint)) /
geom.Dot(normalVectorToPlaneOfTriangle, ray.Direction)
if tCoef < 0 {
return false
}
intersectionPoint := geom.Add(ray.Origin, geom.Mul(ray.Direction, tCoef))
if geom.Dot(geom.Cross(geom.Sub(t.firstPoint, t.secondPoint),
geom.Sub(t.firstPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 ||
geom.Dot(geom.Cross(geom.Sub(t.secondPoint, t.thirdPoint),
geom.Sub(t.secondPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 ||
geom.Dot(geom.Cross(geom.Sub(t.thirdPoint, t.firstPoint),
geom.Sub(t.thirdPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 {
return false
}
return true
}
type Quad struct {
firstTriangle Triangle
secondTriangle Triangle
}
func NewQuad(a, b, c, d geom.Vector) Quad {
var r Quad
r.firstTriangle.firstPoint = a
r.firstTriangle.secondPoint = b
r.firstTriangle.thirdPoint = d
r.secondTriangle.firstPoint = b
r.secondTriangle.secondPoint = c
r.secondTriangle.secondPoint = d
return r
}
func (q *Quad) Intersect(ray geom.Ray) bool {
if q.firstTriangle.Intersect(ray) && q.secondTriangle.Intersect(ray) {
return true
}
return false
}
type Sphere struct {
center geom.Vector
raduis float64
}
func NewSphere(origin geom.Vector, r float64) Sphere {
var s Sphere
s.center = origin
s.raduis = r
return s
}
func quadraticSolve(a, b, d float64) (t1, t2 float64) {
t1 = -b + math.Sqrt(d)/2*a
t2 = -b - math.Sqrt(d)/2*a
return
}
func (s *Sphere) Intersect(ray geom.Ray) bool {
point := geom.Sub(ray.Origin, s.center)
a := geom.Dot(ray.Direction, ray.Direction)
b := geom.Dot(ray.Direction, point) * 2
c := geom.Dot(point, point) - s.raduis*s.raduis
d := b*b - 4*a*c
if d < 0 {
return false
}
t1, t2 := quadraticSolve(a, b, d)
if t1 >= 0 || t2 >= 0 {
return true
}
return false
}
func main() {
a, b, c := geom.NewVector(-1, -1, 0), geom.NewVector(1, -1, 0), geom.NewVector(0, 1, 0)
tr := NewTriangle(a, b, c)
ray := geom.NewRay(geom.NewVector(0, 0, -1), geom.NewVector(0, 0, 1))
if tr.Intersect(ray) {
fmt.Println("ray intersects tr")
}
}

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

# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]
# _/tmp/d20181122-57-1noljpl [_/tmp/d20181122-57-1noljpl.test]
./solution_test.go:16:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:26:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:36:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:46:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:56:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:66:13: cannot use triangle (type Triangle) as type geom.Intersectable in argument to checkFigure:
	Triangle does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:77:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:88:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:99:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: cannot use quad (type Quad) as type geom.Intersectable in argument to checkFigure:
	Quad does not implement geom.Intersectable (Intersect method has pointer receiver)
./solution_test.go:110:13: too many errors
FAIL	_/tmp/d20181122-57-1noljpl [build failed]

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

Иво обнови решението на 21.11.2018 11:08 (преди 9 месеца)

+package main
+
+import (
+ "fmt"
+ "math"
+
+ "github.com/fmi/go-homework/geom"
+)
+
+func equalsForFloats(a, b float64) bool {
+
+ const epsilotn float64 = 1e-7
+
+ if math.Abs((a - b)) < a*epsilotn {
+
+ return true
+ }
+
+ return false
+}
+
+type Triangle struct {
+ firstPoint geom.Vector
+ secondPoint geom.Vector
+ thirdPoint geom.Vector
+}
+
+func NewTriangle(a, b, c geom.Vector) Triangle {
+
+ var t Triangle
+ t.firstPoint = a
+ t.secondPoint = b
+ t.thirdPoint = c
+
+ return t
+}
+
+func (t *Triangle) Intersect(ray geom.Ray) bool {
+
+ firstVector := geom.Vector{
+ X: t.secondPoint.X - t.firstPoint.X,
+ Y: t.secondPoint.Y - t.firstPoint.Y,
+ Z: t.secondPoint.Z - t.firstPoint.Z}
+
+ secondVector := geom.Vector{
+ X: t.thirdPoint.X - t.firstPoint.X,
+ Y: t.thirdPoint.Y - t.firstPoint.Y,
+ Z: t.thirdPoint.Z - t.firstPoint.Z}
+
+ normalVectorToPlaneOfTriangle := geom.Cross(firstVector, secondVector)
+
+ if equalsForFloats(geom.Dot(normalVectorToPlaneOfTriangle, ray.Direction), 0) {
+
+ return false
+ }
+
+ freeCoefOFPlane := geom.Dot(normalVectorToPlaneOfTriangle, t.thirdPoint)
+ tCoef := (freeCoefOFPlane - geom.Dot(normalVectorToPlaneOfTriangle, t.firstPoint)) /
+ geom.Dot(normalVectorToPlaneOfTriangle, ray.Direction)
+
+ if tCoef < 0 {
+
+ return false
+ }
+
+ intersectionPoint := geom.Add(ray.Origin, geom.Mul(ray.Direction, tCoef))
+
+ if geom.Dot(geom.Cross(geom.Sub(t.firstPoint, t.secondPoint),
+ geom.Sub(t.firstPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 ||
+ geom.Dot(geom.Cross(geom.Sub(t.secondPoint, t.thirdPoint),
+ geom.Sub(t.secondPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 ||
+ geom.Dot(geom.Cross(geom.Sub(t.thirdPoint, t.firstPoint),
+ geom.Sub(t.thirdPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 {
+
+ return false
+ }
+
+ return true
+}
+
+type Quad struct {
+ firstTriangle Triangle
+ secondTriangle Triangle
+}
+
+func NewQuad(a, b, c, d geom.Vector) Quad {
+
+ var r Quad
+ r.firstTriangle.firstPoint = a
+ r.firstTriangle.secondPoint = b
+ r.firstTriangle.thirdPoint = d
+ r.secondTriangle.firstPoint = b
+ r.secondTriangle.secondPoint = c
+ r.secondTriangle.secondPoint = d
+
+ return r
+}
+
+func (q *Quad) Intersect(ray geom.Ray) bool {
+
+ if q.firstTriangle.Intersect(ray) && q.secondTriangle.Intersect(ray) {
+
+ return true
+ }
+
+ return false
+}
+
+type Sphere struct {
+ center geom.Vector
+ raduis float64
+}
+
+func NewSphere(origin geom.Vector, r float64) Sphere {
+
+ var s Sphere
+ s.center = origin
+ s.raduis = r
+
+ return s
+}
+
+func quadraticSolve(a, b, d float64) (t1, t2 float64) {
+
+ t1 = -b + math.Sqrt(d)/2*a
+ t2 = -b - math.Sqrt(d)/2*a
+ return
+}
+
+func (s *Sphere) Intersect(ray geom.Ray) bool {
+
+ point := geom.Sub(ray.Origin, s.center)
+
+ a := geom.Dot(ray.Direction, ray.Direction)
+ b := geom.Dot(ray.Direction, point) * 2
+ c := geom.Dot(point, point) - s.raduis*s.raduis
+
+ d := b*b - 4*a*c
+
+ if d < 0 {
+
+ return false
+ }
+
+ t1, t2 := quadraticSolve(a, b, d)
+ if t1 >= 0 && t2 >= 0 {
+
+ return true
+ }
+
+ return false
+}
+
+func main() {
+ a, b, c := geom.NewVector(-1, -1, 0), geom.NewVector(1, -1, 0), geom.NewVector(0, 1, 0)
+ tr := NewTriangle(a, b, c)
+
+ ray := geom.NewRay(geom.NewVector(0, 0, -1), geom.NewVector(0, 0, 1))
+
+ if tr.Intersect(ray) {
+ fmt.Println("ray intersects tr")
+ }
+}

Иво обнови решението на 21.11.2018 11:12 (преди 9 месеца)

package main
import (
"fmt"
"math"
"github.com/fmi/go-homework/geom"
)
func equalsForFloats(a, b float64) bool {
const epsilotn float64 = 1e-7
if math.Abs((a - b)) < a*epsilotn {
return true
}
return false
}
type Triangle struct {
firstPoint geom.Vector
secondPoint geom.Vector
thirdPoint geom.Vector
}
func NewTriangle(a, b, c geom.Vector) Triangle {
var t Triangle
t.firstPoint = a
t.secondPoint = b
t.thirdPoint = c
return t
}
func (t *Triangle) Intersect(ray geom.Ray) bool {
firstVector := geom.Vector{
X: t.secondPoint.X - t.firstPoint.X,
Y: t.secondPoint.Y - t.firstPoint.Y,
Z: t.secondPoint.Z - t.firstPoint.Z}
secondVector := geom.Vector{
X: t.thirdPoint.X - t.firstPoint.X,
Y: t.thirdPoint.Y - t.firstPoint.Y,
Z: t.thirdPoint.Z - t.firstPoint.Z}
normalVectorToPlaneOfTriangle := geom.Cross(firstVector, secondVector)
if equalsForFloats(geom.Dot(normalVectorToPlaneOfTriangle, ray.Direction), 0) {
return false
}
freeCoefOFPlane := geom.Dot(normalVectorToPlaneOfTriangle, t.thirdPoint)
tCoef := (freeCoefOFPlane - geom.Dot(normalVectorToPlaneOfTriangle, t.firstPoint)) /
geom.Dot(normalVectorToPlaneOfTriangle, ray.Direction)
if tCoef < 0 {
return false
}
intersectionPoint := geom.Add(ray.Origin, geom.Mul(ray.Direction, tCoef))
if geom.Dot(geom.Cross(geom.Sub(t.firstPoint, t.secondPoint),
geom.Sub(t.firstPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 ||
geom.Dot(geom.Cross(geom.Sub(t.secondPoint, t.thirdPoint),
geom.Sub(t.secondPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 ||
geom.Dot(geom.Cross(geom.Sub(t.thirdPoint, t.firstPoint),
geom.Sub(t.thirdPoint, intersectionPoint)), normalVectorToPlaneOfTriangle) < 0 {
return false
}
return true
}
type Quad struct {
firstTriangle Triangle
secondTriangle Triangle
}
func NewQuad(a, b, c, d geom.Vector) Quad {
var r Quad
r.firstTriangle.firstPoint = a
r.firstTriangle.secondPoint = b
r.firstTriangle.thirdPoint = d
r.secondTriangle.firstPoint = b
r.secondTriangle.secondPoint = c
r.secondTriangle.secondPoint = d
return r
}
func (q *Quad) Intersect(ray geom.Ray) bool {
if q.firstTriangle.Intersect(ray) && q.secondTriangle.Intersect(ray) {
return true
}
return false
}
type Sphere struct {
center geom.Vector
raduis float64
}
func NewSphere(origin geom.Vector, r float64) Sphere {
var s Sphere
s.center = origin
s.raduis = r
return s
}
func quadraticSolve(a, b, d float64) (t1, t2 float64) {
t1 = -b + math.Sqrt(d)/2*a
t2 = -b - math.Sqrt(d)/2*a
return
}
func (s *Sphere) Intersect(ray geom.Ray) bool {
point := geom.Sub(ray.Origin, s.center)
a := geom.Dot(ray.Direction, ray.Direction)
b := geom.Dot(ray.Direction, point) * 2
c := geom.Dot(point, point) - s.raduis*s.raduis
d := b*b - 4*a*c
if d < 0 {
return false
}
t1, t2 := quadraticSolve(a, b, d)
- if t1 >= 0 && t2 >= 0 {
+ if t1 >= 0 || t2 >= 0 {
return true
}
return false
}
func main() {
a, b, c := geom.NewVector(-1, -1, 0), geom.NewVector(1, -1, 0), geom.NewVector(0, 1, 0)
tr := NewTriangle(a, b, c)
ray := geom.NewRay(geom.NewVector(0, 0, -1), geom.NewVector(0, 0, 1))
if tr.Intersect(ray) {
fmt.Println("ray intersects tr")
}
}

Решението ти изглежда прилично. Лошото е, че не си проверил дали фигурите ти имплементират интерфейса. Ако беше предал по - рано щях да съм го видял и да те предупредя.