コード例 #1
0
ファイル: LineSegment.cs プロジェクト: ase-lab/Skyhunter
        public bool Equals(LineSegment l)
        {
            if (this.isEqual(l))
                if (lowerBound.Equals(l.lowerBound) && upperBound.Equals(l.upperBound))
                    return true;

            return false;
        }
コード例 #2
0
        public Bitmap2D scaleBitmap2DToRestrictedLineSegment(Bitmap2D bitmap2DToScale, LineSegment lineSegment, Point one, Point two)
        {
            //Given a line segment, that has already been restricted to the region rectangle given the 2 points one, and two
            //scale / padd the image given the fact that the original line might have partially been out of the bounds of the rectangle.

            return null;
        }
コード例 #3
0
        public void getArrayIndiciesMarkedByLineTest()
        {
            //Horizontal line

            LineSegment line = new LineSegment(new Point(.5, .5), new Point(5.5, .5));
            long arrayspacesizex = 10;
            long arrayspacesizey = 10;
            List<Point> expected = new List<Point>();
            expected.Add(new Point(0, 0));
            expected.Add(new Point(1, 0));
            expected.Add(new Point(2, 0));
            expected.Add(new Point(3, 0));
            expected.Add(new Point(4, 0));
            expected.Add(new Point(5, 0));

            List<Point> actual;
            actual = ArraySpaceUtilities.getArrayIndiciesMarkedByLine(line, arrayspacesizex, arrayspacesizey);
            Assert.AreEqual(expected.Count, actual.Count);

            Point[] expectedArray = expected.ToArray();
            Point[] actualArray = actual.ToArray();

            for (int i = 0; i < expectedArray.Length; i++)
            {
                Assert.AreEqual(true, actualArray[i].Equals(expectedArray[i]));
            }

            //Vertical line
            line = new LineSegment(new Point(2.5, 1.5), new Point(2.5, 4.5));
            expected.Clear();
            expected.Add(new Point(2, 1));
            expected.Add(new Point(2, 2));
            expected.Add(new Point(2, 3));
            expected.Add(new Point(2, 4));
            expected.Reverse();
            actual.Clear();
            actual = ArraySpaceUtilities.getArrayIndiciesMarkedByLine(line, arrayspacesizex, arrayspacesizey);
            Assert.AreEqual(expected.Count, actual.Count);
            expectedArray = expected.ToArray();
            actualArray = actual.ToArray();

            for (int i = 0; i < expectedArray.Length; i++)
            {
                Assert.AreEqual(true, actualArray[i].Equals(expectedArray[i]));
            }

            //diagonal line
            line = new LineSegment(new Point(0.5, 0.5), new Point(5.5, 2.5));
            expected.Clear();
            expected.Add(new Point(0, 0));
            expected.Add(new Point(1, 0));
            expected.Add(new Point(1, 1));
            expected.Add(new Point(2, 1));
            expected.Add(new Point(3, 1));
            expected.Add(new Point(4, 1));
            expected.Add(new Point(4, 2));
            expected.Add(new Point(5, 2));

            actual.Clear();
            actual = ArraySpaceUtilities.getArrayIndiciesMarkedByLine(line, arrayspacesizex, arrayspacesizey);
            Assert.AreEqual(expected.Count, actual.Count);
            expectedArray = expected.ToArray();
            actualArray = actual.ToArray();

            for (int i = 0; i < expectedArray.Length; i++)
            {
                Assert.AreEqual(true, actualArray[i].Equals(expectedArray[i]));
            }
        }
コード例 #4
0
        public void restrictLineToArraySpaceTest()
        {
            Point one = new Point(1, 1);
            Point two = new Point(4, 4); ;
            long arrayspacesizex = 10;
            long arrayspacesizey = 10;
            LineSegment expected = new LineSegment(one, two);
            LineSegment actual;
            actual = ArraySpaceUtilities.restrictLineToArraySpace(one, two, arrayspacesizex, arrayspacesizey);
            Assert.AreEqual(expected.lowerBound.x, actual.lowerBound.x);
            Assert.AreEqual(expected.lowerBound.y, actual.lowerBound.y);
            Assert.AreEqual(expected.upperBound.x, actual.upperBound.x);
            Assert.AreEqual(expected.upperBound.y, actual.upperBound.y);

            one = new Point(4, 5);
            two = new Point(9, 9);
            expected = new LineSegment(one, two);
            actual = ArraySpaceUtilities.restrictLineToArraySpace(one, two, arrayspacesizex, arrayspacesizey);
            Assert.AreEqual(expected.lowerBound.x, actual.lowerBound.x);
            Assert.AreEqual(expected.lowerBound.y, actual.lowerBound.y);
            Assert.AreEqual(expected.upperBound.x, actual.upperBound.x);
            Assert.AreEqual(expected.upperBound.y, actual.upperBound.y);

            one = new Point(-2, 5);
            two = new Point(5, 5);
            expected = new LineSegment(new Point(0, 5), new Point(5, 5));
            actual = ArraySpaceUtilities.restrictLineToArraySpace(one, two, arrayspacesizex, arrayspacesizey);
            Assert.AreEqual(expected.lowerBound.x, actual.lowerBound.x);
            Assert.AreEqual(expected.lowerBound.y, actual.lowerBound.y);
            Assert.AreEqual(expected.upperBound.x, actual.upperBound.x);
            Assert.AreEqual(expected.upperBound.y, actual.upperBound.y);

            one = new Point(15, 5);
            two = new Point(5, 5);
            expected = new LineSegment(new Point(10, 5), new Point(5, 5));
            actual = ArraySpaceUtilities.restrictLineToArraySpace(one, two, arrayspacesizex, arrayspacesizey);
            Assert.AreEqual(expected.lowerBound.x, actual.lowerBound.x);
            Assert.AreEqual(expected.lowerBound.y, actual.lowerBound.y);
            Assert.AreEqual(expected.upperBound.x, actual.upperBound.x);
            Assert.AreEqual(expected.upperBound.y, actual.upperBound.y);
        }
