aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/Rectangles.cs
blob: 5168f3dbc69e40c49a484897b168942a1c852ac1 (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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cairo;
using System.Diagnostics;

namespace Crow
{
    public class Rectangles
    {
        public List<Rectangle> list = new List<Rectangle>();
        public int count
        {
            get { return list.Count; }
        }

        public void AddRectangle(Rectangle r)
        {
			if (doesNotContain (r)) {
				list.Add (r);
				boundsUpToDate = false;
			}
        }
        public void Reset()
        {
            list = new List<Rectangle>();
			_bounds = Rectangle.Empty;
			boundsUpToDate = true;
        }
        bool doesNotContain(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 Rectangles intersectingRects(Rectangle r)
        {
            Rectangles tmp = new Rectangles();

            foreach (Rectangle rInList in list)
                if (rInList.Intersect(r))
                    tmp.list.Add(rInList);//on bypass le test déjà fait a l'ajout intial du rect dans la liste

            return tmp;
        }
        public Rectangles SmallerContainedRects(Rectangle r)
        {
            Rectangles tmp = new Rectangles();

            foreach (Rectangle rInList in list)
                if (r.ContainsOrIsEqual(rInList) && rInList.Size < r.Size)
                    tmp.list.Add(rInList);

            return tmp;
        }
		/// <summary>
		/// Return rectangles with size smaller than r.size
		/// </summary>
        public Rectangles SmallerRects(Rectangle r)
        {
            Rectangles tmp = new Rectangles();

            foreach (Rectangle rInList in list)
                if (rInList.Size < r.Size)
                    tmp.list.Add(rInList);

            return tmp;
        }
        public Rectangles containedOrEqualRects(Rectangle r)
        {
            Rectangles tmp = new Rectangles();

            foreach (Rectangle rInList in list)
				if (r.ContainsOrIsEqual(rInList))// && rInList.Size <= r.Size)
                    tmp.list.Add(rInList);

            return tmp;
        }
		public void Srcoll(GraphicObject w)
		{
			Scroller sw = w as Scroller;
			if (sw == null)
				return;

			List<Rectangle> newList = new List<Rectangle>();

			foreach (Rectangle rInList in list)
			{
				Rectangle r = rInList;

				if (sw.VerticalScrolling)
					r.Top -= (int)sw.ScrollY;
				if (sw.HorizontalScrolling)
					r.Left -= (int)sw.ScrollX;

				newList.Add(r);
			}
			list = newList;        
		}
        public void Rebase(GraphicObject w)
        {
			Rectangle r = w.Parent.ContextCoordinates(w.Slot);
            List<Rectangle> newList = new List<Rectangle>();

            foreach (Rectangle rInList in list)
            {
                Rectangle rebasedR = rInList;
                rebasedR.TopLeft-= r.TopLeft;

				Scroller sw = w as Scroller;
                if (sw != null)
                {
					if (sw.VerticalScrolling) {
						rebasedR.Top -= (int)sw.ScrollY;
//						if (sw.scrollY < 0)
//							Debug.WriteLine ("..");
					}if (sw.HorizontalScrolling)
						rebasedR.Left -= (int)sw.ScrollX;
                }

                newList.Add(rebasedR);
            }
			list = newList;        
        }
		public void stroke(Context ctx, Color c)
		{
			foreach (Rectangle r in list)
			{
				ctx.Rectangle(r);
			}

			ctx.SetSourceColor(c);

			ctx.LineWidth = 2;
			ctx.Stroke ();
		}
        public void clearAndClip(Context 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(Context ctx)
        {
            foreach (Rectangle r in list)
            {
                ctx.Rectangle(r);
            }

            ctx.Clip();
        }

		Rectangle _bounds;
		bool boundsUpToDate = true;
		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 = Rectangle.Empty;
					boundsUpToDate = true;
				}
				return _bounds;
			}
		}
		public void clear(Context ctx)
        {
            //ctx.Save();

            foreach (Rectangle r in list)
            {
                ctx.Rectangle(r);
            }
            ctx.Operator = Operator.Clear;
            ctx.Fill();
            ctx.Operator = Operator.Over;
            //ctx.Restore();
        }
		public override string ToString ()
		{
			string tmp = "";
			foreach (Rectangle r in list) {
				tmp += r.ToString ();
			}
			return tmp;
		}
    }
}