// Copyright (C) 2002-2006 Nikolaus Gebhardt // This file is part of the "Irrlicht Engine". // For conditions of distribution and use, see copyright notice in irrlicht.h #pragma once #using using namespace System; using namespace System::Runtime::InteropServices; #include "Vector3d.h" namespace Irrlicht { namespace Core { /// /// A 3D line from an start point to an end point /// public __value class Line3D { public: // Constructors Line3D(): start(0,0,0), end(1,1,1) {}; Line3D(float xa, float ya, float za, float xb, float yb, float zb) : start(xa, ya, za), end(xb, yb, zb) {}; Line3D(Vector3D start, Vector3D end) : start(start), end(end) {}; void Set(float xa, float ya, float za, float xb, float yb, float& zb){start.Set(xa, ya, za); end.Set(xb, yb, zb);} void Set(Vector3D nstart, Vector3D nend) {start.Set(nstart); end.Set(nend);} void Set(Line3D line){start.Set(line.start); end.Set(line.end);} /// /// Returns length of line /// Returns length of line. double GetLength() { return start.GetDistanceFrom(end); }; /// /// Returns sqared length of line /// /// Returns sqared length of line. double GetLengthSQ() { return start.GetDistanceFromSQ(end); }; /// /// Returns middle of line /// Vector3D GetMiddle() { return (start + end) * (float)0.5; } /// /// Returns vector of line /// Vector3D GetVector() { return end - start; } /// /// Returns if the overgiven point is between start and end of the /// line. Assumes that the point is already somewhere on the line. /// bool IsPointBetweenStartAndEnd(Vector3D point) { return point.IsBetweenPoints(start, end); } /// Returns the closest point on this line to a point Vector3D GetClosestPoint(Vector3D point) { Vector3D c = point - start; Vector3D v = end - start; float d = (float)v.GetLength(); v = v / d; float t = v.DotProduct(c); if (t < (float)0.0) return start; if (t > d) return end; v = v * t; return start + v; } /// /// Returns if the line intersects with a shpere /// /// Origin of the shpere. /// Radius if the sphere. /// Returns true if there is an intersection. /// If there is one, the distance to the first intersection point /// is stored in outdistance. bool GetIntersectionWithSphere(Vector3D sorigin, float sradius, [PARAMFLAG::Out] double& outdistance) { Vector3D q = sorigin - start; double c = q.GetLength(); Vector3D vv = GetVector(); vv.Normalize(); double v = q.DotProduct(vv); double d = sradius * sradius - (c*c - v*v); if (d < 0.0) return false; outdistance = v - System::Math::Sqrt(d); return true; } // member variables Vector3D start; Vector3D end; }; } }