Exemple #1
0
 public Line3(ILine3 lineIn)
 {
     Point1 = new Vector3(lineIn.Point1);
     Point2 = new Vector3(lineIn.Point2);
     Col    = lineIn.Col;
     Init();
 }
Exemple #2
0
        public IIntersection Intersects(ILine3 ray2)
        {
            try
            {
                var result = new Intersection3();
                var ptOut  = new Vector2();

                if (SlopeXY == ray2.SlopeXY)
                {
                    result.Intersects = false;
                }
                else
                {
                    double denom = (Point1.X - Point2.X) * (ray2.Point1.Y - ray2.Point2.Y)
                                   - (ray2.Point1.X - ray2.Point2.X) * (Point1.Y - Point2.Y);
                    double xOut     = 0;
                    double yOut     = 0;
                    double line1Det = Point1.X * Point2.Y - Point2.X * Point1.Y;
                    double line2Det = ray2.Point1.X * ray2.Point2.Y - ray2.Point2.X * ray2.Point1.Y;
                    xOut   = (line1Det * (ray2.Point1.X - ray2.Point2.X) - line2Det * (Point1.X - Point2.X)) / denom;
                    yOut   = (line1Det * (ray2.Point1.Y - ray2.Point2.Y) - line2Det * (Point1.Y - Point2.Y)) / denom;
                    result = new Intersection3(new Vector3(xOut, yOut, 0), true);
                }
                return(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemple #3
0
        public void Line_translate_returnsVal()
        {
            ILine3 line   = new Line3(1, 1, 1, 2, 2, 2);
            ILine3 ltrans = line.Translate(new Vector3(1, 1, 1));

            Assert.AreEqual(2, ltrans.Point1.X, "x1");
            Assert.AreEqual(2, ltrans.Point1.Y, "y1");
            Assert.AreEqual(2, ltrans.Point1.Z, "z1");
        }
Exemple #4
0
        public void Line_rotateZ_returnsVal()
        {
            ILine3  line = new Line3(1, 1, 0, 2, 2, 0);
            Vector3 pc   = new Vector3(0, 0, 0);

            ILine3 lrot = line.RotateZ(pc, Math.PI);

            Assert.AreEqual(line.Length, lrot.Length, "len");
            Assert.AreEqual(-1d, Math.Round(lrot.Point1.X, 8), "x1");
            Assert.AreEqual(-1d, Math.Round(lrot.Point1.Y, 8), "y1");
            Assert.AreEqual(0d, Math.Round(lrot.Point1.Z, 8), "z1");
        }
Exemple #5
0
        public IIntersection Intersects(ILine3 ray2)
        {
            try
            {
                IIntersection result = new Intersection3();
                Vector3       Point1;
                Intersection3 ir = new Intersection3();
                //transform arc and line to center arc at origin
                IVector3 trans = new Vector3(-1 * Center.X, -1 * Center.Y, 0);
                IArc     arcT  = Translate(trans);
                ILine3   lineT = ray2.Translate(trans);
                //parameterize line
                double dy = lineT.Point2.Y - lineT.Point1.Y;
                double dx = lineT.Point2.X - lineT.Point1.X;

                //calc quadratic solution
                double a       = Math.Pow(dx, 2) + Math.Pow(dy, 2);
                double b       = 2 * (dx) * (lineT.Point1.X - arcT.Center.X) + 2 * (dy) * (lineT.Point1.Y - arcT.Center.Y);
                double c       = Math.Pow(lineT.Point1.X - arcT.Center.X, 2) + Math.Pow(lineT.Point1.Y - arcT.Center.Y, 2) - Math.Pow(arcT.Radius, 2);
                double discrim = Math.Pow(b, 2) - 4 * a * c;

                if (discrim >= 0)//check solution is real
                {
                    double tPos   = (-b + Math.Sqrt(discrim)) / (2 * a);
                    double tNeg   = (-b - Math.Sqrt(discrim)) / (2 * a);
                    double xpos   = dx * tPos + lineT.Point1.X;
                    double ypos   = dy * tPos + lineT.Point1.Y;
                    double xneg   = dx * tNeg + lineT.Point1.X;
                    double yneg   = dy * tNeg + lineT.Point1.Y;
                    double angneg = Math.Atan2(yneg, xneg);
                    double angpos = Math.Atan2(ypos, xpos);

                    if (angneg < 0)
                    {
                        angneg += 2 * Math.PI;
                    }
                    if (angpos < 0)
                    {
                        angpos += 2 * Math.PI;
                    }

                    //transform point back before returning
                    if ((angneg >= arcT.StartAngleRad) && (angneg <= arcT.EndAngleRad))
                    {
                        Point1 = new Vector3(xneg, yneg, 0);
                        var pt = Point1.Translate(new Vector3(Center.X, Center.Y, 0.0));
                        result            = new Intersection3(pt, true);
                        result.Intersects = true;
                    }
                    if ((angpos >= arcT.StartAngleRad) && (angpos <= arcT.EndAngleRad))
                    {
                        Point1 = new Vector3(xpos, ypos, 0);
                        var pt = Point1.Translate(new Vector3(Center.X, Center.Y, 0.0));
                        result            = new Intersection3(pt, true);
                        result.Intersects = true;
                    }
                }
                return(result);
            }
            catch (Exception)
            {
                throw;
            }
        }