コード例 #5
0
ファイル: MainWindow.xaml.cs プロジェクト: ase-lab/Skyhunter
        public void createPixelGrid(byte[] rawData, int sizex, int sizey, SeismicUtilities.Point offsetpoint, int pixelPaddingX, int pixelPaddingY, double rotation)
        {
            long memoryoffset = 0;

            //Turn all pixels white
            while (memoryoffset < sizex * sizey * pixelFormat.BitsPerPixel / 8)
            {
                //deal with one pixel at a time
                rawData[memoryoffset++] = (byte)255;
                rawData[memoryoffset++] = (byte)255;
                rawData[memoryoffset++] = (byte)255;
                rawData[memoryoffset++] = (byte)255;
            }

            //Draw horizontal border lines
            int x = 0, y = 0;
            for (int i = 0; i < sizex; i++)
            {
                x = i;
                y = 0;
                memoryoffset = (y * sizex + x) * pixelFormat.BitsPerPixel / 8;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)255;

                y = sizey - 1;
                memoryoffset = (y * sizex + x) * pixelFormat.BitsPerPixel / 8;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)255;
            }
            //Draw vertical border lines
            for (int i = 0; i < sizey; i++)
            {
                x = 0;
                y = i;
                memoryoffset = (y * sizex + x) * pixelFormat.BitsPerPixel / 8;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)255;

                x = sizex - 1;
                memoryoffset = (y * sizex + x) * pixelFormat.BitsPerPixel / 8;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)0;
                rawData[memoryoffset++] = (byte)255;
            }

            SeismicUtilities.Point bottomLeft = offsetpoint;
            SeismicUtilities.Point bottomRight = new SeismicUtilities.Point(offsetpoint.x + arraySpaceImageWidth - 2 * imagePixelPaddingX, offsetpoint.y);
            SeismicUtilities.Point topLeft = new SeismicUtilities.Point(offsetpoint.x, offsetpoint.y + arraySpaceImageHeight - 2 * imagePixelPaddingY);
            SeismicUtilities.Point topRight = new SeismicUtilities.Point(offsetpoint.x + arraySpaceImageWidth - 2 * imagePixelPaddingX, offsetpoint.y + arraySpaceImageHeight - 2 * imagePixelPaddingY);

            SeismicUtilities.Point rotatedTopLeft = topLeft.rotatePointAboutPoint(rotation, offsetpoint);
            SeismicUtilities.Point rotatedTopRight = topRight.rotatePointAboutPoint(rotation, offsetpoint);
            SeismicUtilities.Point rotatedBottomRight = bottomRight.rotatePointAboutPoint(rotation, offsetpoint);

            //Left rectangle line
            SeismicUtilities.LineSegment line = new SeismicUtilities.LineSegment(new SeismicUtilities.Point(rotatedTopLeft.x, arraySpaceImageHeight - rotatedTopLeft.y), new SeismicUtilities.Point(offsetpoint.x, arraySpaceImageHeight - offsetpoint.y));
            List<SeismicUtilities.Point> points = ArraySpaceUtilities.getArrayIndiciesMarkedByLine(line, sizex, sizey);
            drawPoints(points, sizex, sizey, rawData);

            //Bottom rect line

            line = new SeismicUtilities.LineSegment(new SeismicUtilities.Point(offsetpoint.x, arraySpaceImageHeight - offsetpoint.y), new SeismicUtilities.Point(rotatedBottomRight.x, arraySpaceImageHeight - rotatedBottomRight.y));
            points = ArraySpaceUtilities.getArrayIndiciesMarkedByLine(line, sizex, sizey);
            drawPoints(points, sizex, sizey, rawData);

            //Right rect line
            line = new SeismicUtilities.LineSegment(new SeismicUtilities.Point(rotatedBottomRight.x, arraySpaceImageHeight - rotatedBottomRight.y), new SeismicUtilities.Point(rotatedTopRight.x, arraySpaceImageHeight - rotatedTopRight.y));
            points = ArraySpaceUtilities.getArrayIndiciesMarkedByLine(line, sizex, sizey);
            drawPoints(points, sizex, sizey, rawData);

            //top rect line
            line = new SeismicUtilities.LineSegment(new SeismicUtilities.Point(rotatedTopRight.x, arraySpaceImageHeight - rotatedTopRight.y), new SeismicUtilities.Point(rotatedTopLeft.x, arraySpaceImageHeight - rotatedTopLeft.y));
            points = ArraySpaceUtilities.getArrayIndiciesMarkedByLine(line, sizex, sizey);
            drawPoints(points, sizex, sizey, rawData);

            ////Draw horizontal array space lines
            //for (int i = 0; i < sizex - 2 * pixelPaddingX; i++)
            // {
            //     x = i;
            //     y = pixelPaddingY;
            //     memoryoffset = (y * sizex + x + pixelPaddingX) * pixelFormat.BitsPerPixel / 8;
            //     rawData[memoryoffset++] = (byte)0;
            //     rawData[memoryoffset++] = (byte)0;
            //     rawData[memoryoffset++] = (byte)0;
            //     rawData[memoryoffset++] = (byte)255;

            //     y = sizey - 1 - pixelPaddingY;
            //     memoryoffset = (y * sizex + x + pixelPaddingX) * pixelFormat.BitsPerPixel / 8;
            //     rawData[memoryoffset++] = (byte)0;
            //     rawData[memoryoffset++] = (byte)0;
            //     rawData[memoryoffset++] = (byte)0;
            //     rawData[memoryoffset++] = (byte)255;
            // }

            //Draw vertical array space lines
            //for (int i = 0; i < sizey - 2 * pixelPaddingY; i++)
            //{
            //    x = pixelPaddingX;
            //    y = i + pixelPaddingY;
            //    memoryoffset = (y * sizex + x) * pixelFormat.BitsPerPixel / 8;
            //    rawData[memoryoffset++] = (byte)0;
            //    rawData[memoryoffset++] = (byte)0;
            //    rawData[memoryoffset++] = (byte)0;
            //    rawData[memoryoffset++] = (byte)255;

            //    x = sizex - 1 - pixelPaddingX;

            //    memoryoffset = (y * sizex + x) * pixelFormat.BitsPerPixel / 8;
            //    rawData[memoryoffset++] = (byte)0;
            //    rawData[memoryoffset++] = (byte)0;
            //    rawData[memoryoffset++] = (byte)0;
            //    rawData[memoryoffset++] = (byte)255;
            //}

            #region
            //for (int i = 1; i < numxregions; i++)
            //{
            //    for (int j = 0; j < sizey; j++)
            //    {
            //        x = i * sizex / numxregions;
            //        y = j;
            //        memoryoffset = (y * sizex + x) * pixelFormat.BitsPerPixel / 8;

            //        //if (i == 1)
            //        //{
            //        //    rawData[memoryoffset++] = (byte)0;
            //        //    rawData[memoryoffset++] = (byte)255;
            //        //    rawData[memoryoffset++] = (byte)0;
            //        //    rawData[memoryoffset++] = (byte)255;
            //        //}
            //        //else
            //        //{
            //            rawData[memoryoffset++] = (byte)0;
            //            rawData[memoryoffset++] = (byte)0;
            //            rawData[memoryoffset++] = (byte)0;
            //            rawData[memoryoffset++] = (byte)255;
            //        //}

            //    }
            //}

            //for (int i = 1; i < numyregions; i++)
            //{
            //    for (int j = 0; j < sizex; j++)
            //    {
            //        x = j;
            //        y = i * sizey / numyregions;
            //        memoryoffset = (y * sizex + x )* pixelFormat.BitsPerPixel / 8;

            //        //if (i == 1)
            //        //{
            //        //    rawData[memoryoffset++] = (byte)0;
            //        //    rawData[memoryoffset++] = (byte)255;
            //        //    rawData[memoryoffset++] = (byte)0;
            //        //    rawData[memoryoffset++] = (byte)255;
            //        //}
            //        //else
            //        //{
            //            rawData[memoryoffset++] = (byte)0;
            //            rawData[memoryoffset++] = (byte)0;
            //            rawData[memoryoffset++] = (byte)0;
            //            rawData[memoryoffset++] = (byte)255;
            //        //}
            //    }
            //}
            #endregion
        }
