/// <summary> /// Obtains barycentric coordinate relative to this triangle. /// </summary> /// <param name="input">The vector that lies in triangle plane.</param> /// <returns></returns> public Vector2f GetBarycentric(Vector2f input) { // Automatic Z-projection for 2D. return(LinearSolver.SolveSystem(B.X - A.X, C.X - A.X, B.Y - A.Y, C.Y - A.Y, input.X - A.X, input.Y - A.Y)); }
/// <summary> /// Intersection test. /// </summary> /// <remarks>Test does not handle parallel lines.</remarks> /// <param name="one">The first line segment.</param> /// <param name="other">The second line segment.</param> /// <param name="t1">The interpolation for first line segment, only if intersects is true.</param> /// <param name="t2">The interpolation for second line segment, only if intersects is true.</param> /// <returns>Do lines intersect.</returns> public static bool Intersect(LineSegment2d one, LineSegment2d other, out double t1, out double t2) { t1 = t2 = 0.0; // We solve 2x2 system and then check if it is ok for the third argument. Vector2d r = LinearSolver.SolveSystem( new Matrix2x2d(one.Direction.X, -other.Direction.X, one.Direction.Y, -other.Direction.Y), new Vector2d(other.A.X - one.A.X, other.A.Y - one.A.Y)); // If system has the solution, it must be in range [0,1]. if (r.X < 0.0 || r.X > 0.0) { return(false); } if (r.Y < 0.0 || r.Y > 0.0) { return(false); } // We check if the last line satisfies. if (!Vector2d.NearEqual(one.Sample(r.X), other.Sample(r.Y))) { return(false); } // We copy interpolation. t1 = r.X; t2 = r.Y; return(true); }
/// <summary> /// Obtains barycentric coordinate relative to this triangle. /// </summary> /// <param name="input">The vector that lies in triangle plane.</param> /// <returns></returns> public Vector2f GetBarycentric(Vector3f input) { //#ifdef 3D // We need positive components of normal, only magnitude relavant. Vector3f normal = this.Normal; normal = Vector3f.ComponentMultiply(normal, normal); if (normal.X > normal.Y) { if (normal.X > normal.Y) { // We project to x axis, all xs are the same. return(LinearSolver.SolveSystem(B.Y - A.Y, C.Y - A.Y, B.Z - A.Z, C.Z - A.Z, input.Y - A.Y, input.Z - A.Z)); } else { // We project to z axis, all zs are the same. return(LinearSolver.SolveSystem(B.X - A.X, C.X - A.X, B.Y - A.Y, C.Y - A.Y, input.X - A.X, input.Y - A.Y)); } } else { if (normal.Y > normal.Z) { // We project to y axis, all ys are the same. return(LinearSolver.SolveSystem(B.X - A.X, C.X - A.X, B.Z - A.Z, C.Z - A.Z, input.X - A.X, input.Z - A.Z)); } else { // We project to z axis, all zs are the same. return(LinearSolver.SolveSystem(B.X - A.X, C.X - A.X, B.Y - A.Y, C.Y - A.X, input.X - A.X, input.Y - A.Y)); } } //#endif }