コード例 #1
0
ファイル: Line.cs プロジェクト: huhen/agg-sharp
        /// <summary>
        /// Computes the point resulting from the intersection with another line
        /// </summary>
        /// <param name="otherLine">the other line to apply the intersection. The lines are supposed to intersect</param>
        /// <returns>point resulting from the intersection. If the point coundn't be obtained, return null</returns>
        public Vector3 ComputeLineIntersection(Line otherLine)
        {
            //x = x1 + a1*t = x2 + b1*s
            //y = y1 + a2*t = y2 + b2*s
            //z = z1 + a3*t = z2 + b3*s

            Vector3 linePoint     = otherLine.GetPoint();
            Vector3 lineDirection = otherLine.Direction;

            double t;

            if (Math.Abs(Direction.y * lineDirection.x - Direction.x * lineDirection.y) > EqualityTolerance)
            {
                t = (-startPoint.y * lineDirection.x + linePoint.y * lineDirection.x + lineDirection.y * startPoint.x - lineDirection.y * linePoint.x) / (Direction.y * lineDirection.x - Direction.x * lineDirection.y);
            }
            else if (Math.Abs(-Direction.x * lineDirection.z + Direction.z * lineDirection.x) > EqualityTolerance)
            {
                t = -(-lineDirection.z * startPoint.x + lineDirection.z * linePoint.x + lineDirection.x * startPoint.z - lineDirection.x * linePoint.z) / (-Direction.x * lineDirection.z + Direction.z * lineDirection.x);
            }
            else if (Math.Abs(-Direction.z * lineDirection.y + Direction.y * lineDirection.z) > EqualityTolerance)
            {
                t = (startPoint.z * lineDirection.y - linePoint.z * lineDirection.y - lineDirection.z * startPoint.y + lineDirection.z * linePoint.y) / (-Direction.z * lineDirection.y + Direction.y * lineDirection.z);
            }
            else
            {
#if DEBUG
                throw new InvalidOperationException();
#else
                return(Vector3.Zero);
#endif
            }

            double x = startPoint.x + Direction.x * t;
            double y = startPoint.y + Direction.y * t;
            double z = startPoint.z + Direction.z * t;

            return(new Vector3(x, y, z));
        }
コード例 #2
0
        /// <summary>
        /// Computes the point resulting from the intersection with another line
        /// </summary>
        /// <param name="otherLine">the other line to apply the intersection. The lines are supposed to intersect</param>
        /// <returns>point resulting from the intersection. If the point couldn't be obtained, return null</returns>
        public Vector3 ComputeLineIntersection(Line otherLine)
        {
            //x = x1 + a1*t = x2 + b1*s
            //y = y1 + a2*t = y2 + b2*s
            //z = z1 + a3*t = z2 + b3*s

            Vector3 linePoint     = otherLine.GetPoint();
            Vector3 lineDirection = otherLine.Direction;

            double t;

            if (Math.Abs(Direction.Y * lineDirection.X - Direction.X * lineDirection.Y) > EqualityTolerance)
            {
                t = (-startPoint.Y * lineDirection.X + linePoint.Y * lineDirection.X + lineDirection.Y * startPoint.X - lineDirection.Y * linePoint.X) / (Direction.Y * lineDirection.X - Direction.X * lineDirection.Y);
            }
            else if (Math.Abs(-Direction.X * lineDirection.Z + Direction.Z * lineDirection.X) > EqualityTolerance)
            {
                t = -(-lineDirection.Z * startPoint.X + lineDirection.Z * linePoint.X + lineDirection.X * startPoint.Z - lineDirection.X * linePoint.Z) / (-Direction.X * lineDirection.Z + Direction.Z * lineDirection.X);
            }
            else if (Math.Abs(-Direction.Z * lineDirection.Y + Direction.Y * lineDirection.Z) > EqualityTolerance)
            {
                t = (startPoint.Z * lineDirection.Y - linePoint.Z * lineDirection.Y - lineDirection.Z * startPoint.Y + lineDirection.Z * linePoint.Y) / (-Direction.Z * lineDirection.Y + Direction.Y * lineDirection.Z);
            }
            else
            {
#if DEBUG
                throw new InvalidOperationException();
#else
                return(Vector3.Zero);
#endif
            }

            double x = startPoint.X + Direction.X * t;
            double y = startPoint.Y + Direction.Y * t;
            double z = startPoint.Z + Direction.Z * t;

            return(new Vector3(x, y, z));
        }