コード例 #6
0
        public static List<Point> getArrayIndiciesMarkedByLine(LineSegment line, long arrayspacesizex, long arrayspacesizey)
        {
            //columnList = new List<PixelColumn>();
            List<Point> coordinateList = new List<Point>();

            double regionone, regiontwo, regionmax, regionmin;

            regionone = line.lowerBound.x;
            regiontwo = (int)regionone + 1;

            //Console.Out.WriteLine("Starting Regions: " + regionone + ", " + regiontwo);
            bool positiveSlope = false;
            if (line.slope > 0)
            {
                positiveSlope = true;
            }
            else
            {
                positiveSlope = false;
            }

            if ((int)regionone == (int)regiontwo)
            {
                //Near verticle line

                regionmax = line.getMaxOnRegion(regionone, regiontwo);
                regionmin = line.getMinOnRegion(regionone, regiontwo);
                if (regionmax == Double.PositiveInfinity || (int)regionmax == arrayspacesizey)
                {
                    regionmax = arrayspacesizey - 1;
                }

                if ((int)regionone == arrayspacesizex)
                {
                    regionone = arrayspacesizex - 1;
                }

                addAllVerticalPointsToList(ref coordinateList, (int)regionmin, (int)regionmax, (int)regionone, positiveSlope);
            }
            else
            {
                while (regiontwo < line.upperBound.x)
                {
                    //Using this region, add all points to the list
                    regionmax = line.getMaxOnRegion(regionone, regiontwo);
                    regionmin = line.getMinOnRegion(regionone, regiontwo);

                    if (regionmax == Double.PositiveInfinity || (int)regionmax == arrayspacesizey)
                    {
                        regionmax = arrayspacesizey - 1;
                    }

                    //Console.Out.WriteLine("region: " + regionone + ", " + regiontwo + " Max: " + regionmax + ", Min: " + regionmin);

                    //addAllVerticalColumnsToList(ref columnList, (int)regionmin, (int)regionmax, (int)regionone);
                    addAllVerticalPointsToList(ref coordinateList, (int)regionmin, (int)regionmax, (int)regionone, positiveSlope);
                    regionone = regiontwo;
                    regiontwo++;
                }

                //Add last region manually

                regiontwo = line.upperBound.x;
                regionmax = line.getMaxOnRegion(regionone, regiontwo);
                regionmin = line.getMinOnRegion(regionone, regiontwo);

                if (regionmax == Double.PositiveInfinity || (int)regionmax == arrayspacesizey)
                {
                    regionmax = arrayspacesizey - 1;
                }

                if ((int)regionone == arrayspacesizex)
                {
                    regionone = arrayspacesizex - 1;
                }
                //Console.Out.WriteLine("last region: " + regionone + ", " + regiontwo + " Max: " + regionmax + ", Min: " + regionmin);
                //addAllVerticalColumnsToList(ref columnList, (int)regionmin, (int)regionmax, (int)regionone);
                addAllVerticalPointsToList(ref coordinateList, (int)regionmin, (int)regionmax, (int)regionone, positiveSlope);
            }

            //Reverse the list if the points go in the opposite direction, ie if the ipad is flipped 180 degrees,
            //the two points used to find the line will be reversed, thus here we are moving left to right vs right to left
            //if (one.x > two.x)
            //{
            //    coordinateList.Reverse();
            //}

            return coordinateList;
        }
