Пример #1
0
 public bool Equals(myDouble v)
 {
     if ((object)v == null)
     {
         return(false);
     }
     return(Compare(v) == 0);
 }
Пример #2
0
        public double Angle(Vector v)
        {
            myDouble dot = Dot(v);
            myDouble div = new myDouble(dot / (Magnitude * v.Magnitude), 1);

            myDouble arcCos = Math.Acos(div);

            return(arcCos);
        }
Пример #3
0
        public Vector RotateYZ(double degrees)
        {
            myDouble radians = Math.PI * degrees / 180.0;

            myDouble sin = Math.Sin(radians);
            myDouble cos = Math.Cos(radians);

            return(new Vector(
                       dx,
                       (cos * dy) + (-sin * dz),
                       (sin * dy) + (cos * dz)
                       ));
        }
Пример #4
0
        public I3DObject RotateYZ(double degrees)
        {
            myDouble radians = (Math.PI * degrees) / 180.0;

            myDouble sin = Math.Sin(radians);
            myDouble cos = Math.Cos(radians);

            return(new Point(
                       x,
                       (cos * y) + (-1 * sin * z),
                       (sin * y) + (cos * z),
                       Color
                       ));
        }
Пример #5
0
        public int Compare(myDouble v)
        {
            if ((object)v == null)
            {
                throw new ArgumentNullException();
            }
            double delta = Value - v.Value;

            if (delta >= Epsilon)
            {
                return(1);
            }
            if (delta <= -Epsilon)
            {
                return(-1);
            }
            return(0);
        }
Пример #6
0
        // create a simular vector where the smallest nonzero companent is 1
        // if all components are zero, return that.
        public Vector Normalize()
        {
            if (dx == 0 && dy == 0 && dz == 0)
            {
                return(this);
            }
            myDouble ABSdx = ABS(dx);
            myDouble ABSdy = ABS(dy);
            myDouble ABSdz = ABS(dz);

            if (dx != 0 && (dy == 0 || ABSdx <= ABSdy) && (dz == 0 || ABSdx <= ABSdz))
            {
                return(new Vector(dx / ABSdx, dy / ABSdx, dz / ABSdx));
            }
            else if (dy != 0 && (dx == 0 || ABSdy <= ABSdx) && (dz == 0 || ABSdy <= ABSdz))
            {
                return(new Vector(dx / ABSdy, dy / ABSdy, dz / ABSdy));
            }
            return(new Vector(dx / ABSdz, dy / ABSdz, dz / ABSdz));
        }
