aboutsummaryrefslogtreecommitdiffstatsci
path: root/Backends/VkvgBackend/src/Region.cs
blob: b02eacf4dd72ebfe6e525a254e7ddfb13ee675c1 (plain)
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
// Copyright (c) 2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)

using System.Collections.Generic;
using System;
using Drawing2D;
using System.Diagnostics.CodeAnalysis;

namespace Crow.VkvgBackend {
	public class Region : IRegion
	{
		Rectangle _bounds;
		bool boundsUpToDate = true;
		public List<Rectangle> list = new List<Rectangle>();
		public int count => list.Count;

		public void AddRectangle(Rectangle r)
		{
			if (r == default)
				return;
			if (DoesNotContains (r)) {
				list.Add (r);
				boundsUpToDate = false;
			}
		}
		public bool DoesNotContains(Rectangle r)
		{
			foreach (Rectangle rInList in list)
				if (rInList.ContainsOrIsEqual(r))
					return false;
			return true;
		}
		public bool intersect(Rectangle r)
		{
			foreach (Rectangle rInList in list)
				if (rInList.Intersect(r))
					return true;
			return false;
		}
		public void stroke(IContext ctx, Color c)
		{
			foreach (Rectangle r in list)
				ctx.Rectangle(r);

			ctx.SetSource(c);

			ctx.LineWidth = 2;
			ctx.Stroke ();
		}
		public void clearAndClip(IContext ctx)
		{
			if (list.Count == 0)
				return;
			foreach (Rectangle r in list)
				ctx.Rectangle(r);

			ctx.ClipPreserve();
			ctx.Operator = Operator.Clear;
			ctx.Fill();
			ctx.Operator = Operator.Over;
		}

		public void clip(IContext ctx)
		{
			foreach (Rectangle r in list)
				ctx.Rectangle(r);

			ctx.Clip();
		}
		public Rectangle Bounds {
			get {
				if (!boundsUpToDate) {
					if (list.Count > 0) {
						_bounds = list [0];
						for (int i = 1; i < list.Count; i++) {
							_bounds += list [i];
						}
					} else
						_bounds = default;
					boundsUpToDate = true;
				}
				return _bounds;
			}
		}
		public void clear(IContext ctx)
		{
			foreach (Rectangle r in list)
				ctx.Rectangle(r);
			ctx.Operator = Operator.Clear;
			ctx.Fill();
			ctx.Operator = Operator.Over;
		}

		#region  IRegion implemenatation
		public bool IsEmpty => list.Count == 0;
		public int NumRectangles => list.Count;
		public Rectangle GetRectangle(int i) => list[i];
		public void UnionRectangle (Rectangle r) {
			/*if (r == default)
				System.Diagnostics.Debugger.Break ();*/
			AddRectangle (r);
		}
		public bool OverlapOut (Rectangle r) {
			foreach (Rectangle rInList in list)
				if (rInList.Intersect(r))
					return false;
			return true;
		}
		public RegionOverlap Contains(Rectangle rectangle)
		{
			throw new NotImplementedException();
		}
		public void Reset()
		{
			list = new List<Rectangle>();
			_bounds = default;
			boundsUpToDate = true;
		}

		public bool Equals([AllowNull] IRegion other)
			=> other is Region r ? Bounds.Equals (r.Bounds) : false;
		#endregion

		public override string ToString ()
		{
			string tmp = "";
			foreach (Rectangle r in list) {
				tmp += r.ToString ();
			}
			return tmp;
		}

		public void Dispose()
		{

		}

	}
}