コード例 #7
0
        public static LineSegment restrictLineToArraySpace(Point one, Point two, long arrayspacesizex, long arrayspacesizey)
        {
            //These points are in relative array space, but can be outside of the array bounds (ie could be larger then the number of array regions, or negative.)
            //Given two points creates a line, then creates a line segment that is within the array space, along this line
            bool pointOneIsExterior = false, pointTwoIsExterior = false;

            if (one.x < 0 || one.x >= arrayspacesizex || one.y < 0 || one.y >= arrayspacesizey)
            {
                pointOneIsExterior = true;
            }

            if (two.x < 0 || two.x >= arrayspacesizex || two.y < 0 || two.y >= arrayspacesizey)
            {
                pointTwoIsExterior = true;
            }

            //Common case in which both points are interior to the domain
            if (!pointOneIsExterior && !pointTwoIsExterior)
            {
                return new LineSegment(one, two);
            }

            //At least 1 pointis exterior, we need to create a line and restrict part of it to the domain
            LineSegment givenLineSegment = new LineSegment(one, two);

            //Create 4 lines to represent to bounds of this rectangle to test intersections against

            LineSegment left = new LineSegment(new Point(0, 0), new Point(0, arrayspacesizey));
            LineSegment right = new LineSegment(new Point(arrayspacesizex, 0), new Point(arrayspacesizex, arrayspacesizey));
            LineSegment bottom = new LineSegment(new Point(0, 0), new Point(arrayspacesizex, 0));
            LineSegment top = new LineSegment(new Point(0, arrayspacesizey), new Point(arrayspacesizex, arrayspacesizey));
            Point intersectionPoint = new Point(0, 0), intersectionPointOne = new Point(0, 0), intersectionPointTwo = new Point(0, 0);
            bool intersectionPointOneFound = false;
            bool infiniteIntersections = false;
            //we need to see if a line segment exists
            if (pointOneIsExterior && pointTwoIsExterior)
            {
                //both points are exterior, check if there is an overlap of the line segment and the bounding box
                //If there exists a line segment, it will be made with a line that intersects two of the lines of the bounding rectangle

                if (givenLineSegment.intersectsLineSegment(left, ref intersectionPoint, ref infiniteIntersections))
                {

                    intersectionPointOne = intersectionPoint;
                    intersectionPointOneFound = true;
                }

                if (givenLineSegment.intersectsLineSegment(right, ref intersectionPoint, ref infiniteIntersections))
                {

                    if (intersectionPointOneFound)
                    {
                        //The intersection point is on the end of array space, such that when evaluating it. it will be outside of the array
                        //(ex x = 40, array size = 40, but only elements 0-39 are valid)
                        //Thus reduce the size slightly such that when casted to int it will round down to the last element,
                        //but be suffeciently high when evaluated as to give all the necessary boxes
                        //intersectionPoint.x -= reduceConstant;
                        intersectionPointTwo = intersectionPoint;
                        return new LineSegment(intersectionPointOne, intersectionPointTwo);
                    }
                    else
                    {
                        //The intersection point is on the end of array space, such that when evaluating it. it will be outside of the array
                        //(ex x = 40, array size = 40, but only elements 0-39 are valid)
                        //Thus reduce the size slightly such that when casted to int it will round down to the last element,
                        //but be suffeciently high when evaluated as to give all the necessary boxes
                        //intersectionPoint.x -= reduceConstant;
                        intersectionPointOne = intersectionPoint;
                        intersectionPointOneFound = true;
                    }
                }

                if (givenLineSegment.intersectsLineSegment(bottom, ref intersectionPoint, ref infiniteIntersections))
                {

                    if (intersectionPointOneFound)
                    {
                        intersectionPointTwo = intersectionPoint;
                        return new LineSegment(intersectionPointOne, intersectionPointTwo);
                    }
                    else
                    {
                        intersectionPointOne = intersectionPoint;
                        intersectionPointOneFound = true;
                    }
                }

                if (givenLineSegment.intersectsLineSegment(top, ref intersectionPoint, ref infiniteIntersections))
                {

                    if (intersectionPointOneFound)
                    {
                        //The intersection point is on the end of array space, such that when evaluating it. it will be outside of the array
                        //(ex x = 40, array size = 40, but only elements 0-39 are valid)
                        //Thus reduce the size slightly such that when casted to int it will round down to the last element,
                        //but be suffeciently high when evaluated as to give all the necessary boxes
                        //intersectionPoint.y -= reduceConstant;
                        intersectionPointTwo = intersectionPoint;

                        return new LineSegment(intersectionPointOne, intersectionPointTwo);
                    }
                    else
                    {
                        //The intersection point is on the end of array space, such that when evaluating it. it will be outside of the array
                        //(ex x = 40, array size = 40, but only elements 0-39 are valid)
                        //Thus reduce the size slightly such that when casted to int it will round down to the last element,
                        //but be suffeciently high when evaluated as to give all the necessary boxes
                        //intersectionPoint.y -= reduceConstant;
                        intersectionPointOne = intersectionPoint;
                        intersectionPointOneFound = true;
                    }
                }

                //Only other option is no intersections took place
                if (!intersectionPointOneFound)
                {
                    //No intersection found
                    return null;
                }
                else
                {
                    //One intersection found?? not possible?
                }
            }
            else if (pointOneIsExterior)
            {
                //only point one is exterior, there must be exactly one intersection point with the bounding rect, use this intersection point + point 2 for a line segment

                if (givenLineSegment.intersectsLineSegment(left, ref intersectionPoint, ref infiniteIntersections))
                {

                    return new LineSegment(intersectionPoint, two);
                }
                else if (givenLineSegment.intersectsLineSegment(right, ref intersectionPoint, ref infiniteIntersections))
                {
                    //The intersection point is on the end of array space, such that when evaluating it. it will be outside of the array
                    //(ex x = 40, array size = 40, but only elements 0-39 are valid)
                    //Thus reduce the size slightly such that when casted to int it will round down to the last element,
                    //but be suffeciently high when evaluated as to give all the necessary boxes
                    //intersectionPoint.x -= reduceConstant;
                    return new LineSegment(intersectionPoint, two);
                }
                else if (givenLineSegment.intersectsLineSegment(bottom, ref intersectionPoint, ref infiniteIntersections))
                {
                    return new LineSegment(intersectionPoint, two);
                }
                else if (givenLineSegment.intersectsLineSegment(top, ref intersectionPoint, ref infiniteIntersections))
                {
                    //The intersection point is on the end of array space, such that when evaluating it. it will be outside of the array
                    //(ex x = 40, array size = 40, but only elements 0-39 are valid)
                    //Thus reduce the size slightly such that when casted to int it will round down to the last element,
                    //but be suffeciently high when evaluated as to give all the necessary boxes
                    //intersectionPoint.y -= reduceConstant;
                    return new LineSegment(intersectionPoint, two);
                }
                else
                {
                    //Cant happen, but still need to do the last check to get the intersection point
                }

            }
            else
            {
                //Onlypoint two is exterior, there must be actaly one intersection point with the bounding rect, use this intersection point + point one for linesegment
                if (givenLineSegment.intersectsLineSegment(left, ref intersectionPoint, ref infiniteIntersections))
                {
                    return new LineSegment(intersectionPoint, one);
                }
                else if (givenLineSegment.intersectsLineSegment(right, ref intersectionPoint, ref infiniteIntersections))
                {
                    //The intersection point is on the end of array space, such that when evaluating it. it will be outside of the array
                    //(ex x = 40, array size = 40, but only elements 0-39 are valid)
                    //Thus reduce the size slightly such that when casted to int it will round down to the last element,
                    //but be suffeciently high when evaluated as to give all the necessary boxes
                    //intersectionPoint.x -= reduceConstant;
                    return new LineSegment(intersectionPoint, one);
                }
                else if (givenLineSegment.intersectsLineSegment(bottom, ref intersectionPoint, ref infiniteIntersections))
                {
                    return new LineSegment(intersectionPoint, one);
                }
                else if (givenLineSegment.intersectsLineSegment(top, ref intersectionPoint, ref infiniteIntersections))
                {
                    //The intersection point is on the end of array space, such that when evaluating it. it will be outside of the array
                    //(ex x = 40, array size = 40, but only elements 0-39 are valid)
                    //Thus reduce the size slightly such that when casted to int it will round down to the last element,
                    //but be suffeciently high when evaluated as to give all the necessary boxes
                    //intersectionPoint.y -= reduceConstant;
                    return new LineSegment(intersectionPoint, one);
                }
                else
                {
                    //Cant happen, but still need to do the last check/functioncall to get the intersection point
                }
            }

            return null;
        }