Пример #7
0
        public Point Intersect(Line l)
        {
            Point Pt = null;
            Point Pu = null;

            Vector crossVectors = Direction.Cross(l.Direction);
            Vector crossT       = (P1 - l.P1).Cross(l.Direction);
            Vector crossU       = (l.P1 - P1).Cross(Direction);

            //double dotVectors = Direction.Dot(l.Direction);

            if (crossVectors.Magnitude == 0 && crossU.Magnitude == 0)
            {
                //colinear
                System.Diagnostics.Trace.WriteLine("colinear check failed, so lines are colinear with no single point of intersection intersection.");
                return(null);
            }
            else if (crossVectors.Magnitude == 0 && crossU.Magnitude != 0)
            {
                // parallel
                System.Diagnostics.Trace.WriteLine("parallel check failed, so lines are parallel with no intersection.");
                return(null);
            }
            //else if (dotVectors != 0)
            //{
            myDouble t = crossT.Magnitude / crossVectors.Magnitude;
            myDouble u = crossU.Magnitude / crossVectors.Magnitude;

            Pt = P1 + Direction.Scale(t);
            Pu = l.P1 + l.Direction.Scale(u);

            if (Pt != Pu)
            {
                System.Diagnostics.Trace.WriteLine("(Pt!=Pu) check failed, no intersection.");
                return(null);
            }
            // If the point Pu is on this line, the vector from the starting point of l1 (this) to
            // Pt will point in the same direction as l1 (this). (the two vectors will have an agle of 0)
            myDouble angleU = (Pu - P1).Angle(Direction);

            if (angleU != 0)
            {
                System.Diagnostics.Trace.WriteLine("(angleU != 0) check failed, no intersection.");
                return(null);
            }
            // If the point Pt is on the second line (l), the vector from the starting point of l to
            // Pt will point in the same direction as l. (the two vectors will have an agle of 0)
            myDouble angleT = (Pt - l.P1).Angle(l.Direction);

            if (angleT != 0)
            {
                System.Diagnostics.Trace.WriteLine("(angleT != 0) check failed, no intersection.");
                return(null);
            }

            //double dotVectors = Direction.Dot(l.Direction);
            //double dotT = (P1 - l.P1).Dot(l.Direction);
            //double dotU = (l.P1 - P1).Dot(Direction);

            //double t = crossT.Magnitude / crossVectors.Magnitude;
            //if (dotU < 0 != dotVectors < 0)
            //    t *= -1;
            //double u = crossU.Magnitude / crossVectors.Magnitude;
            //if (dotT < 0 != dotVectors < 0)
            //    u *= -1;

            //double t = 0;
            //if (Math.Abs(crossVectors.dx) > double.Epsilon) t = crossT.dx / crossVectors.dx;
            //else if (Math.Abs(crossVectors.dy) > double.Epsilon) t = crossT.dy / crossVectors.dy;
            //else if (Math.Abs(crossVectors.dz) > double.Epsilon) t = crossT.dz / crossVectors.dz;
            //double u = 0;
            //if (Math.Abs(crossVectors.dx) > double.Epsilon) u = crossU.dx / crossVectors.dx;
            //else if (Math.Abs(crossVectors.dy) > double.Epsilon) u = crossU.dy / crossVectors.dy;
            //else if (Math.Abs(crossVectors.dz) > double.Epsilon) u = crossU.dz / crossVectors.dz;

            // we now know that the lines will intersect if the extend to
            //  infinity in both directions, so test to see if the point
            //  between the end points of both lines
            if (t >= 0 && t <= 1 && u >= 0 && u <= 1)
            {
#if __DoubleCheck == false
                //unfortunatly, t is always positive, so we don't detect if the computed
                // intersection is before P1 on the xtended line, so make sie Pi is in the
                // bounding box of the line
                //if (!(Pi <= Max && Pi >= Min) || !(Pi <= l.Max && Pi >= l.Min))
                //{
                //    return null;
                //}
                return(Pt);
#endif
            }
            else
            {
                return(null);
            }
            //}
#if __DoubleCheck
            Line  l1  = this.Reverse();
            Line  l2  = l.Reverse();
            Point Pir = null;

            Vector crossVectorsr = l1.Direction.Cross(l2.Direction);
            Vector crossTr       = (l1.P1 - l2.P1).Cross(l2.Direction);
            Vector crossUr       = (l2.P1 - l1.P1).Cross(l1.Direction);

            if (crossVectors.Magnitude == 0 && crossU.Magnitude == 0)
            {
                //colinear
                return(Pir);
            }
            else if (crossVectors.Magnitude == 0 && crossU.Magnitude != 0)
            {
                // parallel
                return(Pir);
            }
            else if (crossVectors.Magnitude != 0)
            {
                double t = crossTr.Magnitude / crossVectorsr.Magnitude;
                double u = crossUr.Magnitude / crossVectorsr.Magnitude;
                if (t >= 0 && t <= 1 && u >= 0 && u <= 1)
                {
                    Pir = l1.P1 + l1.Direction.Scale(t);

                    //unfortunatly, t is always positive, so we don't detect if the computed
                    // intersection is before P1 on the xtended line, so make sie Pi is in the
                    // bounding box of the line
                    //if (!(Pir <= l1.Max && Pir >= l1.Min) || !(Pir <= l2.Max && Pir >= l2.Min))
                    //{
                    //    return null;
                    //}
                }
                else
                {
                    return(null);
                }
            }
#endif
        }