plane.monkey2 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. Namespace std.geom
  2. Alias Planef:Plane<Float>
  3. Struct Plane<T>
  4. Field n:Vec3<T>
  5. Field d:T
  6. Method New()
  7. End
  8. Method New( nx:Float,ny:Float,nz:Float,d:Float )
  9. n.x=nx;n.y=ny;n.z=nz;Self.d=d
  10. End
  11. Method New( n:Vec3<T>,d:T )
  12. Self.n=n
  13. Self.d=d
  14. End
  15. Method New( p:Vec3<T>,n:Vec3<T> )
  16. Self.n=n
  17. Self.d=-n.Dot( p )
  18. End
  19. Method New( v0:Vec3<T>,v1:Vec3<T>,v2:Vec3<T> )
  20. n=(v1-v0).Cross(v2-v0).Normalize()
  21. d=-n.Dot( v0 )
  22. End
  23. Operator-:Plane()
  24. Return New Plane( -n,-d )
  25. End
  26. Method Distance:Double( p:Vec3<T> )
  27. Return n.Dot( p )+d
  28. End
  29. Method Nearest:Vec3<T>( p:Vec3<T> )
  30. Return p-n*Distance( p )
  31. End
  32. Method Normalize:Plane()
  33. Local l:=n.Length
  34. Return New Planef( n/l,d/l )
  35. End
  36. Method TIntersect:Double( line:Line<T> )
  37. Return -Distance( line.o )/n.Dot( line.d )
  38. End
  39. Method Intersect:Vec3<T>( line:Line<T> )
  40. Return line * TIntersect( line )
  41. End
  42. Method Intersect:Line<T>( p:Plane<T> )
  43. Local v:=n.Cross( p.n ).Normalize()
  44. Local o:=p.Intersect( New Line<T>( n*-d,n.Cross( v ) ) )
  45. Return New Line<T>( o,v )
  46. End
  47. Operator To:String()
  48. Return "Plane("+n+","+d+")"
  49. End
  50. End