コード例 #8
0
ファイル: LineSegmentTest.cs プロジェクト: ase-lab/Skyhunter
        public void LineSegmentConstructorTest()
        {
            Point one = new Point(0, 0);
            Point two = new Point(1, 1);

            LineSegment target = new LineSegment(one, two);
            Assert.AreEqual(target.lowerBound.x, one.x);
            Assert.AreEqual(target.lowerBound.y, one.y);

            Assert.AreEqual(target.upperBound.x, two.x);
            Assert.AreEqual(target.upperBound.y, two.y);

            double expectedSlope = 1.0;
            double expectedYIntercept = 0.0;

            Assert.AreEqual(expectedSlope, target.slope);
            Assert.AreEqual(expectedYIntercept, target.yintercept);

            //Test reversing of which point comes first
            one = new Point(5, 5);
            target = new LineSegment(one, two);
            Assert.AreEqual(target.lowerBound.x, two.x);
            Assert.AreEqual(target.lowerBound.y, two.y);

            Assert.AreEqual(target.upperBound.x, one.x);
            Assert.AreEqual(target.upperBound.y, one.y);
            Assert.AreEqual(expectedSlope, target.slope);
            Assert.AreEqual(expectedYIntercept, target.yintercept);

            //vertical points
            one = new Point(0, 0);
            two = new Point(0, 5);
            target = new LineSegment(one, two);
            Assert.AreEqual(true, target.isInfiniteSlope);
            Assert.AreEqual(target.lowerBound.x, one.x);
            Assert.AreEqual(target.lowerBound.y, one.y);

            Assert.AreEqual(target.upperBound.x, two.x);
            Assert.AreEqual(target.upperBound.y, two.y);

            one = new Point(0, 0);
            two = new Point(0, 5);
            target = new LineSegment(two, one);
            Assert.AreEqual(true, target.isInfiniteSlope);
            Assert.AreEqual(target.lowerBound.x, one.x);
            Assert.AreEqual(target.lowerBound.y, one.y);

            Assert.AreEqual(target.upperBound.x, two.x);
            Assert.AreEqual(target.upperBound.y, two.y);
        }
