download Region.cs
Language: C#
License: GPL
Copyright: Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts. Copyright 1993, 1994, 1995 Alexandre Julliard Copyright 1998 Huw Davies (c) 1987, 1988 X Consortium (C) 2003 Neil Cawse.
LOC: 851
Project Info
DotGNU Portable.NET(dotgnu-pnet)
Server: Savannah GNU
Type: cvs
...net\pnetlib\System.Drawing\
   .cvsignore
   Bitmap.cs
   Brush.cs
   Brushes.cs
   CharacterRange.cs
   Color.cs
   ColorConverter.cs
   ColorTranslator.cs
   ContentAlignment.cs
   Font.cs
   FontConverter.cs
   FontFamily.cs
   FontStyle.cs
   Graphics.cs
   GraphicsUnit.cs
   Icon.cs
   IconConverter.cs
   Image.cs
   ImageAnimator.cs
   ImageConverter.cs
   ImageFormatConverter.cs
   KnownColor.cs
   Makefile.am
   Pen.cs
   Pens.cs
   Point.cs
   PointConverter.cs
   PointF.cs
   Rectangle.cs
   RectangleConverter.cs
   RectangleF.cs
   Region.cs
   RotateFlipType.cs
   S.cs
   Size.cs
   SizeConverter.cs
   SizeF.cs
   SolidBrush.cs
   StringAlignment.cs
   StringDigitSubstitute.cs
   StringFormat.cs
   StringFormatFlags.cs
   StringTrimming.cs
   StringUnit.cs
   System.Drawing.build
   SystemBrushes.cs
   SystemColors.cs
   SystemIcons.cs
   SystemPens.cs
   TextureBrush.cs
   TODOAttribute.cs
   ToolboxBitmapAttribute.cs
   XorBrush.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
