Exemplo n.º 1
0
		/* For debugging */
		public static void DumpRegion (Region rgn)
		{
			Matrix matrix = new Matrix ();
			RectangleF [] rects = rgn.GetRegionScans (matrix);

			for (int i = 0; i < rects.Length; i++)
				Console.WriteLine ( rects[i]);
		}
Exemplo n.º 2
0
		public static void Main(string[] args)
		{
			Region region = new Region ();
			
			RectangleF[] rects = region.GetRegionScans (new Matrix ());
			
			for (int i = 0; i < rects.Length; i++)
				Console.WriteLine ("{0}", rects [i]);
		}
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rgn"></param>
        /// <param name="pt1"></param>
        /// <param name="pt2"></param>
        /// <param name="ptIntercept"></param>
        /// <returns></returns>
        public static bool GetBoundaryIntercept(System.Drawing.Region rgn, PointF pt1, PointF pt2, out PointF ptIntercept)
        {
            bool hasIntercept = false;

            ptIntercept = new PointF(0, 0);

            bool pt1InRegion = rgn.IsVisible(pt1);
            bool pt2InRegion = rgn.IsVisible(pt2);

            if ((pt1InRegion && !pt2InRegion) || (pt2InRegion && !pt1InRegion))
            {
                PointF ptInside;
                PointF ptOutside;

                if (pt1InRegion)
                {
                    ptInside  = pt1;
                    ptOutside = pt2;
                }
                else
                {
                    ptInside  = pt2;
                    ptOutside = pt1;
                }

                RectangleF[] rcScans = rgn.GetRegionScans(new Matrix());
                RectangleF   rcCur;
                PointF[]     ptsIntersect;
                int          numIntersects;
                double       minDist = double.MaxValue;
                double       curDist;
                PointF       curPt;
                int          rcIdx;
                int          ptIdx;

                for (rcIdx = 0; rcIdx < rcScans.Length; rcIdx++)
                {
                    rcCur         = rcScans[rcIdx];
                    numIntersects = Geometry.GetLineIntersect(ptOutside, ptInside, rcCur, out ptsIntersect);

                    for (ptIdx = 0; ptIdx < numIntersects; ptIdx++)
                    {
                        curPt   = ptsIntersect[ptIdx];
                        curDist = Geometry.PointDistance(ptOutside, curPt);
                        if (curDist < minDist)
                        {
                            ptIntercept  = curPt;
                            minDist      = curDist;
                            hasIntercept = true;
                        }
                    }
                }
            }

            return(hasIntercept);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Retrieves an array of rectangles that approximates a region, and computes the
 /// pixel area of it. This method is necessary to work around some bugs in .NET
 /// and to increase performance for the way in which we typically use this data.
 /// </summary>
 /// <param name="region">The Region to retrieve data from.</param>
 /// <param name="scans">An array of Rectangle to put the scans into.</param>
 /// <param name="area">An integer to write the computed area of the region into.</param>
 /// <remarks>
 /// Note to implementors: Simple implementations may simple call region.GetRegionScans()
 /// and process the data for the 'out' variables.</remarks>
 public static void GetRegionScans(Region region, out Rectangle[] scans, out int area)
 {
     using (Matrix matrix = new Matrix ()) {
         RectangleF [] s = region.GetRegionScans (matrix);
         scans = new Rectangle [s.Length];
         area = 0;
         for (int i = 0; i < s.Length; i++){
             scans [i].X      = (int) s [i].X;
             scans [i].Y      = (int) s [i].Y;
             scans [i].Width  = (int) s [i].Width;
             scans [i].Height = (int) s [i].Height;
             area += scans[i].Width * scans[i].Height;
         }
     }
 }
Exemplo n.º 5
0
        // Returns a percentage of the ErrorArea circle trespassing on the zone.
        // If anyone knows calculus better than me I'd welcome you to clean up this function =)
        public float TrespassArea(Point3D pntLocation, float flErrorRadius) {

            float flReturnPercentage = 0.0F;
            float flErrorArea = (float)(flErrorRadius * flErrorRadius * Math.PI);

            GraphicsPath gpLocationError = new GraphicsPath();
            gpLocationError.AddEllipse(new RectangleF(pntLocation.X - flErrorRadius, pntLocation.Y - flErrorRadius, flErrorRadius * 2, flErrorRadius * 2));
            gpLocationError.CloseAllFigures();

            Region regZone = new Region(this.ZoneGraphicsPath);
            regZone.Intersect(gpLocationError);
            RectangleF[] a_recScans = regZone.GetRegionScans(new Matrix());
            Rectangle recIntersection = new Rectangle(int.MaxValue, int.MaxValue, 0, 0);

            int iPixelCount = 0;

            if (a_recScans.Length > 0) {

                for (int i = 0; i < a_recScans.Length; i++) {
                    recIntersection.X = a_recScans[i].X < recIntersection.X ? (int)a_recScans[i].X : recIntersection.X;
                    recIntersection.Y = a_recScans[i].Y < recIntersection.Y ? (int)a_recScans[i].Y : recIntersection.Y;

                    recIntersection.Width = a_recScans[i].Right > recIntersection.Right ? (int)a_recScans[i].Right - recIntersection.X : recIntersection.Width;
                    recIntersection.Height = a_recScans[i].Bottom > recIntersection.Bottom ? (int)a_recScans[i].Bottom - recIntersection.Y : recIntersection.Height;
                }

                //recIntersection = this.RecFtoRec(regZone.GetBounds(this.CreateGraphics()));
                Point pntVisible = new Point(recIntersection.X, recIntersection.Y);

                for (pntVisible.X = recIntersection.X; pntVisible.X <= recIntersection.Right; pntVisible.X++) {
                    for (pntVisible.Y = recIntersection.Y; pntVisible.Y <= recIntersection.Bottom; pntVisible.Y++) {
                        if (regZone.IsVisible(pntVisible) == true) {
                            iPixelCount++;
                        }
                    }
                }
            }

            flReturnPercentage = (float)iPixelCount / flErrorArea;

            // Accounts for low error when using this method. (98.4% should be 100%)
            // but using regZone.GetRegionScans is slightly lossy.
            if (flReturnPercentage > 0.0F) {
                flReturnPercentage = (float)Math.Min(1.0F, flReturnPercentage + 0.02);
            }

            return flReturnPercentage;
        }
Exemplo n.º 6
0
		private void OnShapeChange (object sender, EventArgs e)
		{
			GraphicsPath path = GetShape (shapeComboBox);
			if (path != null) {
				if (region != null) {
					region.Dispose ();
				}
				region = new Region (path);
				scans = region.GetRegionScans (Matrix);

				infoLabel.Text = System.String.Format ("{0} rectangles to re-create the shape.", scans.Length);
				StringBuilder sb = new StringBuilder ();
				for (int i = 0; i < scans.Length; i++) {
					sb.AppendFormat ("{0}: x {1}, y {2}, w {3}, h {4}{5}", i,
						scans[i].X, scans[i].Y, scans[i].Width, scans[i].Height, 
						Environment.NewLine);
				}
				scansTextBox.Text = sb.ToString ();
			}
			UpdateUI ();
		}
Exemplo n.º 7
0
		public void TestCloneAndEquals()
		{
			Bitmap bmp = new Bitmap (600, 800);
			Graphics dc = Graphics.FromImage (bmp);
			Rectangle rect1, rect2;
			Region rgn1, rgn2;
			RectangleF [] rects;
			RectangleF [] rects2;
			Matrix matrix = new Matrix ();

			rect1 = new Rectangle (500, 30, 60, 80);
			rect2 = new Rectangle (520, 40, 60, 80);
			rgn1 = new Region (rect1);
			rgn1.Union (rect2);
			rgn2 = rgn1.Clone ();

			rects = rgn1.GetRegionScans (matrix);
			rects2 = rgn2.GetRegionScans (matrix);

			Assert.AreEqual (rects.Length, rects2.Length);

			for (int i = 0; i < rects.Length; i++) {

				Assert.AreEqual (rects[i].X, rects[i].X);
				Assert.AreEqual (rects[i].Y, rects[i].Y);
				Assert.AreEqual (rects[i].Width, rects[i].Width);
				Assert.AreEqual (rects[i].Height, rects[i].Height);
			}

			Assert.AreEqual (true, rgn1.Equals (rgn2, dc));
		}
Exemplo n.º 8
0
		public void SmallXor2 ()
		{
			Region region = new Region (sp2);
			region.Xor (sp1);
			CompareSmallRegion (region, sxor, 7, 7);

			RectangleF[] scans = region.GetRegionScans (matrix);
			Assert.AreEqual (4, scans.Length, "GetRegionScans");
			CheckRectF ("[0]", 0, 0, 3, 2, scans[0]);
			CheckRectF ("[1]", 0, 2, 2, 1, scans[1]);
			CheckRectF ("[2]", 3, 2, 2, 1, scans[2]);
			CheckRectF ("[3]", 2, 3, 3, 2, scans[3]);
		}
Exemplo n.º 9
0
		public void SmallComplement2 ()
		{
			Region region = new Region (sp2);
			region.Complement (sp1);
			CompareSmallRegion (region, sexclude1, 7, 7);

			RectangleF[] scans = region.GetRegionScans (matrix);
			Assert.AreEqual (2, scans.Length, "GetRegionScans");
			CheckRectF ("[0]", 0, 0, 3, 2, scans[0]);
			CheckRectF ("[1]", 0, 2, 2, 1, scans[1]);
		}
Exemplo n.º 10
0
        /// <summary>Implements a b&w + hue-based transformation for an image.</summary>
        /// <param name="original">The original image.</param>
        /// <param name="selectedPixels">The location in the original image of the selected pixels for hue
        /// comparison.</param>
        /// <param name="epsilon">Allowed hue variation from selected pixels.</param>
        /// <param name="paths">GraphicPath instances demarcating regions containing possible pixels to be
        /// left in color.</param>
        /// <param name="parallel">Whether to run in parallel.</param>
        /// <returns>The new Bitmap.</returns>
        public Bitmap Colorize(Bitmap original, List<Point> selectedPixels, int epsilon, List<GraphicsPath> paths, bool parallel)
        {
            // Create a new bitmap with the same size as the original
            int width = original.Width, height = original.Height;
            Bitmap colorizedImage = new Bitmap(width, height);

            // Optimization: For every GraphicsPath, get a bounding rectangle.  This allows for quickly
            // ruling out pixels that are definitely not containing within the selected region.
            Rectangle [] pathsBounds = null;
            if (paths != null && paths.Count > 0) 
            {
                pathsBounds = new Rectangle[paths.Count];
                for(int i=0; i<pathsBounds.Length; i++)
                {
                    pathsBounds[i] = Rectangle.Ceiling(paths[i].GetBounds());
                }
            }

            // Optimization: Hit-testing against GraphicPaths is relatively slow.  Hit testing
            // against rectangles is very fast.  As such, appromixate the area of the GraphicsPath
            // with rectangles which can be hit tested against instead of the paths.  Not quite
            // as accurate, but much faster.
            List<RectangleF[]> compositions = null;
            if (paths != null && paths.Count > 0)
            {
                compositions = new List<RectangleF[]>(paths.Count);
                using (Matrix m = new Matrix())
                {
                    for(int i=0; i<paths.Count; i++)
                    {
                        using (Region r = new Region(paths[i])) compositions.Add(r.GetRegionScans(m));
                    }
                }
            }

            // Use FastBitmap instances to provide unsafe/faster access to the pixels
            // in the original and in the new images
            using (FastBitmap fastColorizedImage = new FastBitmap(colorizedImage))
            using (FastBitmap fastOriginalImage = new FastBitmap(original))
            {
                // Extract the selected hues from the selected pixels
                List<float> selectedHues = new List<float>(selectedPixels.Count);
                foreach (Point p in selectedPixels)
                {
                    selectedHues.Add(fastOriginalImage.GetColor(p.X, p.Y).GetHue());
                }

                // For progress update purposes, figure out how many pixels there
                // are in total, and how many constitute 1% so that we can raise
                // events after every additional 1% has been completed.
                long totalPixels = height * width;
                long pixelsPerProgressUpdate = totalPixels / 100;
                if (pixelsPerProgressUpdate == 0) pixelsPerProgressUpdate = 1;
                long pixelsProcessed = 0;

                // Pixels close to the selected hue but not close enough may be
                // left partially desaturated.  The saturation window determines
                // what pixels fall into that range.
                const int maxSaturationWindow = 10;
                int saturationWindow = Math.Min(maxSaturationWindow, epsilon);

                // Separated out the body of the loop just to make it easier
                // to switch between sequential and parallel for demo purposes
                Action<int> processRow = y =>
                {
                    for (int x = 0; x < width; x++)
                    {
                        // Get the color/hue of th epixel
                        Color c = fastOriginalImage.GetColor(x, y);
                        float pixelHue = c.GetHue();

                        // Use hit-testing to determine if the pixel is in the selected region.
                        bool pixelInSelectedRegion = false;

                        // First, if there are no paths, by definition it is in the selected
                        // region, since the whole image is then selected
                        if (paths == null || paths.Count == 0) pixelInSelectedRegion = true;
                        else
                        {
                            // For each path, first see if the pixel is within the bounding
                            // rectangle; if it's not, it's not in the selected region.
                            Point p = new Point(x, y);
                            for (int i = 0; i < pathsBounds.Length && !pixelInSelectedRegion; i++)
                            {
                                if (pathsBounds[i].Contains(p))
                                {
                                    // The pixel is within a bounding rectangle, so now
                                    // see if it's within the composition rectangles
                                    // approximating the region.
                                    foreach (RectangleF bound in compositions[i])
                                    {
                                        if (bound.Contains(x, y))
                                        {
                                            // If it is, it's in the region.
                                            pixelInSelectedRegion = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }

                        // Now that we know whether a pixel is in the region,
                        // we can figure out what to do with it.  If the pixel
                        // is not in the selected region, it needs to be converted
                        // to grayscale.
                        bool useGrayscale = true;
                        if (pixelInSelectedRegion)
                        {
                            // If it is in the selected region, get the color hue's distance 
                            // from each target hue.  If that distance is less than the user-selected
                            // hue variation limit, leave it in color.  If it's greater than the
                            // variation limit, but within the saturation window of the limit,
                            // desaturate it proportionally to the distance from the limit.
                            foreach (float selectedHue in selectedHues)
                            {
                                // A hue wheel is 360 degrees. If you pick two points on a wheel, there
                                // will be two distances between them, depending on which way you go around
                                // the wheel from one to the other (unless they're exactly opposite from
                                // each other on the wheel, the two distances will be different).  We always
                                // want to do our work based on the smaller of the two distances (e.g. a hue
                                // with the value 359 is very, very close to a hue with the value 1).  So,
                                // we take the absolute value of the difference between the two hues.  If that
                                // distance is 180 degrees, then both distances are the same, so it doesn't
                                // matter which we go with. If that difference is less than 180 degrees, 
                                // we know this must be the smaller of the two distances, since the sum of the 
                                // two distances must add up to 360.  If, however, it's larger than 180, it's the
                                // longer distance, so to get the shorter one, we have to subtract it from 360.
                                float distance = Math.Abs(pixelHue - selectedHue);
                                if (distance > 180) distance = 360 - distance;

                                if (distance <= epsilon)
                                {
                                    useGrayscale = false;
                                    break;
                                }
                                else if ((distance - epsilon) / saturationWindow < 1.0f)
                                {
                                    useGrayscale = false;
                                    c = ColorFromHsb(
                                        pixelHue,
                                        c.GetSaturation() * (1.0f - ((distance - epsilon) / maxSaturationWindow)),
                                        c.GetBrightness());
                                    break;
                                }
                            }
                        }

                        // Set the pixel color into the new image
                        if (useGrayscale) c = ToGrayscale(c);
                        fastColorizedImage.SetColor(x, y, c);
                    }

                    // Notify any listeners of our progress, if enough progress has been made
                    Interlocked.Add(ref pixelsProcessed, width);
                    OnProgressChanged((int)(100 * pixelsProcessed / (double)totalPixels));
                };

                // Copy over every single pixel, and possibly transform it in the process
                if (parallel)
                {
                    Parallel.For(0, height, processRow);
                }
                else
                {
                    for (int y = 0; y < height; y++) processRow(y);
                }
            }

            // We're done creating the image.  Return it.
            return colorizedImage;
        }
Exemplo n.º 11
0
	private void InvalidateInternal(Region region, bool invalidateChildren)
			{
				if(invalidateChildren)
				{
					for(int i = (numChildren - 1); i >= 0; --i)
					{
						Control child = children[i];
						if (child.visible)
						{
							Region region1 = (Region)region.Clone();
							region1.Intersect(child.Bounds);
							region1.Translate(-child.Left, - child.Top);
							child.InvalidateInternal(region1, true);
						}
					}
				}

				// Exclude the children from the invalidate
				for(int i = (numChildren - 1); i >= 0; --i)
				{
					Control child = children[i];
					if (child.visible)
					{
						region.Exclude(children[i].Bounds);
					}
				}

				// TODO Inefficient
				RectangleF[] rs = region.GetRegionScans(new Drawing.Drawing2D.Matrix());
				if( rs.Length > 0 ) {
					// TODO Inefficient
					Point p = ClientOrigin;
					Size  s = ClientSize;
					int xOrigin = p.X;
					int yOrigin = p.Y;
				// The rectangle relative to the toolkit that is the bounds for this control.
					Rectangle parentInvalidateBounds = new Rectangle(xOrigin, yOrigin, s.Width, s.Height);
					
					for(int i = 0; i < rs.Length; i++)
					{
						Rectangle b = Rectangle.Truncate(rs[i]);
						// Get in local coordinates.
						b.Offset(xOrigin, yOrigin);
						b.Intersect(parentInvalidateBounds);
						if(!b.IsEmpty)
						{
							if(toolkitWindow == null)
							{
								CreateControl();
							}
							toolkitWindow.Invalidate(b.X, b.Y, b.Width, b.Height);
						}
					}
				}
			}
Exemplo n.º 12
0
		public void TestUnionGroup1 ()
		{
			Bitmap bmp = new Bitmap (600, 800);
			Graphics dc = Graphics.FromImage (bmp);
			Matrix matrix = new Matrix ();
			Rectangle rect1, rect2, rect3, rect4;
			Region rgn1, rgn2, rgn3, rgn4;
			RectangleF [] rects;

			rect1 = new Rectangle (500, 30, 60, 80);
			rect2 = new Rectangle (520, 40, 60, 80);
			rgn1 = new Region(rect1);
			rgn2 = new Region(rect2);
			rgn1.Union(rgn2);
			rects = rgn1.GetRegionScans (matrix);

			Assert.AreEqual (3, rects.Length);
			Assert.AreEqual (500, rects[0].X);
			Assert.AreEqual (30, rects[0].Y);
			Assert.AreEqual (60, rects[0].Width);
			Assert.AreEqual (10, rects[0].Height);

			Assert.AreEqual (500, rects[1].X);
			Assert.AreEqual (40, rects[1].Y);
			Assert.AreEqual (80, rects[1].Width);
			Assert.AreEqual (70, rects[1].Height);

			Assert.AreEqual (520, rects[2].X);
			Assert.AreEqual (110, rects[2].Y);
			Assert.AreEqual (60, rects[2].Width);
			Assert.AreEqual (10, rects[2].Height);

			rect1 = new Rectangle (20, 180, 40, 50);
			rect2 = new Rectangle (50, 190, 40, 50);
			rect3 = new Rectangle (70, 210, 30, 50);
			rgn1 = new Region (rect1);
			rgn2 = new Region (rect2);
			rgn3 = new Region (rect3);

			rgn1.Union (rgn2);
			rgn1.Union (rgn3);
			rects = rgn1.GetRegionScans (matrix);
			Assert.AreEqual (5, rects.Length);

			Assert.AreEqual (20, rects[0].X);
			Assert.AreEqual (180, rects[0].Y);
			Assert.AreEqual (40, rects[0].Width);
			Assert.AreEqual (10, rects[0].Height);

			Assert.AreEqual (20, rects[1].X);
			Assert.AreEqual (190, rects[1].Y);
			Assert.AreEqual (70, rects[1].Width);
			Assert.AreEqual (20, rects[1].Height);

			Assert.AreEqual (20, rects[2].X);
			Assert.AreEqual (210, rects[2].Y);
			Assert.AreEqual (80, rects[2].Width);
			Assert.AreEqual (20, rects[2].Height);

			Assert.AreEqual (50, rects[3].X);
			Assert.AreEqual (230, rects[3].Y);
			Assert.AreEqual (50, rects[3].Width);
			Assert.AreEqual (10, rects[3].Height);

			Assert.AreEqual (70, rects[4].X);
			Assert.AreEqual (240, rects[4].Y);
			Assert.AreEqual (30, rects[4].Width);
			Assert.AreEqual (20, rects[4].Height);

			rect1 = new Rectangle (20, 330, 40, 50);
			rect2 = new Rectangle (50, 340, 40, 50);
			rect3 = new Rectangle (70, 360, 30, 50);
			rect4 = new Rectangle (80, 400, 30, 10);
			rgn1 = new Region (rect1);
			rgn2 = new Region (rect2);
			rgn3 = new Region (rect3);
			rgn4 = new Region (rect4);

			rgn1.Union (rgn2);
			rgn1.Union (rgn3);
			rgn1.Union (rgn4);

			rects = rgn1.GetRegionScans (matrix);

			Assert.AreEqual (6, rects.Length);

			Assert.AreEqual (20, rects[0].X);
			Assert.AreEqual (330, rects[0].Y);
			Assert.AreEqual (40, rects[0].Width);
			Assert.AreEqual (10, rects[0].Height);

			Assert.AreEqual (20, rects[1].X);
			Assert.AreEqual (340, rects[1].Y);
			Assert.AreEqual (70, rects[1].Width);
			Assert.AreEqual (20, rects[1].Height);

			Assert.AreEqual (20, rects[2].X);
			Assert.AreEqual (360, rects[2].Y);
			Assert.AreEqual (80, rects[2].Width);
			Assert.AreEqual (20, rects[2].Height);

			Assert.AreEqual (50, rects[3].X);
			Assert.AreEqual (380, rects[3].Y);
			Assert.AreEqual (50, rects[3].Width);
			Assert.AreEqual (10, rects[3].Height);

			Assert.AreEqual (70, rects[4].X);
			Assert.AreEqual (390, rects[4].Y);
			Assert.AreEqual (30, rects[4].Width);
			Assert.AreEqual (10, rects[4].Height);

			Assert.AreEqual (70, rects[5].X);
			Assert.AreEqual (400, rects[5].Y);
			Assert.AreEqual (40, rects[5].Width);
			Assert.AreEqual (10, rects[5].Height);

			rect1 = new Rectangle (10, 20, 50, 50);
			rect2 = new Rectangle (100, 100, 60, 60);
			rect3 = new Rectangle (200, 200, 80, 80);

			rgn1 = new Region (rect1);
			rgn1.Union (rect2);
			rgn1.Union (rect3);

			rects = rgn1.GetRegionScans (matrix);

			Assert.AreEqual (3, rects.Length);

			Assert.AreEqual (10, rects[0].X);
			Assert.AreEqual (20, rects[0].Y);
			Assert.AreEqual (50, rects[0].Width);
			Assert.AreEqual (50, rects[0].Height);

			Assert.AreEqual (100, rects[1].X);
			Assert.AreEqual (100, rects[1].Y);
			Assert.AreEqual (60, rects[1].Width);
			Assert.AreEqual (60, rects[1].Height);

			Assert.AreEqual (200, rects[2].X);
			Assert.AreEqual (200, rects[2].Y);
			Assert.AreEqual (80, rects[2].Width);
			Assert.AreEqual (80, rects[2].Height);
		}
Exemplo n.º 13
0
		public void Complement_383878 ()
		{
			using (Region clipRegion = new Region ()) {
				clipRegion.MakeInfinite ();

				Rectangle smaller = new Rectangle (5, 5, -10, -10);
				Rectangle bigger = new Rectangle (-5, -5, 12, 12);

				clipRegion.Intersect (smaller);
				clipRegion.Complement (bigger);

				Assert.IsFalse (clipRegion.IsEmpty (graphic), "IsEmpty");
				Assert.IsFalse (clipRegion.IsInfinite (graphic), "IsInfinite");

				RectangleF [] rects = clipRegion.GetRegionScans (new Matrix ());
				Assert.AreEqual (2, rects.Length, "Length");
				Assert.AreEqual (new RectangleF (5, -5, 2, 10), rects [0], "0");
				Assert.AreEqual (new RectangleF (-5, 5, 12, 2), rects [1], "1");
			}
		}
Exemplo n.º 14
0
		public void Rectangle_GetRegionScans ()
		{
			Matrix matrix = new Matrix ();
			GraphicsPath gp = new GraphicsPath ();
			gp.AddRectangle (new Rectangle (10, 10, 10, 10));
			Region region = new Region (gp);
			Assert.AreEqual (1, region.GetRegionScans (matrix).Length, "1");

			gp.AddRectangle (new Rectangle (20, 20, 20, 20));
			region = new Region (gp);
			Assert.AreEqual (2, region.GetRegionScans (matrix).Length, "2");
		}
Exemplo n.º 15
0
		public void TestInfiniteAndEmpty()
		{
			Bitmap bmp = new Bitmap (600, 800);
			Graphics dc = Graphics.FromImage (bmp);
			Rectangle rect1, rect2;
			Region rgn1;
			RectangleF [] rects;
			Matrix matrix = new Matrix ();

			rect1 = new Rectangle (500, 30, 60, 80);
			rect2 = new Rectangle (520, 40, 60, 80);
			rgn1 = new Region (rect1);
			rgn1.Union (rect2);

			Assert.AreEqual (false, rgn1.IsEmpty (dc));
			Assert.AreEqual (false, rgn1.IsInfinite (dc));

			rgn1.MakeEmpty();
			Assert.AreEqual (true, rgn1.IsEmpty (dc));

			rgn1 = new Region (rect1);
			rgn1.Union (rect2);
			rgn1.MakeInfinite ();
			rects = rgn1.GetRegionScans (matrix);

			Assert.AreEqual (1, rects.Length);
			Assert.AreEqual (-4194304, rects[0].X);
			Assert.AreEqual (-4194304, rects[0].Y);
			Assert.AreEqual (8388608, rects[0].Width);
			Assert.AreEqual (8388608, rects[0].Height);
			Assert.AreEqual (true, rgn1.IsInfinite (dc));
		}
Exemplo n.º 16
0
		public void TestTranslate()
		{
			Region rgn1 = new Region (new RectangleF (10, 10, 120,120));
			rgn1.Translate (30,20);
			Matrix matrix = new Matrix ();

			RectangleF [] rects = rgn1.GetRegionScans (matrix);

			Assert.AreEqual (1, rects.Length);

			Assert.AreEqual (40, rects[0].X);
			Assert.AreEqual (30, rects[0].Y);
			Assert.AreEqual (120, rects[0].Width);
			Assert.AreEqual (120, rects[0].Height);
		}
Exemplo n.º 17
0
		public void TestIntersect()
		{


			Bitmap bmp = new Bitmap (600, 800);
			Graphics dc = Graphics.FromImage (bmp);
			Matrix matrix = new Matrix ();
			RectangleF [] rects;
			RectangleF rect3, rect4;
			Region rgn3, rgn4;

			/* Two simple areas */
			Rectangle rect1 = new Rectangle (260, 30, 60, 80);
			Rectangle rect2 = new Rectangle (290, 40, 60, 80);
			Region rgn1 = new Region (rect1);
			Region rgn2 = new Region (rect2);
			rgn1.Intersect (rgn2);

			rects = rgn1.GetRegionScans (matrix);
			Assert.AreEqual (1, rects.Length);

			Assert.AreEqual (290, rects[0].X);
			Assert.AreEqual (40, rects[0].Y);
			Assert.AreEqual (30, rects[0].Width);
			Assert.AreEqual (70, rects[0].Height);							

			/* No intersect */
			rect1 = new Rectangle (20, 330, 40, 50);
			rect2 = new Rectangle (50, 340, 40, 50);
			rect3 = new Rectangle (70, 360, 30, 50);
			rect4 = new Rectangle (80, 400, 30, 10);
			rgn1 = new Region (rect1);
			rgn2 = new Region (rect2);
			rgn3 = new Region (rect3);
			rgn4 = new Region (rect4);

			rgn1.Intersect (rgn2);
			rgn1.Intersect (rgn3);
			rgn1.Intersect (rgn4);
			rects = rgn1.GetRegionScans (matrix);
			Assert.AreEqual (0, rects.Length);
		}
Exemplo n.º 18
0
		public void TestXor()
		{
			Bitmap bmp = new Bitmap (600, 800);
			Graphics dc = Graphics.FromImage (bmp);
			Matrix matrix = new Matrix ();
			RectangleF [] rects;

			Rectangle rect1 = new Rectangle (380, 30, 60, 80);
			Rectangle rect2 = new Rectangle (410, 40, 60, 80);
			Region rgn1 = new Region (rect1);
			Region rgn2 = new Region (rect2);
			rgn1.Xor (rgn2);


			rects = rgn1.GetRegionScans (matrix);

			Assert.AreEqual (4, rects.Length);

			Assert.AreEqual (380, rects[0].X);
			Assert.AreEqual (30, rects[0].Y);
			Assert.AreEqual (60, rects[0].Width);
			Assert.AreEqual (10, rects[0].Height);

			Assert.AreEqual (380, rects[1].X);
			Assert.AreEqual (40, rects[1].Y);
			Assert.AreEqual (30, rects[1].Width);
			Assert.AreEqual (70, rects[1].Height);

			Assert.AreEqual (440, rects[2].X);
			Assert.AreEqual (40, rects[2].Y);
			Assert.AreEqual (30, rects[2].Width);
			Assert.AreEqual (70, rects[2].Height);

			Assert.AreEqual (410, rects[3].X);
			Assert.AreEqual (110, rects[3].Y);
			Assert.AreEqual (60, rects[3].Width);
			Assert.AreEqual (10, rects[3].Height);
		}
Exemplo n.º 19
0
		public void TestUnionGroup2 ()
		{
			RectangleF[] rects;
			Region r1  = new Region ();
			Rectangle rect2 = Rectangle.Empty;
			Rectangle rect1 = Rectangle.Empty;
			Rectangle rect3 = Rectangle.Empty;
			Rectangle rect4 = Rectangle.Empty;

			{ // TEST1: Not intersecting rects. Union just adds them

				rect1 = new Rectangle (20, 20, 20, 20);
				rect2 = new Rectangle (20, 80, 20, 10);
				rect3 = new Rectangle (60, 60, 30, 10);

				r1 = new Region (rect1);
				r1.Union (rect2);
				r1.Union (rect3);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual (3, rects.Length, "TUG1Test1");
				AssertEqualRectangles (new RectangleF (20, 20, 20, 20), rects[0], "TUG1Test2");
				AssertEqualRectangles (new RectangleF (60, 60, 30, 10), rects[1], "TUG1Test3");
				AssertEqualRectangles (new RectangleF (20, 80, 20, 10), rects[2], "TUG1Test4");
			}

			{ // TEST2: Intersecting from the right
				 /*
				 *  -----------
				 *  |         |
				 *  |     |-------- |
				 *  |     |         |
				 *  |     |-------- |
				 *  |	      |
				 *  ----------|
				 *
				 */

				rect1 = new Rectangle (10, 10, 100, 100);
				rect2 = new Rectangle (40, 60, 100, 20);
				r1 = new Region (rect1);
				r1.Union (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (3, rects.Length, "TUG2Test1");
				AssertEqualRectangles (new RectangleF (10, 10, 100, 50), rects[0], "TUG2Test2");
				AssertEqualRectangles (new RectangleF (10, 60, 130, 20), rects[1], "TUG2Test3");
				AssertEqualRectangles (new RectangleF (10, 80, 100, 30), rects[2], "TUG2Test4");
			}

			{ // TEST3: Intersecting from the right
				 /*
				 *  	-----------
				 *  	|         |
				 * |-------- |    |
				 * |         |    |
				 * |-------- |    |
				 *  	|	  |
				 *  	----------|
				 *
				 */

				rect1 = new Rectangle (70, 10, 100, 100);
				rect2 = new Rectangle (40, 60, 100, 20);

				r1 = new Region (rect1);
				r1.Union (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (3, rects.Length, "TUG3Test1");
				AssertEqualRectangles (new RectangleF (70, 10, 100, 50), rects[0], "TUG3Test2");
				AssertEqualRectangles (new RectangleF (40, 60, 130, 20), rects[1], "TUG3Test3");
				AssertEqualRectangles (new RectangleF (70, 80, 100, 30), rects[2], "TUG3Test4");
			}

			{ // TEST4: Intersecting from the top
				 /*
				 *  	   -----
				 *  	   |   |
				 *  	-----------
				 *  	|  |   |  |
				 *  	|  -----  |
				 *  	|         |
				 *  	|	  |
				 *  	----------|
				 *
				 */

				rect1 = new Rectangle (40, 100, 100, 100);
				rect2 = new Rectangle (70, 80, 50, 40);
				r1 = new Region (rect1);
				r1.Union (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (2, rects.Length, "TUG4Test1");
				AssertEqualRectangles (new RectangleF (70, 80, 50, 20), rects[0], "TUG4Test2");
				AssertEqualRectangles (new RectangleF (40, 100, 100, 100), rects[1], "TUG4Test3");
			}

			{ // TEST5: Intersecting from the bottom
				 /*

				 *  	-----------
				 *  	|  	  |
				 *  	|  	  |
				 *  	|         |
				 *  	|  |   |  |
				 *  	|--|   |--|
				 *	   |   |
				 *  	   -----
				 */

				rect1 = new Rectangle (40, 10, 100, 100);
				rect2 = new Rectangle (70, 80, 50, 40);

				r1 = new Region (rect1);
				r1.Union (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (2, rects.Length, "TUG5Test1");
				AssertEqualRectangles (new RectangleF (40, 10, 100, 100), rects[0], "TUG5Test2");
				AssertEqualRectangles (new RectangleF (70, 110, 50, 10), rects[1], "TUG5Test3");
			}

			{ // TEST6: Multiple regions, two separted by zero pixels

				rect1 = new Rectangle (30, 30, 80, 80);
				rect2 = new Rectangle (45, 45, 200, 200);
				rect3 = new Rectangle (160, 260, 10, 10);
				rect4 = new Rectangle (170, 260, 10, 10);

				r1 = new Region (rect1);
				r1.Union (rect2);
				r1.Union (rect3);
				r1.Union (rect4);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (4, rects.Length, "TUG6Test1");
				AssertEqualRectangles (new RectangleF (30, 30, 80, 15), rects[0], "TUG6Test2");
				AssertEqualRectangles (new RectangleF (30, 45, 215, 65), rects[1], "TUG6Test3");
				AssertEqualRectangles (new RectangleF (45, 110, 200, 135), rects[2], "TUG6Test4");
				AssertEqualRectangles (new RectangleF (160, 260, 20, 10), rects[3], "TUG6Test5");
			}
		}
Exemplo n.º 20
0
        private void DrawDotDensity(ref Graphics g, Theme _Theme, Matrix mTransMatrix, RectangleF CurExt)
        {
            int DotCount;
            int DotSize = (int)_Theme.DotSize;
            double DotValue = _Theme.DotValue;

            Brush BrDot = new SolidBrush(_Theme.DotColor);
            Pen PnDot = new Pen(_Theme.DotColor);
            PointF[] Pt = new PointF[1];

            int j;
            int TrialCount;

            Shape _Shape;
            GraphicsPath gpShp = new GraphicsPath();
            RectangleF RectRnd = new RectangleF();
            Matrix _Matrix = new Matrix();
            StringFormat _StringFormat = new StringFormat();
            _StringFormat.Alignment = StringAlignment.Center;
            _StringFormat.LineAlignment = StringAlignment.Center;
            _StringFormat.FormatFlags = StringFormatFlags.NoClip;

            Theme _ATheme = m_Themes.GetActiveTheme();
            bool bLayerVisibility;

            g.SmoothingMode = SmoothingMode.AntiAlias;
            //*** Traverse Layers collection
            foreach (Layer _Layer in Layers)
            {
                if (DotValue == 0)
                    break;

                if ((_ATheme == null))
                {
                    bLayerVisibility = _Layer.Visible;
                }
                else
                {
                    bLayerVisibility = (bool)(_ATheme.LayerVisibility[_Layer.ID]);
                }
                //*** Consider polygon layers lying within current map extent and visibility is on
                if (_Layer.LayerType == ShapeType.Polygon & _Layer.Extent.IntersectsWith(CurExt) & bLayerVisibility)
                {
                    //Render layer only if it lies within current map extent
                    Hashtable ht = _Layer.GetRecords(_Layer.LayerPath + "\\" + _Layer.ID);
                    IDictionaryEnumerator dicEnumerator = ht.GetEnumerator();
                    //*** Traverse each Shape of the layer
                    while (dicEnumerator.MoveNext())
                    {
                        _Shape = (Shape)dicEnumerator.Value;
                        //*** Consider shape only if it lies within current map extent
                        if (_Shape.Extent.IntersectsWith(CurExt))
                        {
                            if (_Theme.AreaIndexes.ContainsKey(_Shape.AreaId))
                            {
                                DotCount = (int)((double)((AreaInfo)_Theme.AreaIndexes[_Shape.AreaId]).DataValue / DotValue);
                                Region Rgn = new Region();
                                RectangleF[] Rect;
                                PointF[] Vertex = new PointF[3];
                                //*** for triangle
                                object MarkerSize = Math.Sqrt(3) / 4 * DotSize;
                                //*** for triangle

                                if (DotCount > 0)
                                {
                                    gpShp.Reset();
                                    for (j = 0; j <= _Shape.Parts.Count - 1; j++)
                                    {
                                        gpShp.AddPolygon((PointF[])_Shape.Parts[j]);
                                    }
                                    Rgn = new Region(gpShp);
                                    //Rgn.Intersect(CurExt)
                                    Rgn.Transform(mTransMatrix);
                                    Rect = Rgn.GetRegionScans(_Matrix);
                                    //http://www.dotnet247.com/247reference/a.aspx?u=http://www.bobpowell.net/gdiplus_faq.htm
                                    //Rect.Sort(Rect)
                                }
                                else
                                {
                                    Rect = new RectangleF[0];   // Rect[] length is set to 0 because Rect.length was required in If condition below.
                                }

                                //*** Draw random dots inside region
                                j = 1;
                                TrialCount = 1;

                                //*** Bugfix / Enhancement 19 Jun 2006 Distribution of Dots
                                while (j <= DotCount)
                                {
                                    if (DotCount == 0)
                                        break;

                                    if (Rect.Length == 0)
                                        break;

                                    VBMath.Randomize();

                                    try
                                    {
                                        if (Rect.Length / 2 > j + 1)
                                        {
                                            if ((double)j / 2 == (int)(j / 2))  //Math.Round((double)(j / 2)))
                                            {
                                                if ((double)j / 8 == Math.Round((double)(j / 8)))
                                                {
                                                    RectRnd = Rect[j];
                                                }
                                                else
                                                {
                                                    RectRnd = Rect[(int)(Rect.Length / 2) - j];
                                                }
                                            }
                                            else
                                            {
                                                if ((double)j / 6 == Math.Round((double)(j / 6)))
                                                {
                                                    RectRnd = Rect[Rect.Length - j];
                                                }
                                                else
                                                {
                                                    RectRnd = Rect[(int)(Rect.Length / 2) + j];
                                                }
                                            }
                                        }
                                        else
                                        {
                                            RectRnd = Rect[(int)(VBMath.Rnd() * (Rect.Length - 1))];
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.Write(ex.Message);
                                    }

                                    Pt[0].X = RectRnd.X + RectRnd.Width * (float)(0.6 * (float)VBMath.Rnd() + 0.2);
                                    Pt[0].Y = RectRnd.Y - RectRnd.Height / 2;
                                    //* Rnd()

                                    if (Rgn.IsVisible(Pt[0]))
                                    {
                                        switch (_Theme.DotStyle)
                                        {
                                            case MarkerStyle.Circle:
                                                g.FillEllipse(BrDot, (int)(Pt[0].X - DotSize / 2), (int)(Pt[0].Y - DotSize / 2), (int)DotSize, (int)DotSize);
                                                break;
                                            case MarkerStyle.Square:
                                                g.FillRectangle(BrDot, (int)(Pt[0].X - DotSize / 2), (int)(Pt[0].Y - DotSize / 2), (int)DotSize, (int)DotSize);
                                                break;
                                            case MarkerStyle.Triangle:
                                                Vertex[0] = new PointF(Pt[0].X - float.Parse(MarkerSize.ToString()), Pt[0].Y + float.Parse(MarkerSize.ToString()));
                                                Vertex[1] = new PointF(Pt[0].X + float.Parse(MarkerSize.ToString()), Pt[0].Y + float.Parse(MarkerSize.ToString()));
                                                Vertex[2] = new PointF(Pt[0].X, Pt[0].Y - float.Parse(MarkerSize.ToString()));
                                                g.FillPolygon(BrDot, Vertex);
                                                break;
                                            case MarkerStyle.Cross:
                                                g.DrawLine(PnDot, (int)(Pt[0].X - DotSize / 2), (int)Pt[0].Y, (int)(Pt[0].X + DotSize / 2), (int)Pt[0].Y);
                                                g.DrawLine(PnDot, (int)Pt[0].X, (int)(Pt[0].Y + DotSize / 2), (int)Pt[0].X, (int)(Pt[0].Y - DotSize / 2));
                                                break;
                                            case MarkerStyle.Custom:
                                                g.DrawString(_Theme.DotChar.ToString(), _Theme.DotFont, BrDot, Pt[0].X, Pt[0].Y, _StringFormat);
                                                break;
                                        }
                                        j = j + 1;
                                    }

                                    if (TrialCount > DotCount + 2)
                                        break;

                                    TrialCount += 1;
                                }
                                Rgn.Dispose();
                                Rect = null;
                            }
                        }
                    }
                    ht = null;
                    dicEnumerator = null;
                }
                // _Layer = null;
            }
            g.SmoothingMode = SmoothingMode.None;

            _Matrix = null;
            // RectRnd = null;
            Pt = null;
            // _Layer = null;
            _Shape = null;
            BrDot.Dispose();
            PnDot.Dispose();
            gpShp.Dispose();
            _StringFormat.Dispose();
        }
Exemplo n.º 21
0
		public void Curve_GetRegionScans ()
		{
			Point[] points = new Point[2] { new Point (-4194304, -4194304), new Point (4194304, 4194304) };
			GraphicsPath gp = new GraphicsPath ();
			gp.AddCurve (points);
			Region region = new Region (gp);
			// too big, returns 0
			Assert.AreEqual (0, region.GetRegionScans (matrix).Length, "GetRegionScans");
		}
Exemplo n.º 22
0
        private bool detectLabelOverlap( ZedGraph.GraphPane pane, Graphics g, Bitmap gmap, ZedGraph.TextObj text, out Region textBoundsRegion )
        {
            string shape, coords;
            text.GetCoords( pane, g, 1.0f, out shape, out coords );
            if( shape != "poly" ) throw new InvalidOperationException( "shape must be 'poly'" );
            string[] textBoundsPointStrings = coords.Split( ",".ToCharArray() );
            if( textBoundsPointStrings.Length != 9 ) throw new InvalidOperationException( "coords length must be 8" );
            Point[] textBoundsPoints = new Point[]
                        {
                            new Point( Convert.ToInt32(textBoundsPointStrings[0]), Convert.ToInt32(textBoundsPointStrings[1])),
                            new Point( Convert.ToInt32(textBoundsPointStrings[2]), Convert.ToInt32(textBoundsPointStrings[3])),
                            new Point( Convert.ToInt32(textBoundsPointStrings[4]), Convert.ToInt32(textBoundsPointStrings[5])),
                            new Point( Convert.ToInt32(textBoundsPointStrings[6]), Convert.ToInt32(textBoundsPointStrings[7]))
                        };
            byte[] textBoundsPointTypes = new byte[]
                        {
                            (byte) PathPointType.Start,
                            (byte) PathPointType.Line,
                            (byte) PathPointType.Line,
                            (byte) PathPointType.Line
                        };
            GraphicsPath textBoundsPath = new GraphicsPath( textBoundsPoints, textBoundsPointTypes );
            textBoundsPath.CloseFigure();
            textBoundsRegion = new Region( textBoundsPath );
            textBoundsRegion.Intersect( pane.Chart.Rect );
            RectangleF[] textBoundsRectangles = textBoundsRegion.GetRegionScans( g.Transform );

            for( int j = 0; j < textBoundsRectangles.Length; ++j )
            {
                if( g.Clip.IsVisible( textBoundsRectangles[j] ) )
                    return true;

                for( float y = textBoundsRectangles[j].Top; y <= textBoundsRectangles[j].Bottom ; ++y )
                    for( float x = textBoundsRectangles[j].Left; x <= textBoundsRectangles[j].Right; ++x )
                        if( gmap.GetPixel( Convert.ToInt32( x ), Convert.ToInt32( y ) ).ToArgb() != pane.Chart.Fill.Color.ToArgb() )
                            return true;
            }
            return false;
        }
Exemplo n.º 23
0
		public void SmallUnion_Self1 ()
		{
			Region region = new Region (sp1);
			region.Union (sp1);
			CompareSmallRegion (region, self1, 7, 7);

			RectangleF[] scans = region.GetRegionScans (matrix);
			Assert.AreEqual (1, scans.Length, "GetRegionScans");
			CheckRectF ("[0]", 0, 0, 3, 3, scans[0]);
		}
Exemplo n.º 24
0
		public void SmallUnion1 ()
		{
			Region region = new Region (sp1);
			region.Union (sp2);
			CompareSmallRegion (region, sunion, 7, 7);

			RectangleF[] scans = region.GetRegionScans (matrix);
			Assert.AreEqual (3, scans.Length, "GetRegionScans");
			CheckRectF ("[0]", 0, 0, 3, 2, scans[0]);
			CheckRectF ("[1]", 0, 2, 5, 1, scans[1]);
			CheckRectF ("[2]", 2, 3, 3, 2, scans[2]);
		}
Exemplo n.º 25
0
		public void TestComplementGroup1 ()
		{
			RectangleF[] rects;
			Region r1  = new Region ();
			Region r2 = new Region ();
			Rectangle rect1 = Rectangle.Empty;
			Rectangle rect2 = Rectangle.Empty;
			Rectangle rect3 = Rectangle.Empty;
			Rectangle rect4 = Rectangle.Empty;
			Rectangle rect5 = Rectangle.Empty;
			Rectangle rect6 = Rectangle.Empty;
			Rectangle rect7 = Rectangle.Empty;


			{ // TEST1

				rect1 = new Rectangle (20, 20, 20, 20);
				rect2 = new Rectangle (20, 80, 20, 10);
				rect3 = new Rectangle (60, 60, 30, 10);

				r1 = new Region (rect1);
				r2 = new Region (rect2);
				r2.Union (rect3);
				r1.Complement (r2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (2, rects.Length, "TCG1Test1");
				AssertEqualRectangles (new RectangleF (60, 60, 30, 10), rects[0], "TCG1Test2");
				AssertEqualRectangles (new RectangleF (20, 80, 20, 10), rects[1], "TCG1Test3");
			}


			{ // TEST2

				rect1 = new Rectangle (10, 10, 100, 100);
				rect2 = new Rectangle (40, 60, 100, 20);

				r1 = new Region (rect1);
				r1.Complement (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (1, rects.Length, "TCG2Test1");
				AssertEqualRectangles (new RectangleF (110, 60, 30, 20), rects[0], "TCG2Test2");
			}

			{ // TEST3

				rect1 = new Rectangle (70, 10, 100, 100);
				rect2 = new Rectangle (40, 60, 100, 20);

				r1 = new Region (rect1);
				r1.Complement (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (1, rects.Length, "TCG3Test1");
				AssertEqualRectangles (new RectangleF (40, 60, 30, 20), rects[0], "TCG3Test2");
			}

			{ // TEST4

				rect1 = new Rectangle (40, 100, 100, 100);
				rect2 = new Rectangle (70, 80, 50, 40);

				r1 = new Region (rect1);
				r1.Complement (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (1, rects.Length, "TCG4Test1");
				AssertEqualRectangles (new RectangleF (70, 80, 50, 20), rects[0], "TCG4Test2");
			}

			{ // TEST5

				rect1 = new Rectangle (40, 10, 100, 100);
				rect2 = new Rectangle (70, 80, 50, 40);

				r1 = new Region (rect1);
				r1.Complement (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (1, rects.Length, "TCG5Test1");
				AssertEqualRectangles (new RectangleF (70, 110, 50, 10), rects[0], "TCG5Test2");
			}

			{ // TEST6: Multiple regions

				rect1 = new Rectangle (30, 30, 80, 80);
				rect2 = new Rectangle (45, 45, 200, 200);
				rect3 = new Rectangle (160, 260, 10, 10);
				rect4 = new Rectangle (170, 260, 10, 10);

				r1 = new Region (rect1);
				r1.Complement (rect2);
				r1.Complement (rect3);
				r1.Complement (rect4);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (1, rects.Length, "TCG6Test1");
				AssertEqualRectangles (new RectangleF (170, 260, 10, 10), rects[0], "TCG6Test2");
			}

		}
Exemplo n.º 26
0
		public void SmallIntersection_Self2 ()
		{
			Region region = new Region (sp2);
			region.Intersect (sp2);
			CompareSmallRegion (region, self2, 7, 7);

			RectangleF[] scans = region.GetRegionScans (matrix);
			Assert.AreEqual (1, scans.Length, "GetRegionScans");
			CheckRectF ("[0]", 2, 2, 3, 3, scans[0]);
		}
Exemplo n.º 27
0
		public void TestComplementGroup2 ()
		{

			Bitmap bmp = new Bitmap (600, 800);
			Graphics dc = Graphics.FromImage (bmp);
			Matrix matrix = new Matrix ();
			Rectangle rect1, rect2;
			Region rgn1, rgn2;
			RectangleF [] rects;

			rect1 = new Rectangle (20, 30, 60, 80);
			rect2 = new Rectangle (50, 40, 60, 80);
			rgn1 = new Region (rect1);
			rgn2 = new Region (rect2);
			dc.DrawRectangle (Pens.Green, rect1);
			dc.DrawRectangle (Pens.Red, rect2);
			rgn1.Complement (rgn2);
			dc.FillRegion (Brushes.Blue, rgn1);
			dc.DrawRectangles (Pens.Yellow, rgn1.GetRegionScans (matrix));

			rects = rgn1.GetRegionScans (matrix);

			Assert.AreEqual (2, rects.Length);

			Assert.AreEqual (80, rects[0].X);
			Assert.AreEqual (40, rects[0].Y);
			Assert.AreEqual (30, rects[0].Width);
			Assert.AreEqual (70, rects[0].Height);

			Assert.AreEqual (50, rects[1].X);
			Assert.AreEqual (110, rects[1].Y);
			Assert.AreEqual (60, rects[1].Width);
			Assert.AreEqual (10, rects[1].Height);

		}
Exemplo n.º 28
0
		public void SmallComplement_Self1 ()
		{
			Region region = new Region (sp1);
			region.Complement (sp1);
			CompareSmallRegion (region, sempty, 7, 7);

			RectangleF[] scans = region.GetRegionScans (matrix);
			Assert.AreEqual (0, scans.Length, "GetRegionScans");
		}
Exemplo n.º 29
0
		public void TestExcludeGroup1 ()
		{
			RectangleF[] rects;
			Region r1  = new Region ();
			Region r2 = new Region ();
			Rectangle rect1 = Rectangle.Empty;
			Rectangle rect2 = Rectangle.Empty;
			Rectangle rect3 = Rectangle.Empty;
			Rectangle rect4 = Rectangle.Empty;
			Rectangle rect5 = Rectangle.Empty;
			Rectangle rect6 = Rectangle.Empty;
			Rectangle rect7 = Rectangle.Empty;


			{ // TEST1: Not intersecting rects. Exclude just adds them

				rect1 = new Rectangle (20, 20, 20, 20);
				rect2 = new Rectangle (20, 80, 20, 10);
				rect3 = new Rectangle (60, 60, 30, 10);

				r1 = new Region (rect1);
				r2 = new Region (rect2);
				r2.Union (rect3);
				r1.Exclude (r2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (1, rects.Length, "TEG1Test1");
				AssertEqualRectangles (new RectangleF (20, 20, 20, 20), rects[0], "TEG1Test2");
			}

			{ // TEST2: Excluding from the right
				 /*
				 *  -----------
				 *  |         |
				 *  |     |-------- |
				 *  |     |         |
				 *  |     |-------- |
				 *  |	      |
				 *  ----------|
				 *
				 */

				rect1 = new Rectangle (10, 10, 100, 100);
				rect2 = new Rectangle (40, 60, 100, 20);
				r1 = new Region (rect1);
				r1.Exclude (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (3, rects.Length, "TEG2Test1");
				AssertEqualRectangles (new RectangleF (10, 10, 100, 50), rects[0], "TEG2Test2");
				AssertEqualRectangles (new RectangleF (10, 60, 30, 20), rects[1], "TEG2Test3");
				AssertEqualRectangles (new RectangleF (10, 80, 100, 30), rects[2], "TEG2Test4");
			}


			{ // TEST3: Intersecting from the right
				 /*
				 *  	-----------
				 *  	|         |
				 * |-------- |    |
				 * |         |    |
				 * |-------- |    |
				 *  	|	  |
				 *  	----------|
				 *
				 */

				rect1 = new Rectangle (70, 10, 100, 100);
				rect2 = new Rectangle (40, 60, 100, 20);

				r1 = new Region (rect1);
				r1.Exclude (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (3, rects.Length, "TEG3Test1");
				AssertEqualRectangles (new RectangleF (70, 10, 100, 50), rects[0], "TEG3Test2");
				AssertEqualRectangles (new RectangleF (140, 60, 30, 20), rects[1], "TEG3Test3");
				AssertEqualRectangles (new RectangleF (70, 80, 100, 30), rects[2], "TEG3Test4");
			}


			{ // TEST4: Intersecting from the top
				 /*
				 *  	   -----
				 *  	   |   |
				 *  	-----------
				 *  	|  |   |  |
				 *  	|  -----  |
				 *  	|         |
				 *  	|	  |
				 *  	----------|
				 *
				 */

				rect1 = new Rectangle (40, 100, 100, 100);
				rect2 = new Rectangle (70, 80, 50, 40);

				r1 = new Region (rect1);
				r1.Exclude (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual  (3, rects.Length, "TEG4Test1");
				AssertEqualRectangles (new RectangleF (40, 100, 30, 20), rects[0], "TEG4Test2");
				AssertEqualRectangles (new RectangleF (120, 100, 20, 20), rects[1], "TEG4Test3");
				AssertEqualRectangles (new RectangleF (40, 120, 100, 80), rects[2], "TEG4Test4");
			}


			{ // TEST5: Intersecting from the bottom
				 /*
				 *  	-----------
				 *  	|  	  |
				 *  	|         |
				 *  	|         |
				 *  	|  |   |  |
				 *  	|--|   |--|
				 *	   |   |
				 *  	   -----
				 *
				 */

				rect1 = new Rectangle (40, 10, 100, 100);
				rect2 = new Rectangle (70, 80, 50, 40);

				r1 = new Region (rect1);
				r1.Exclude (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual (3, rects.Length, "TEG5Test1");
				AssertEqualRectangles (new RectangleF (40, 10, 100, 70), rects[0], "TEG5Test2");
				AssertEqualRectangles (new RectangleF (40, 80, 30, 30), rects[1], "TEG5Test3");
				AssertEqualRectangles (new RectangleF (120, 80, 20, 30), rects[2], "TEG5Test4");
			}


			{ // TEST6: Multiple regions

				rect1 = new Rectangle (30, 30, 80, 80);
				rect2 = new Rectangle (45, 45, 200, 200);
				rect3 = new Rectangle (160, 260, 10, 10);
				rect4 = new Rectangle (170, 260, 10, 10);

				r1 = new Region (rect1);
				r1.Exclude (rect2);
				r1.Exclude (rect3);
				r1.Exclude (rect4);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual (2, rects.Length, "TEG6Test1");
				AssertEqualRectangles (new RectangleF (30, 30, 80, 15), rects[0], "TEG6Test2");
				AssertEqualRectangles (new RectangleF (30, 45, 15, 65), rects[1], "TEG6Test3");
			}


			{ // TEST7: Intersecting from the top with a larger rect
				 /*
				 *    -----------------
				 *    |               |
				 *    |	-----------   |
				 *  	|  |   |  |
				 *  	|  -----  |
				 *  	|         |
				 *  	|	  |
				 *  	----------|
				 *
				 */

				rect1 = new Rectangle (50, 100, 100, 100);
				rect2 = new Rectangle (30, 70, 150, 40);

				r1 = new Region (rect1);
				r1.Exclude (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual (1, rects.Length, "TEG7Test1");
				AssertEqualRectangles (new RectangleF (50, 110, 100, 90), rects[0], "TEG7Test2");
			}

			{ // TEST8: Intersecting from the right with a larger rect
				 /*
				 *
				 * |--------|
				 * |	    |
				 * |	-----------
				 * | 	|         |
				 * |	|	  |
				 * |    |	  |
				 * |    |         |
				 * | 	|	  |
				 * | 	----------|
				 * |-------|
				 */

				rect1 = new Rectangle (70, 60, 100, 70);
				rect2 = new Rectangle (40, 10, 100, 150);

				r1 = new Region (rect1);
				r1.Exclude (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual (1, rects.Length, "TEG8Test1");
				AssertEqualRectangles (new RectangleF (140, 60, 30, 70), rects[0], "TEG8Test2");

			}

			{ // TEST9: Intersecting from the left with a larger rect
				 /*
				 *
				 * 		|--------|
				 * 		|	 |
				 * 	-----------      |
				 *  	|         |      |
				 * 	|	  |      |
				 *      |	  |	 |
				 *      |         |	 |
				 *  	|	  |	 |
				 *  	----------|      |
				 * 		|--------|
				 *
				 */


				rect1 = new Rectangle (70, 60, 100, 70);
				rect2 = new Rectangle (100, 10, 100, 150);

				r1 = new Region (rect1);
				r1.Exclude (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual (1, rects.Length, "TEG9Test1");
				AssertEqualRectangles (new RectangleF (70, 60, 30, 70), rects[0], "TEG9Test2");
			}


			{ // TEST10: Intersecting from the bottom with a larger rect
				 /*
				 * *
				 * 		|--------|
				 * 		|	 |
				 * 		|	 |
				 * 		|	 |
				 * 	  --------------------
				 *  	  |        	     |
				 * 	  |  		     |
				 *  	  |------------------|
				 */


				rect1 = new Rectangle (20, 20, 100, 100);
				rect2 = new Rectangle (10, 80, 140, 150);

				r1 = new Region (rect1);
				r1.Exclude (rect2);

				rects = r1.GetRegionScans (new Matrix ());
				Assert.AreEqual (1, rects.Length, "TEG10Test1");
				AssertEqualRectangles (new RectangleF (20, 20, 100, 60), rects[0], "TEG10Test2");
			}


		}
Exemplo n.º 30
0
		public void SmallXor_Self2 ()
		{
			Region region = new Region (sp2);
			region.Xor (sp2);
			CompareSmallRegion (region, sempty, 7, 7);

			RectangleF[] scans = region.GetRegionScans (matrix);
			Assert.AreEqual (0, scans.Length, "GetRegionScans");
		}
Exemplo n.º 31
0
		public void TestExcludeGroup2 ()
		{
			Bitmap bmp = new Bitmap (600, 800);
			Graphics dc = Graphics.FromImage (bmp);
			Matrix matrix = new Matrix ();
			Rectangle rect1, rect2;
			Region rgn1;
			RectangleF [] rects;

			rect1 = new Rectangle (130, 30, 60, 80);
			rect2 = new Rectangle (170, 40, 60, 80);
			rgn1 = new Region (rect1);
			rgn1.Exclude (rect2);
			rects = rgn1.GetRegionScans (matrix);

			Assert.AreEqual (2, rects.Length);

			Assert.AreEqual (130, rects[0].X);
			Assert.AreEqual (30, rects[0].Y);
			Assert.AreEqual (60, rects[0].Width);
			Assert.AreEqual (10, rects[0].Height);

			Assert.AreEqual (130, rects[1].X);
			Assert.AreEqual (40, rects[1].Y);
			Assert.AreEqual (40, rects[1].Width);
			Assert.AreEqual (70, rects[1].Height);
		}