コード例 #9
0
ファイル: LineSegmentTest.cs プロジェクト: ase-lab/Skyhunter
        public void getMaxOnRegionTest()
        {
            Point one = new Point(0, 0);
            Point two = new Point(1, 1);
            LineSegment target = new LineSegment(one, two);

            double regionx1 = .5;
            double regionx2 = .75;
            double expected = .75;

            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            regionx1 = .25;
            regionx2 = 1.0;
            expected = 1.0;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            //Test when max is outside the region, and restricted
            regionx2 = 1.5;
            regionx1 = .25;
            expected = 1.0;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            one = new Point(0, 1);
            two = new Point(1, 0);
            target = new LineSegment(one, two);
            regionx1 = -.5;
            regionx2 = .2;
            expected = 1.0;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            regionx1 = .5;
            regionx2 = .2;
            expected = .8;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            regionx1 = -.5;
            regionx2 = 1.2;
            expected = 1.0;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            regionx1 = .5;
            regionx2 = 1.2;
            expected = .5;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            //region outside
            one = new Point(0, 1);
            two = new Point(1, 0);
            target = new LineSegment(one, two);
            regionx1 = -.5;
            regionx2 = -.2;
            expected = Double.NaN;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            regionx1 = 1.5;
            regionx2 = 3.2;
            expected = Double.NaN;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            //Test vertical line segment, outside less than
            one = new Point(1, 1);
            two = new Point(1, 0);
            target = new LineSegment(one, two);
            regionx1 = -.5;
            regionx2 = .2;
            expected = Double.NaN;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            //outside greater than
            regionx1 = 1.5;
            regionx2 = 6.62;
            expected = Double.NaN;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            //Region contained
            regionx1 = -.5;
            regionx2 = 1.2;
            expected = 1.0;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            //region equal
            regionx1 = 1.0;
            regionx2 = 1.0;
            expected = 1.0;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));

            regionx1 = 1.5;
            regionx2 = 1.0;
            expected = 1.0;
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx1, regionx2));
            Assert.AreEqual(expected, target.getMaxOnRegion(regionx2, regionx1));
        }