/*
 * Region.cs - Implementation of the "System.Drawing.Region" class.
 *
 * Copyright (C) 2003  Neil Cawse.
 * Based on work from the Wine Project
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * GDI region objects. Shamelessly ripped out from the X11 distribution
 * Thanks for the nice licence.
 *
 * Copyright 1993, 1994, 1995 Alexandre Julliard
 * Modifications and additions: Copyright 1998 Huw Davies
 *                                                           1999 Alex Korobka
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* Copyright (c) 1987, 1988  X Consortium
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *  
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *  
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *  
 * Except as contained in this notice, the name of the X Consortium shall not be
 * used in advertising or otherwise to promote the sale, use or other dealings
 * in this Software without prior written authorization from the X Consortium.
 *  
 * Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
 *  
 * All Rights Reserved
 *  
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and that
 * both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Digital not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *  
 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

namespace System.Drawing
{

using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Drawing.Toolkit;

#if !ECMA_COMPAT
[ComVisible(false)]
#endif
public sealed class Region : MarshalByRefObject, IDisposable
{
	// Internal byte representation of the region
	private RegionData rgnData;
	// Overall extent of the region
	private RectangleF extent;
	// rectangles that make up the region sorted top to bottom, left to right
	private RectangleF[] rects;

	// Constructors.
	public Region()
	{
		rgnData = new RegionData() ;
		MakeInfinite();
	}

	public Region(GraphicsPath path)
	{
		if(path == null)
		{
			throw new ArgumentNullException("path", "Argument cannot be null");
		}
		rgnData = new RegionData( path );
	}

	public Region(Rectangle rect) : this( (RectangleF)rect)
	{}

	public Region(RectangleF rect)
	{
		rgnData = new RegionData( rect );
		if (rect.Width < 0)
			rect = new RectangleF(rect.Right, rect.Top, -rect.Width, rect.Height);
		if (rect.Height < 0)
			rect = new RectangleF(rect.Left, rect.Bottom, rect.Width, -rect.Height);

		if(rect.Width != 0 || rect.Height != 0)
		{
			extent = rect;
			rects = new RectangleF[1];
			rects[0] = rect;
		}
		else
		{
			// extent is already empty(0,0,0,0)
			rects = new RectangleF[0];
		}
	}

	private Region(int initialRectSize)
	{
		rects = new RectangleF[initialRectSize];
	}

	public Region(RegionData otherRgnData)
	{
		if(otherRgnData == null)
		{
			throw new ArgumentNullException("rgnData", "Argument cannot be null");
		}
		Region r = otherRgnData.ConstructRegion ( otherRgnData ) ;
		this.rects = r.rects ;
		this.extent = r.extent ;
		this.rgnData = r.GetRegionData() ;
	}

	// Helpers, to replace missing "Math" class in some profiles.
	private static int Math_Min(int a, int b)
	{
		if(a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	private static float Math_Min(float a, float b)
	{
		if(a < b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	private static int Math_Max(int a, int b)
	{
		if(a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}
	private static float Math_Max(float a, float b)
	{
		if(a > b)
		{
			return a;
		}
		else
		{
			return b;
		}
	}

	// Make an exact copy of this region.
	public Region Clone()
	{
		Region newRegion = new Region();
		newRegion.rgnData = rgnData.Clone();
		newRegion.rects = (RectangleF[])rects.Clone();
		newRegion.extent = extent;
		return newRegion;
	}

	// Form the complement of subtracting this region from another.
	public void Complement(GraphicsPath path)
	{
		Complement ( new Region(path));
	}

	public void Complement(Rectangle rect)
	{
		Complement( new Region(rect));
	}

	public void Complement(RectangleF rect)
	{
		Complement( new Region(rect));
	}

	public void Complement(Region region)
	{
		rgnData.Complement( region ) ;
		Region subtract = Subtract(region, this);
		extent = subtract.extent;
		rects = subtract.rects;
	}

	// Dispose of this region.
	public void Dispose()
	{
		Dispose(true);
		GC.SuppressFinalize(this);
	}

	private void Dispose(bool disposing)
	{
		// Nothing to do in this implementation.
	}

	~Region()
	{
		Dispose(false);
	}

	// Determine if two regions are equal after applying a transformation.
	// TODO transformation
	public bool Equals(Region region, Graphics g)
	{
		if(region == null)
		{
			throw new ArgumentNullException("region", "Argument cannot be null");
		}
		if(g == null)
		{
			throw new ArgumentNullException("g", "Argument cannot be null");
		}

		if (rects.Length == 0)
			return true;
		if (region.rects.Length != rects.Length | region.extent != extent)
			return false;
		for (int i = 0; i < rects.Length; i++)
		{
			if (rects[i] != region.rects[i])
				return false;
		}
		return true;
	}

	// Subtract another region from this one.
	public void Exclude(GraphicsPath path)
	{
		Exclude( new Region(path));
	}
	
	public void Exclude(Rectangle rect)
	{
		Exclude( new Region(rect));
	}

	public void Exclude(RectangleF rect)
	{
		Exclude( new Region(rect));
	}

	public void Exclude(Region region)
	{
		rgnData.Exclude( region ) ;
		Region subtract = Subtract(this, region);
		extent = subtract.extent;
		rects = subtract.rects;
	}

	// Create a region object from a HRGN.
	[TODO]
	public static Region FromHrgn(IntPtr hrgn)
	{
		return new Region();
	}

	// Get the bounds of this region on a particular graphics object.
	public RectangleF GetBounds(Graphics graphics)
	{
		return extent;
	}

	// Get a HRGN object for this region.
	public IntPtr GetHrgn(Graphics g)
	{
		// Not used in this implementation
		return IntPtr.Zero;
	}

	// Get the raw region data for this region.
	public RegionData GetRegionData()
	{
		return rgnData;
	}

	// Get an array of rectangles that represents this region.
	[TODO]
	public RectangleF[] GetRegionScans(Matrix matrix)
	{
		//HACK: this is temporary and will not work on transforms with shear.
		RectangleF[] newRects = new RectangleF[rects.Length];
		for(int i = 0; i < newRects.Length; i++)
		{
			RectangleF r = rects[i];
			float ox, oy, or, ob;
			matrix.TransformPoint(r.X, r.Y, out ox, out oy);
			matrix.TransformPoint(r.Right, r.Bottom, out or, out ob);
			newRects[i] = RectangleF.FromLTRB(ox, oy, or, ob);
		}
		return newRects;
	}

	internal RectangleF[] GetRegionScansIdentity()
	{
		return rects;
	}

	// Form the intersection of this region and another.
	public void Intersect(GraphicsPath path)
	{
		Intersect(new Region(path));
	}
	
	public void Intersect(Rectangle rect)
	{
		Intersect(new Region(rect));
	}
	
	public void Intersect(RectangleF rect)
	{
		Intersect(new Region(rect));
	}
	
	public void Intersect(Region region)
	{
		rgnData.Intersect(region);
		Intersect(this, region);
	}

	// Determine if this region is empty on a particular graphics object.
	public bool IsEmpty(Graphics g)
	{
		// TODO Transformation
		return extent.IsEmpty;
	}

	// Determine if this region is infinite on a particular graphics object.
	[TODO]
	public bool IsInfinite(Graphics g)
	{
		// TODO
		return false;
	}

	// Determine if a point is contained within this region.
	public bool IsVisible(Point point)
	{
		return IsVisible((PointF)point);
	}
	
	public bool IsVisible(PointF point)
	{
		if (extent.Contains( point ))
			for (int i = 0; i < rects.Length; i++)
				if (rects[i].Contains( point))
					return true;
		return false;
	}

	public bool IsVisible(Rectangle rect)
	{
		return IsVisible((RectangleF)rect);
	}
	
	public bool IsVisible(RectangleF rect)
	{
		if (rects.Length > 0 && extent.IntersectsWith( rect))
		{
			for (int i = 0; i < rects.Length; i++)
			{
				RectangleF currentRect = rects[i];
				// Not far enough down yet
				if (currentRect.Bottom <= rect.Top)
					continue;
				// Too far down
				if (currentRect.Top >= rect.Bottom)
					break;
				// Not far over enough yet
				if (currentRect.Right <= rect.Left)
					continue;
				if (currentRect.Left >= rect.Right) 
					continue;
				return true;
			}
		}
		return false;
	}

	public bool IsVisible(Point point, Graphics g)
	{
		return IsVisible((PointF)point, g);
	}
	
	[TODO]
	public bool IsVisible(PointF point, Graphics g)
	{
		// TODO
		return false;
	}

	public bool IsVisible(Rectangle rect, Graphics g)
	{
		return IsVisible((RectangleF)rect, g);
	}
	
	[TODO]
	public bool IsVisible(RectangleF rect, Graphics g)
	{
		// TODO
		return false;
	}

	public bool IsVisible(float x, float y)
	{
		return IsVisible(new PointF(x, y));
	}

	public bool IsVisible(int x, int y, Graphics g)
	{
		return IsVisible(new PointF(x, y), g);
	}

	public bool IsVisible(float x, float y, Graphics g)
	{
		return IsVisible(new PointF(x, y), g);
	}
	
	public bool IsVisible(int x, int y, int width, int height)
	{
		return IsVisible(new RectangleF(x, y, width, height));
	}

	public bool IsVisible(float x, float y, float width, float height)
	{
		return IsVisible(new RectangleF(x, y, width, height));
	}

	public bool IsVisible(int x, int y, int width, int height, Graphics g)
	{
		return IsVisible(new RectangleF(x, y, width, height), g);
	}

	public bool IsVisible(float x, float y, float width,
		float height, Graphics g)
	{
		return IsVisible(new RectangleF(x, y, width, height), g);
	}

	// Make this region empty.
	public void MakeEmpty()
	{
		rgnData.MakeEmpty() ;
		rects = new RectangleF[0];
		extent = Rectangle.Empty;
	}

	// Make this region infinite.
	public void MakeInfinite()
	{
		rgnData.MakeInfinite() ;
		const float maxCoord = 4194304; //Math.Pow(2, 22)
		extent = new RectangleF(-maxCoord, -maxCoord, 2 * maxCoord, 2 * maxCoord);
		rects = new RectangleF[1] { extent };
	}

	// Transform this region using a specified matrix.
	[TODO]
	public void Transform(Matrix matrix)
	{
		rgnData.Transform( matrix ) ;
		// TODO: other state variables
	}

	// Translate this matrix by a specific amount.
	public void Translate(int dx, int dy)
	{
		Translate((float)dx, (float)dy);
	}
	
	public void Translate(float dx, float dy)
	{
		rgnData.Translate( dx, dy ) ;
		for( int i = 0; i < rects.Length; i++)
			rects[i].Offset(dx, dy);
		if (rects.Length>0)
			extent.Offset(dx, dy);
	}

	// Form the union of this region and another.
	public void Union(GraphicsPath path)
	{
		Union( new Region(path));
	}

	public void Union(Rectangle rect)
	{
		Union( new Region(rect));
	}

	public void Union(RectangleF rect)
	{
		Union( new Region(rect));
	}
	
	public void Union(Region region)
	{
		rgnData.Union( region );
		Union (this, region);
	}

	// Union of reg1 and reg2
	public static void Union ( Region reg1, Region reg2)
	{
		if ( reg1 == reg2 | reg2.rects.Length == 0 )
			return;
		if ( reg1.rects.Length == 0 )
		{
			reg1.rects = (RectangleF[])reg2.rects.Clone();
			reg1.extent = reg2.extent;
			return;
		}
		// Region 1 completely subsumes region 2
		RectangleF reg1Extent = reg1.extent;
		RectangleF reg2Extent = reg2.extent;
		if ( reg1.rects.Length == 1 && reg1Extent.Left <= reg2Extent.Left && reg1Extent.Top <= reg2Extent.Top && reg1Extent.Right >= reg2Extent.Right && reg1Extent.Bottom >= reg2Extent.Bottom )
			return;

		// Region 2 completely subsumes region 1
		if ( reg2.rects.Length == 1 && (reg2Extent.Left <= reg1Extent.Left) && reg2Extent.Top <= reg1Extent.Top && reg2Extent.Right >= reg1Extent.Right && reg2Extent.Bottom >= reg1Extent.Bottom )
		{
			reg1.rects = (RectangleF[])reg2.rects.Clone();
			reg1.extent = reg2.extent;
			return;
		}

		Region union = RegionOperation( reg1, reg2, RegionOperationType.Union);
		reg1.rects = union.rects;
		reg1.extent = RectangleF.Union( reg1.extent, reg2.extent);
	}

	// Subtract reg2 from reg1
	private static Region Subtract ( Region reg1, Region reg2)
	{
		RectangleF extent1 = reg1.extent;
		if (reg1.rects.Length==0 || reg2.rects.Length == 0 || !extent1.IntersectsWith(reg2.extent))
		{
			Region region = new Region();
			region.extent = reg1.extent;
			region.rects = (RectangleF[])reg1.rects.Clone();
			return region;
		}
		else
		{
			Region subtract = RegionOperation( reg1, reg2, RegionOperationType.Subtract);
			CalculateExtents(subtract);
			return subtract;
		}
	}

	// Creates a new array and copies when we run out of space. Doubles the size.

	// Intersection of reg1 and reg2
	private static void Intersect ( Region reg1, Region reg2)
	{
		// Trivial case
		RectangleF reg1Extent = reg1.extent;
		if ( reg1.rects.Length == 0 || reg2.rects.Length == 0  || !reg1Extent.IntersectsWith(reg2.extent))
		{
			reg1.MakeEmpty();
			return;
		}
		Region intersect = RegionOperation( reg1, reg2, RegionOperationType.Intersect);
		CalculateExtents(intersect);
		reg1.rects = intersect.rects;
		reg1.extent = intersect.extent;
	}

	private static void AllocateSpace( Region reg, int nextRectangle)
	{
		// If we have run out of space in the array, create a new copy with double the size
		if (reg.rects.Length == nextRectangle)
		{
			RectangleF[] newRects = new RectangleF[reg.rects.Length * 2];
			Array.Copy(reg.rects, newRects, reg.rects.Length);
			reg.rects = newRects;
		}
	}

	// Re-calculate the extents of a region
	private static void CalculateExtents (Region reg)
	{
		if (reg.rects.Length == 0)
		{
			reg.extent = RectangleF.Empty;
			return;
		}

		/* Since rectStart is the first rectangle in the region, it must have the
		 * smallest top and since rectEnd is the last rectangle in the region,
		 * it must have the largest bottom, because of banding.
		 */
		RectangleF rectStart = reg.rects[0];
		float left = rectStart.Left;
		float top = rectStart.Top;
		RectangleF rectEnd = reg.rects[reg.rects.Length-1];
		float right = rectEnd.Right;
		float bottom = rectEnd.Bottom;

		for (int i = 0; i < reg.rects.Length; i++)
		{
			RectangleF rect = reg.rects[i];
			if (rect.Left < left)
				left = rect.Left;
			if (rect.Right > right)
				right = rect.Right;
		}
		reg.extent = RectangleF.FromLTRB(left, top, right, bottom);
		
	}

	/* Handle a non-overlapping band for the union operation. Just
	 * Adds the rectangles into the region. Doesn't have to check for
	 * subsumption or anything.
	 * nextRectangle is incremented and the final rectangles overwritten
	 * with the rectangles we're passed.
	 */
	private static void UnionNonOverlapBands (Region regNew, ref int nextRectangle, Region reg, int rect, int rectEnd, float top, float bottom)
	{
	 
		int nextRect = nextRectangle;

		while (rect != rectEnd)
		{
			AllocateSpace( regNew, nextRectangle);
			RectangleF curRect = reg.rects[rect];
			regNew.rects[nextRect] = RectangleF.FromLTRB(curRect.Left, top, curRect.Right, bottom);
			nextRectangle += 1;
			nextRect++;
			rect++;
		}
	}

	/* Handle an overlapping band for the union operation. Picks the
	 * left-most rectangle each time and merges it into the region.
	 * Rectangles are overwritten in reg.rects and nextRectangle will
	 * be changed.
	 */
	private static void UnionOverlapBands (Region regNew, ref int nextRectangle, Region reg1, int r1, int r1End, Region reg2, int r2, int r2End, float top, float bottom)
	{
		while (r1 != r1End && r2 != r2End)
			if (reg1.rects[r1].Left < reg2.rects[r2].Left)
				UnionOverlapBandsMerge(reg1, ref r1, regNew, ref nextRectangle, top, bottom);
			else
				UnionOverlapBandsMerge(reg2, ref r2, regNew, ref nextRectangle, top, bottom);

		if (r1 != r1End)
			while (r1 != r1End)
				UnionOverlapBandsMerge(reg1, ref r1, regNew, ref nextRectangle, top, bottom);
		else
			while (r2 != r2End)
				 UnionOverlapBandsMerge(reg2, ref r2, regNew, ref nextRectangle, top, bottom);
	}

	private static void UnionOverlapBandsMerge(Region reg, ref int r, Region regNew, ref int nextRectangle, float top, float bottom)
	{
		bool addNew = false;
		
		RectangleF rRect = reg.rects[r];
		if (nextRectangle == 0)
			addNew = true;
		else
		{
			RectangleF rect = regNew.rects[nextRectangle - 1];
			if (nextRectangle != 0 && rect.Top == top && rect.Bottom == bottom && rect.Right >= rRect.Left)
			{
				if (rect.Right < rRect.Right)
					regNew.rects[nextRectangle - 1]= RectangleF.FromLTRB( rect.Left, rect.Top, rRect.Right, rect.Bottom);
			}
			else
				addNew = true;
		}
		if (addNew)
		{
			AllocateSpace( regNew, nextRectangle);
			regNew.rects[nextRectangle] = RectangleF.FromLTRB(rRect.Left, top, rRect.Right, bottom);
			nextRectangle++;
		}
		r++;
	}

	/* Overlapping band subtraction. x1 is the left-most point not yet checked
	 */
	private static void SubtractOverlapBands(Region regNew, ref int nextRectangle, Region reg1, int r1, int r1End, Region reg2, int r2, int r2End, float top, float bottom)
	{
		float left = reg1.rects[r1].Left;
		RectangleF rect1 = Rectangle.Empty;
		RectangleF rect2 = Rectangle.Empty;
		if (r1 != r1End)
			rect1 = reg1.rects[r1];
		if (r2 != r2End)
			rect2 = reg2.rects[r2];
			
		while (r1 != r1End && r2 != r2End)
		{
			if (rect2.Right <= left)
			{
				// Subtrahend missed the boat: go to next subtrahend.
				r2++;
				if (r2 != r2End)
					rect2 = reg2.rects[r2];
			}
			else if (rect2.Left <= left)
			{
				 // Subtrahend preceeds minuend: nuke left edge of minuend.
				left = rect2.Right;
				if (left >= rect1.Right)
				{
					/* Minuend completely covered: advance to next minuend and
					 * reset left fence to edge of new minuend.
					 */
					r1++;
					if (r1 != r1End)
					{
						rect1 = reg1.rects[r1];
						left = rect1.Left;
					}				
				}
				else
				{
					/* Subtrahend now used up since it doesn't extend beyond
					 * minuend
					 */
					r2++;
					if (r2 != r2End)
						rect2 = reg2.rects[r2];
				}
			}
			else if (rect2.Left < rect1.Right)
			{
				/* Left part of subtrahend covers part of minuend: add uncovered
				 * part of minuend to region and skip to next subtrahend.
				 */
				AllocateSpace(regNew, nextRectangle);
				regNew.rects[nextRectangle++] = RectangleF.FromLTRB(left, top, rect2.Left, bottom);
				left = rect2.Right;
				if (left >= rect1.Right)
				{
					// Minuend used up: advance to new...
					r1++;
					if (r1 != r1End)
					{
						rect1 = reg1.rects[r1];
						left = rect1.Left;
					}
				}
				else
				{
					// Subtrahend used up
					r2++;
					if (r2 != r2End)
						rect2 = reg2.rects[r2];
				}
			}
			else
			{
				// Minuend used up: add any remaining piece before advancing.
				if (rect1.Right > left)
				{
					AllocateSpace(regNew, nextRectangle);
					regNew.rects[nextRectangle++] = RectangleF.FromLTRB(left, top, rect1.Right, bottom);
				}
				r1++;
				if (r1 != r1End)
				{
					rect1 = reg1.rects[r1];
					left = rect1.Left;
				}
			}
		}

		// Add remaining minuend rectangles to region.
		while (r1 != r1End)
		{
			AllocateSpace(regNew, nextRectangle);
			regNew.rects[nextRectangle++] = RectangleF.FromLTRB(left, top, rect1.Right, bottom);
			r1++;
			if (r1 != r1End)
			{
				rect1 = reg1.rects[r1];
				left = rect1.Left;
			}
		}
	}

	/* Deal with non-overlapping band for subtraction. Any parts from
	 * region 2 we discard. Anything from region 1 we add to the region.
	 */
	private static void SubtractNonOverlapBands(Region regNew, ref int nextRectangle, Region reg, int r, int rEnd, float top, float bottom)
	{
		while (r != rEnd)
		{
			AllocateSpace(regNew, nextRectangle);
			RectangleF rect = reg.rects[r];
			regNew.rects[nextRectangle++] = RectangleF.FromLTRB(rect.Left, top, rect.Right, bottom);
			r++;
		}
	}

	/* Handle an overlapping band for REGION_Intersect.
	 * Rectangles may be added to the region.
	 */
	private static void IntersectOverlapBands(Region regNew, ref int nextRectangle, Region reg1, int r1, int r1End, Region reg2, int r2, int r2End, float top, float bottom)
	{
		float left, right;

		while (r1 != r1End && r2 != r2End)
		{
			RectangleF rect1 = reg1.rects[r1];
			RectangleF rect2 = reg2.rects[r2];
			left = Math_Max(rect1.Left, rect2.Left);
			right =	Math_Min(rect1.Right, rect2.Right);

			/*
			 * If there's any overlap between the two rectangles, add that
			 * overlap to the new region.
			 * There's no need to check for subsumption because the only way
			 * such a need could arise is if some region has two rectangles
			 * right next to each other. Since that should never happen...
			 */
			if (left < right)
			{
				AllocateSpace(regNew, nextRectangle);
				regNew.rects[nextRectangle++] = RectangleF.FromLTRB(left, top, right, bottom);
			}

			/*
			 * Need to advance the pointers. Shift the one that extends
			 * to the right the least, since the other still has a chance to
			 * overlap with that region's next rectangle, if you see what I mean.
			 */
			if (rect1.Right < rect2.Right)
				r1++;
			else if (rect2.Right < rect1.Right)
				r2++;
			else
			{
				r1++;
				r2++;
			}
		}
	}

	/* Attempt to merge the rects in the current band with those in the
	 * previous one. Used only by RegionOperation.
	 * Results: The new index for the previous band.
	 *
	 * If coalescing takes place:
	 * - rectangles in the previous band will have their bottom fields
	 * altered.
	 * - nextRectangle will be decreased.
	 */
	private static int CoalesceRegion ( Region reg, ref int nextRectangle,
		int prevStart,  /* Index of start of previous band */
		int curStart    /* Index of start of current band */
		)
	{
	
		int curNumRects; /* Number of rectangles in current band */
		int regEnd = nextRectangle; /* End of region */
		int prevRect = prevStart; /* Current rect in previous band */
		int prevNumRects = curStart - prevStart; /* Number of rectangles in previous band */
		int curRect = curStart; /* Current rect in current band */
		float bandtop = reg.rects[curRect].Top; /* top coordinate for current band */
    
		/* Figure out how many rectangles are in the current band. Have to do
		 * this because multiple bands could have been added in REGION_RegionOp
		 * at the end when one region has been exhausted.
		 */
		for (curNumRects = 0; curRect != regEnd && reg.rects[curRect].Top == bandtop; curNumRects++)
			curRect++;

		if (curRect != regEnd)
		{
			/*
			 * If more than one band was added, we have to find the start
			 * of the last band added so the next coalescing job can start
			 * at the right place... (given when multiple bands are added,
			 * this may be pointless -- see above).
			 */
			regEnd--;
			while (reg.rects[regEnd -1].Top == reg.rects[regEnd].Top)
				regEnd--;
			curStart = regEnd;
			regEnd = nextRectangle;
		}

		if (curNumRects == prevNumRects && curNumRects != 0)
		{
			curRect -= curNumRects;
			/*
			 * The bands may only be coalesced if the bottom of the previous
			 * matches the top scanline of the current.
			 */
			if (reg.rects[prevRect].Bottom == reg.rects[curRect].Top)
			{
				/*
				 * Make sure the bands have rects in the same places. This
				 * assumes that rects have been added in such a way that they
				 * cover the most area possible. I.e. two rects in a band must
				 * have some horizontal space between them.
				 */
				do
				{
					if (reg.rects[prevRect].Left != reg.rects[curRect].Left ||
						reg.rects[prevRect].Right != reg.rects[curRect].Right)
					{
						/*
						 * The bands don't line up so they can't be coalesced.
						 */
						return curStart;
					}
					prevRect++;
					curRect++;
					prevNumRects -= 1;
				}
				while (prevNumRects != 0);

				nextRectangle -= curNumRects;
				curRect -= curNumRects;
				prevRect -= curNumRects;

				/*
				 * The bands may be merged, so set the bottom of each rect
				 * in the previous band to that of the corresponding rect in
				 * the current band.
				 */
				do
				{
					RectangleF previousRectangle = reg.rects[prevRect];
					reg.rects[prevRect] = RectangleF.FromLTRB(previousRectangle.Left, previousRectangle.Top, previousRectangle.Right, reg.rects[curRect].Bottom);
					prevRect++;
					curRect++;
					curNumRects -= 1;
				}
				while (curNumRects != 0);

				/*
				 * If only one band was added to the region, we have to backup
				 * curStart to the start of the previous band.
				 *
				 * If more than one band was added to the region, copy the
				 * other bands down. The assumption here is that the other bands
				 * came from the same region as the current one and no further
				 * coalescing can be done on them since it's all been done
				 * already... curStart is already in the right place.
				 */

				if (curRect == regEnd)
					curStart = prevStart;
				else
				{
					do
					{
						reg.rects[prevRect++] = reg.rects[curRect++];
					}
					while (curRect != regEnd);
				}

			}
		}
		return curStart;
	}

	private enum RegionOperationType
	{
		Intersect,
		Union,
		Subtract
	}
	
	/*      Apply an operation to two regions.
	*
	*      The idea behind this function is to view the two regions as sets.
	*      Together they cover a rectangle of area that this function divides
	*      into horizontal bands where points are covered only by one region
	*      or by both. For the first case, the nonOverlapFunc is called with
	*      each the band and the band's upper and lower extents. For the
	*      second, the overlapFunc is called to process the entire band. It
	*      is responsible for clipping the rectangles in the band, though
	*      this function provides the boundaries.
	*      At the end of each band, the new region is coalesced, if possible,
	*      to reduce the number of rectangles in the region.
	*/

	private static Region RegionOperation( Region reg1, Region reg2, RegionOperationType regionOperationType)
	{
		float ybot; /* Bottom of intersection */
		float ytop; /* Top of intersection */
		int prevBand; /* Index of start of previous band in newReg */
		int curBand; /* Index of start of current band in newReg */
		int r1BandEnd; /* End of current band in r1 */
		int r2BandEnd; /* End of current band in r2 */
		float top; /* Top of non-overlapping band */
		float bot; /* Bottom of non-overlapping band */

		/* Initialization:
		 *  set r1, r2, r1End and r2End appropriately
		 */
		int r1 = 0;
		int r1End = reg1.rects.Length; /* End of 1st region */
		RectangleF reg1Extent = reg1.extent;
		int r2 = 0;
		int r2End = reg2.rects.Length; /* End of 2nd region */
		RectangleF reg2Extent = reg2.extent;

		RectangleF rect1;
		RectangleF rect2;

		/*
		 * Allocate a reasonable number of rectangles for the new region. The idea
		 * is to allocate enough so the individual functions don't need to
		 * reallocate and copy the array, which is time consuming, yet we don't
		 * want to use too much memory.
		 */
		Region newReg = new Region(Math_Max(reg1.rects.Length, reg2.rects.Length) * 2);
		// The total number of rectangles we have written to the array
		int nextRectangle = 0;
    
		/*
		 * Initialize ybot and ytop.
		 * In the upcoming loop, ybot and ytop serve different functions depending
		 * on whether the band being handled is an overlapping or non-overlapping
		 * band.
		 * In the case of a non-overlapping band (only one of the regions
		 * has points in the band), ybot is the bottom of the most recent
		 * intersection and thus clips the top of the rectangles in that band.
		 * ytop is the top of the next intersection between the two regions and
		 * serves to clip the bottom of the rectangles in the current band.
		 *  For an overlapping band (where the two regions intersect), ytop clips
		 * the top of the rectangles of both regions and ybot clips the bottoms.
		 */

		if (reg1Extent.Top < reg2Extent.Top)
			ybot = reg1Extent.Top;
		else
			ybot = reg2Extent.Top;

		/*
		 * prevBand serves to mark the start of the previous band so rectangles
		 * can be coalesced into larger rectangles. qv. miCoalesce, above.
		 * In the beginning, there is no previous band, so prevBand == curBand
		 * (curBand is set later on, of course, but the first band will always
		 * start at index 0). prevBand and curBand must be indices because of
		 * the possible expansion, and resultant moving, of the new region's
		 * array of rectangles.
		 */
		prevBand = 0;

		do
		{
			rect1 = reg1.rects[r1];
			rect2 = reg2.rects[r2];
			curBand = nextRectangle;

			/*
			 * This algorithm proceeds one source-band (as opposed to a
			 * destination band, which is determined by where the two regions
			 * intersect) at a time. r1BandEnd and r2BandEnd serve to mark the
			 * rectangle after the last one in the current band for their
			 * respective regions.
			 */
			r1BandEnd = r1;
			while (r1BandEnd != r1End && reg1.rects[r1BandEnd].Top == rect1.Top)
				r1BandEnd++;

				r2BandEnd = r2;
			while (r2BandEnd != r2End && reg2.rects[r2BandEnd].Top == rect2.Top)
				r2BandEnd++;

			/*
			 * First handle the band that doesn't intersect, if any.
			 *
			 * Note that attention is restricted to one band in the
			 * non-intersecting region at once, so if a region has n
			 * bands between the current position and the next place it overlaps
			 * the other, this entire loop will be passed through n times.
			 */
			if (rect1.Top < rect2.Top)
			{
				top = Math_Max(rect1.Top, ybot);
				bot = Math_Min(rect1.Bottom, rect2.Top);

				if ((top != bot))
				{
					if (regionOperationType == RegionOperationType.Subtract)
						SubtractNonOverlapBands(newReg, ref nextRectangle, reg1, r1, r1BandEnd, top, bot);
					else if (regionOperationType == RegionOperationType.Union)
						UnionNonOverlapBands(newReg, ref nextRectangle, reg1, r1, r1BandEnd, top, bot);
				}

				ytop = rect2.Top;
			}
			else if (rect2.Top < rect1.Top)
			{
				top = Math_Max(rect2.Top, ybot);
				bot = Math_Min(rect2.Bottom, rect1.Top);

				if (top != bot)
				{
					if (regionOperationType == RegionOperationType.Union)
						UnionNonOverlapBands(newReg, ref nextRectangle, reg2, r2, r2BandEnd, top, bot);
				}

				ytop = rect1.Top;
			}
			else
				ytop = rect1.Top;

			/*
			 * If any rectangles got added to the region, try and coalesce them
			 * with rectangles from the previous band. Note we could just do
			 * this test in miCoalesce, but some machines incur a not
			 * inconsiderable cost for function calls, so...
			 */
			if (nextRectangle != curBand)
			{
				prevBand = CoalesceRegion(newReg, ref nextRectangle, prevBand, curBand);
			}

			/*
			 * Now see if we've hit an intersecting band. The two bands only
			 * intersect if ybot > ytop
			 */
			ybot = Math_Min(rect1.Bottom, rect2.Bottom);
			curBand = nextRectangle;
			if (ybot > ytop)
			{
				if (regionOperationType == RegionOperationType.Subtract)
					SubtractOverlapBands(newReg, ref nextRectangle, reg1, r1, r1BandEnd, reg2, r2, r2BandEnd, ytop, ybot);
				else if (regionOperationType == RegionOperationType.Union)
					UnionOverlapBands(newReg, ref nextRectangle, reg1, r1, r1BandEnd, reg2, r2, r2BandEnd, ytop, ybot);
				else if (regionOperationType == RegionOperationType.Intersect)
					IntersectOverlapBands(newReg, ref nextRectangle, reg1, r1, r1BandEnd, reg2, r2, r2BandEnd, ytop, ybot);
			}

			if (nextRectangle != curBand)
				prevBand = CoalesceRegion (newReg, ref nextRectangle, prevBand, curBand);

			/*
			 * If we've finished with a band (bottom == ybot) we skip forward
			 * in the region to the next band.
			 */
			if (rect1.Bottom == ybot)
				r1 = r1BandEnd;
			if (rect2.Bottom == ybot)
				r2 = r2BandEnd;
		}
		while (r1 != r1End && r2 != r2End);

		/*
		 * Deal with whichever region still has rectangles left.
		 */
		curBand = nextRectangle;
		if (r1 != r1End)
		{
			if (regionOperationType == RegionOperationType.Subtract || regionOperationType == RegionOperationType.Union)
			{
				do
				{
					rect1 = reg1.rects[r1];
					r1BandEnd = r1;
					while (r1BandEnd < r1End && reg1.rects[r1BandEnd].Top == rect1.Top)
						r1BandEnd++;
					if (regionOperationType == RegionOperationType.Subtract)
						SubtractNonOverlapBands(newReg, ref nextRectangle, reg1, r1, r1BandEnd, Math_Max(rect1.Top, ybot), rect1.Bottom);
					else if (regionOperationType == RegionOperationType.Union)
						UnionNonOverlapBands(newReg, ref nextRectangle, reg1, r1, r1BandEnd, Math_Max(rect1.Top, ybot), rect1.Bottom);
					r1 = r1BandEnd;
				}
				while (r1 != r1End);
			}
		}
		else if (r2 != r2End && regionOperationType == RegionOperationType.Union)
		{
			do
			{
				rect2 = reg2.rects[r2];
				r2BandEnd = r2;
				while (r2BandEnd < r2End && reg2.rects[r2BandEnd].Top == rect2.Top)
					r2BandEnd++;
				UnionNonOverlapBands(newReg, ref nextRectangle, reg2, r2, r2BandEnd, Math_Max(rect2.Top, ybot), rect2.Bottom);
				r2 = r2BandEnd;
			} while (r2 != r2End);
		}

		if (nextRectangle != curBand)
			CoalesceRegion (newReg, ref nextRectangle, prevBand, curBand);

		/*
		 * A bit of cleanup. To keep regions from growing without bound,
		 * we shrink the array of rectangles to match the new number of
		 * rectangles in the region. This never goes to 0, however...
		 *
		 * Only do this stuff if the number of rectangles allocated is more than
		 * twice the number of rectangles in the region (a simple optimization...).
		 */
		if (nextRectangle < newReg.rects.Length)
		{
			RectangleF[] newRects = new RectangleF[nextRectangle];
			Array.Copy(newReg.rects, newRects, nextRectangle);
			newReg.rects = newRects;
		}
		return newReg;
	}


	// Form the XOR of this region and another.
	public void Xor(GraphicsPath path)
	{
		Xor(new Region(path));
	}
	
	public void Xor(Rectangle rect)
	{
		Xor(new Region(rect));
	}
	
	public void Xor(RectangleF rect)
	{
		Xor(new Region(rect));
	}
	
	public void Xor(Region region)
	{
		rgnData.Xor( region ) ;
		Region reg1 = Subtract(this, region);
		Union(reg1, Subtract(region, this));
		extent = reg1.extent;
		rects = reg1.rects;
	}

}; // class Region

}; // namespace System.Drawing

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us