프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ConverterService / ImageFields / RectCollection.cs @ fe1ef780

이력 | 보기 | 이력해설 | 다운로드 (7.17 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Collections.ObjectModel;
6
using System.Windows;
7

    
8
namespace ImageFields
9
{
10
    internal class RectCollection
11
    {
12
        // Fields
13
        private bool dirty;
14
        private Collection<Int32Rect> rects = new Collection<Int32Rect>();
15

    
16
        // Methods
17
        public void Add(ref Int32Rect toAdd)
18
        {
19
            Merge(this.rects, toAdd);
20
            this.dirty = true;
21
        }
22

    
23
        private static void Merge(Collection<Int32Rect> rects, Int32Rect rectToAdd)
24
        {
25
            foreach (Int32Rect rect in rects)
26
            {
27
                Int32Rect b = rect;
28
                UnionRects(ref rectToAdd, ref b);
29
                if (rect != b)
30
                {
31
                    rects.Remove(rect);
32
                    if (b.Width != 0)
33
                    {
34
                        rects.Add(b);
35
                    }
36
                    break;
37
                }
38
                if (rectToAdd.Width == 0)
39
                {
40
                    break;
41
                }
42
            }
43
            if (rectToAdd.Width != 0)
44
            {
45
                rects.Add(rectToAdd);
46
            }
47
        }
48

    
49
        private static Collection<Int32Rect> MergeCollections(Collection<Int32Rect> oldRects)
50
        {
51
            Collection<Int32Rect> collection;
52
            Collection<Int32Rect> rects = oldRects;
53
            do
54
            {
55
                collection = rects;
56
                rects = new Collection<Int32Rect>();
57
                foreach (Int32Rect rect in collection)
58
                {
59
                    Merge(rects, rect);
60
                }
61
            }
62
            while (rects.Count != collection.Count);
63
            return collection;
64
        }
65

    
66
        private static Int32Rect MergeRects(ref Int32Rect a, Int32Rect b)
67
        {
68
            if ((((a.X >= b.X) && (a.Y >= b.Y)) && ((a.X + a.Width) <= (b.X + b.Width))) && ((a.Y + a.Height) <= (b.Y + b.Height)))
69
            {
70
                return b;
71
            }
72
            if ((((a.X <= b.X) && (a.Y <= b.Y)) && ((a.X + a.Width) >= (b.X + b.Width))) && ((a.Y + a.Height) >= (b.Y + b.Height)))
73
            {
74
                return a;
75
            }
76
            if ((((a.X == b.X) && (a.Width == b.Width)) && ((a.Y + a.Height) >= b.Y)) && (a.Y <= b.Y))
77
            {
78
                return new Int32Rect(a.X, a.Y, a.Width, (b.Y + b.Height) - a.Y);
79
            }
80
            if ((((a.X == b.X) && (a.Width == b.Width)) && ((b.Y + b.Height) >= a.Y)) && (b.Y <= a.Y))
81
            {
82
                return new Int32Rect(a.X, b.Y, a.Width, (a.Y + a.Height) - b.Y);
83
            }
84
            if ((((a.Y == b.Y) && (a.Height == b.Height)) && ((a.X + a.Width) >= b.X)) && (a.X <= b.X))
85
            {
86
                return new Int32Rect(a.X, a.Y, (b.X + b.Width) - a.X, a.Height);
87
            }
88
            if ((((a.Y == b.Y) && (a.Height == b.Height)) && ((b.X + b.Width) >= a.X)) && (b.X <= a.X))
89
            {
90
                return new Int32Rect(b.X, a.Y, (a.X + a.Width) - b.X, a.Height);
91
            }
92
            return Int32Rect.Empty;
93
        }
94

    
95
        private static void UnionRects(ref Int32Rect a, ref Int32Rect b)
96
        {
97
            if (((((b.X + b.Width) >= a.X) && (b.X <= (a.X + a.Width))) && ((b.Y + b.Height) >= a.Y)) && (b.Y <= (a.Y + a.Height)))
98
            {
99
                if ((((a.X >= b.X) && (a.Y >= b.Y)) && ((a.X + a.Width) <= (b.X + b.Width))) && ((a.Y + a.Height) <= (b.Y + b.Height)))
100
                {
101
                    a.Width = 0;
102
                }
103
                else if ((((a.X <= b.X) && (a.Y <= b.Y)) && ((a.X + a.Width) >= (b.X + b.Width))) && ((a.Y + a.Height) >= (b.Y + b.Height)))
104
                {
105
                    b.Width = 0;
106
                }
107
                else if ((((a.X == b.X) && (a.Width == b.Width)) && ((a.Y + a.Height) >= b.Y)) && (a.Y <= b.Y))
108
                {
109
                    a.Height = (b.Y + b.Height) - a.Y;
110
                    b.Width = 0;
111
                }
112
                else if ((((a.X == b.X) && (a.Width == b.Width)) && ((b.Y + b.Height) >= a.Y)) && (b.Y <= a.Y))
113
                {
114
                    b.Height = (a.Y + a.Height) - b.Y;
115
                    a.Width = 0;
116
                }
117
                else if ((((a.Y == b.Y) && (a.Height == b.Height)) && ((a.X + a.Width) >= b.X)) && (a.X <= b.X))
118
                {
119
                    a.Width = (b.X + b.Width) - a.X;
120
                    b.Width = 0;
121
                }
122
                else if ((((a.Y == b.Y) && (a.Height == b.Height)) && ((b.X + b.Width) >= a.X)) && (b.X <= a.X))
123
                {
124
                    b.Width = (a.X + a.Width) - b.X;
125
                    a.Width = 0;
126
                }
127
                else if ((((a.X <= b.X) && (a.Y <= b.Y)) && (((a.X + a.Width) >= b.X) && ((a.Y + a.Height) >= (b.Y + b.Height)))) && ((a.X + a.Width) <= (b.X + b.Width)))
128
                {
129
                    b.Width = ((b.X + b.Width) - a.X) - a.Width;
130
                    b.X = a.X + a.Width;
131
                }
132
                else if ((((b.X <= a.X) && (b.Y <= a.Y)) && (((b.X + b.Width) >= a.X) && ((b.Y + b.Height) >= (a.Y + a.Height)))) && ((b.X + b.Width) <= (a.X + a.Width)))
133
                {
134
                    a.Width = ((a.X + a.Width) - b.X) - b.Width;
135
                    a.X = b.X + b.Width;
136
                }
137
                else if ((((a.Y <= b.Y) && (a.X <= b.X)) && (((a.Y + a.Height) >= b.Y) && ((a.X + a.Width) >= (b.X + b.Width)))) && ((a.Y + a.Height) <= (b.Y + b.Height)))
138
                {
139
                    b.Height = ((b.Y + b.Height) - a.Y) - a.Height;
140
                    b.Y = a.Y + a.Height;
141
                }
142
                else if ((((b.Y <= a.Y) && (b.X <= a.X)) && (((b.Y + b.Height) >= a.Y) && ((b.X + b.Width) >= (a.X + a.Width)))) && ((b.Y + b.Height) <= (a.Y + a.Height)))
143
                {
144
                    a.Height = ((a.Y + a.Height) - b.Y) - b.Height;
145
                    a.Y = b.Y + b.Height;
146
                }
147
                else if ((((a.X <= b.X) && (a.Y >= b.Y)) && (((a.X + a.Width) >= b.X) && ((a.Y + a.Height) <= (b.Y + b.Height)))) && ((a.X + a.Width) <= (b.X + b.Width)))
148
                {
149
                    a.Width = b.X - a.X;
150
                }
151
                else if ((((b.X <= a.X) && (b.Y >= a.Y)) && (((b.X + b.Width) >= a.X) && ((b.Y + b.Height) <= (a.Y + a.Height)))) && ((b.X + b.Width) <= (a.X + a.Width)))
152
                {
153
                    b.Width = a.X - b.X;
154
                }
155
                else if ((((a.Y <= b.Y) && (a.X >= b.X)) && (((a.Y + a.Height) >= b.Y) && ((a.X + a.Width) <= (b.X + b.Width)))) && ((a.Y + a.Height) <= (b.Y + b.Height)))
156
                {
157
                    a.Height = b.Y - a.Y;
158
                }
159
                else if ((((b.Y <= a.Y) && (b.X >= a.X)) && (((b.Y + b.Height) >= a.Y) && ((b.X + b.Width) <= (a.X + a.Width)))) && ((b.Y + b.Height) <= (a.Y + a.Height)))
160
                {
161
                    b.Height = a.Y - b.Y;
162
                }
163
            }
164
        }
165

    
166
        // Properties
167
        public Collection<Int32Rect> Rects
168
        {
169
            get
170
            {
171
                if (this.dirty)
172
                {
173
                    this.rects = MergeCollections(this.rects);
174
                    this.dirty = false;
175
                }
176
                return this.rects;
177
            }
178
        }
179
    }
180

    
181
 
182

    
183
}
클립보드 이미지 추가 (최대 크기: 500 MB)