コード例 #10
0
ファイル: LineSegmentTest.cs プロジェクト: ase-lab/Skyhunter
        public void intersectsLineSegmentTest()
        {
            //Normal line segment intersection
            Point one = new Point(0, 0);
            Point two = new Point(1, 1);
            Point one2 = new Point(0, 1);
            Point two2 = new Point(1, 0);

            LineSegment target = new LineSegment(one, two);
            LineSegment target2 = new LineSegment(one2, two2);

            Point intersectionpoint = null;
            Point intersectionpointExpected = new Point(.5, .5);
            bool infiniteIntersections = false, expectedInfiniteIntersections = false;
            bool intersectionExpected = true;
            bool linesActualIntersection;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting at both end points
            one = new Point(-1, -1);
            two = new Point(0, 0);
            one2 = new Point(0, 0);
            two2 = new Point(2, 1);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting at one end point
            one = new Point(-1, 1);
            two = new Point(1, -1);
            one2 = new Point(1, 1);
            two2 = new Point(0, 0);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting line to horizontal
            one = new Point(0, 0);
            two = new Point(1, 1);
            one2 = new Point(-10, 0.5);
            two2 = new Point(10, 0.5);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0.5, 0.5);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting line to horizontal end point
            one = new Point(0, 0);
            two = new Point(1, 1);
            one2 = new Point(.5, 0.5);
            two2 = new Point(10, 0.5);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0.5, 0.5);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting line end point to horizontal
            one = new Point(0, 0);
            two = new Point(1, 1);
            one2 = new Point(-10, 0);
            two2 = new Point(10, 0);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0.0);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting line end point to horizontal end point
            one = new Point(0, 0);
            two = new Point(1, 1);
            one2 = new Point(0, 0);
            two2 = new Point(10, 0);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment to line end point
            one = new Point(0, 0);
            two = new Point(0, 1);
            one2 = new Point(1, 1.5);
            two2 = new Point(0, 0.5);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0.5);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment to line
            one = new Point(0, 0);
            two = new Point(0, 1);
            one2 = new Point(-1, 0);
            two2 = new Point(1, 1);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0.5);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment end point to line
            one = new Point(0.0, 0.0);
            two = new Point(0.0, 1.0);
            one2 = new Point(-1.0, -.5);
            two2 = new Point(1.0, 0.5);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0.0);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment end to line end
            one = new Point(0, 0);
            two = new Point(1, 1);
            one2 = new Point(1, 1);
            two2 = new Point(1, 3);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(1.0, 1.0);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment to horizontal
            one = new Point(0, 0);
            two = new Point(0, 1);
            one2 = new Point(1, .6);
            two2 = new Point(-1, .6);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0.6);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment to horizontal end
            one = new Point(0, 0);
            two = new Point(0, 1);
            one2 = new Point(1, .6);
            two2 = new Point(0, .6);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0.6);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment end to horizontal
            one = new Point(0, 0);
            two = new Point(0, 1);
            one2 = new Point(1, 1);
            two2 = new Point(-1, 1);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 1);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment end to horizontal end
            one = new Point(0, 0);
            two = new Point(0, 1);
            one2 = new Point(1, 1);
            two2 = new Point(0, 1);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 1);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting vertical segment to vertical segment
            one = new Point(0, 0);
            two = new Point(0, 1);
            one2 = new Point(0, 0);
            two2 = new Point(0, -6);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 0.0);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);

            //Connecting horizontal segment to horizontal segment
            one = new Point(0, 5);
            two = new Point(3, 5);
            one2 = new Point(-1, 5);
            two2 = new Point(0, 5);

            target = new LineSegment(one, two);
            target2 = new LineSegment(one2, two2);

            expectedInfiniteIntersections = false;
            intersectionpointExpected = new Point(0, 5.0);
            intersectionExpected = true;
            linesActualIntersection = target.intersectsLineSegment(target2, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
            linesActualIntersection = target2.intersectsLineSegment(target, ref intersectionpoint, ref infiniteIntersections);
            Assert.AreEqual(intersectionpointExpected.x, intersectionpoint.x);
            Assert.AreEqual(intersectionpointExpected.y, intersectionpoint.y);
            Assert.AreEqual(intersectionExpected, linesActualIntersection);
            Assert.AreEqual(expectedInfiniteIntersections, infiniteIntersections);
        }
コード例 #11
0
ファイル: LineSegmentTest.cs プロジェクト: ase-lab/Skyhunter
        public void inDomainTest()
        {
            Point one = new Point(0, 0);
            Point two = new Point(1, 1);
            LineSegment target = new LineSegment(one, two);

            double xval = .5;
            bool expected = true;
            Assert.AreEqual(expected, target.inDomain(xval));

            xval = 0;
            Assert.AreEqual(expected, target.inDomain(xval));
            xval = 1;
            Assert.AreEqual(expected, target.inDomain(xval));
            expected = false;
            xval = -5;
            Assert.AreEqual(expected, target.inDomain(xval));
            xval = 2;
            Assert.AreEqual(expected, target.inDomain(xval));

            one = new Point(-5, 12);
            two = new Point(12, 188);
            target = new LineSegment(one, two);

            xval = -100;
            Assert.AreEqual(expected, target.inDomain(xval));
            xval = 56;
            Assert.AreEqual(expected, target.inDomain(xval));

            expected = true;
            xval = 8;
            Assert.AreEqual(expected, target.inDomain(xval));
            xval = -2;
            Assert.AreEqual(expected, target.inDomain(xval));
        }
コード例 #12
0
ファイル: LineSegmentTest.cs プロジェクト: ase-lab/Skyhunter
        public void inDomainAndRangeTest()
        {
            Point one = new Point(0, 0);
            Point two = new Point(1, 1);
            LineSegment target = new LineSegment(one, two);
            Point intersectionPoint = new Point(.5, .5);
            bool expected = true;
            Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint));

            intersectionPoint = new Point(1.5, .5);
            expected = false;
            Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint));

            one = new Point(0, 0);
            two = new Point(0, 5);
            target = new LineSegment(one, two);
            expected = false;
            intersectionPoint = new Point(1, 4);
            Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint));

            //Here the point 0,10 is in the domain, but not the range.
            expected = false;
            intersectionPoint = new Point(0, 10);
            Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint));

            //Point is on the line segment, ie within range and domain of a vertical line
            expected = true;
            intersectionPoint = new Point(0, 4);
            Assert.AreEqual(expected, target.inDomainAndRange(intersectionPoint));
        }
コード例 #13
0
ファイル: LineSegment.cs プロジェクト: ase-lab/Skyhunter
        public bool intersectsLineSegment(LineSegment L, ref Point intersectionpoint, ref bool infiniteIntersections)
        {
            Point possibleIntersectionPoint = this.intersectsLine(L, ref infiniteIntersections);

            if (infiniteIntersections)
            {
                //possibly Intersects at infinitely many points due to it being a line, but restricting it to a segment, changes things!
                //Find lowest bound:
                LineSegment theLowerSegment, theHigherSegment;

                if (this.isInfiniteSlope)
                {
                    //Use y values to check, not x!
                    if (this.lowerBound.y < L.lowerBound.y)
                    {
                        theHigherSegment = L;
                        theLowerSegment = this;
                    }
                    else
                    {
                        theHigherSegment = this;
                        theLowerSegment = L;
                    }

                    //See how much overlap there is on the 2 bounds
                    if (theLowerSegment.upperBound.y < theHigherSegment.lowerBound.y)
                    {
                        //Non overlapping
                        infiniteIntersections = false;
                        intersectionpoint = null;
                        return false;
                    }
                    else if (theLowerSegment.upperBound.y == theHigherSegment.lowerBound.y)
                    {
                        //Intersect at only one point
                        intersectionpoint = theLowerSegment.upperBound;
                        infiniteIntersections = false;
                        return true;
                    }
                    else
                    {
                        //Overlap of bounds
                        infiniteIntersections = true;
                        intersectionpoint = null;
                        return false;
                    }
                }

                if (this.lowerBound.x < L.lowerBound.x)
                {
                    theHigherSegment = L;
                    theLowerSegment = this;
                }
                else
                {
                    theHigherSegment = this;
                    theLowerSegment = L;
                }
                //See how much overlap there is on the 2 bounds
                if (theLowerSegment.upperBound.x < theHigherSegment.lowerBound.x)
                {
                    //Non overlapping
                    infiniteIntersections = false;
                    intersectionpoint = null;
                    return false;
                }
                else if (theLowerSegment.upperBound.x == theHigherSegment.lowerBound.x)
                {
                    //Intersect at only one point
                    intersectionpoint = theLowerSegment.upperBound;
                    infiniteIntersections = false;
                    return true;
                }
                else
                {
                    //Overlap of bounds
                    infiniteIntersections = true;
                    intersectionpoint = null;
                    return false;
                }
            }

            if (possibleIntersectionPoint == null)
            {
                //Line doesnt interesect
                intersectionpoint = null;
                return false;
            }

            if (inDomainAndRange(possibleIntersectionPoint) && L.inDomainAndRange(possibleIntersectionPoint))
            {
                intersectionpoint = possibleIntersectionPoint;
                return true;
            }
            //The line intersects but is not within the domain of both lines

            return false;
        }