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
#include "StdAfx.h"
#include "Pad.h"
 
 
BOOL    CPadPoint::FindCrossX(CPadPoint &point, int cy, int &tx)// ÀÓÀÇÀÇ cy¿¡ ÇØ´çÇϴ x, °ª Ã£±â
{
    if(point.y == y)
    {
        tx= point.x;
        return y == cy;
    }
    tx= x+ (cy- y)*(point.x- x)/(point.y- y);
    return TRUE;
}
 
BOOL CPadPoint::FindCrossY(CPadPoint &point, int cx, int &ty)// ÀÓÀÇÀÇ cX¿¡ ÇØ´çÇϴ y °ª Ã£±â
{
    if(point.x == x)
    {
        ty= point.y;
        return x == cx;
    }
    ty= y+ (cx- x)*(point.y- y)/(point.x- x);
    return TRUE;
}
 
BOOL    CPadPoint::ChangeCrossHori(CPadPoint &point, int cy)// ÀÓÀÇÀÇ cy¿¡ ÇØ´çÇϴ x, y ·Î °ª ¹Ù²Ù±â
{
    if(point.y == y)
    {
        x= point.x;
        y= point.y;
        return    y == cy;
    }
    x= x+ (cy- y)*(point.x- x)/(point.y- y);
    y= cy;
    return TRUE;
}
BOOL    CPadPoint::ChangeCrossVert(CPadPoint &point, int cx)// ÀÓÀÇÀÇ cx¿¡ ÇØ´çÇϴ x, y ·Î °ª ¹Ù²Ù±â
{
    if(point.x == x)
    {
        x= point.x;
        y= point.y;
        return x == cx;
    }
    y= y+ (cx- x)*(point.y- y)/(point.x- x);
    x= cx;
    return TRUE;
}
 
void CPadPoligon::CalOutRect()
{
    CPadPoint *point;
 
    m_OutRect.SetRect(10000000, 10000000, -10000000, -10000000);
 
    for(int i= 0; i< m_nPadPoint; i++)
    {
        point= m_PadPoints+ i;
        if(point->x < m_OutRect.left)
            m_OutRect.left= point->x;
        if(point->y < m_OutRect.top)
            m_OutRect.top= point->y;
        if(m_OutRect.right < point->x)
            m_OutRect.right= point->x;
        if(m_OutRect.bottom < point->y)
            m_OutRect.bottom= point->y;
    }
}
 
void CPadPoligon::Find_Corner()
{
    int ip;
    int yMax= 1000000000;
    int iMax, xMax;
    int yMin= -1000000000;
    int iMin, xMin;
 
    if(m_nPadPoint < 3)
        return;
 
    if(! IsClockwise())
    {
        ReverseIndex(0);
    }
 
    for(ip= 0; ip < m_nPadPoint; ip++)
    {
        if(yMax > m_PadPoints[ip].y)// 
        {
            iMax= ip;
            yMax= m_PadPoints[ip].y;
            xMax= m_PadPoints[ip].x;
        }else if(yMax == m_PadPoints[ip].y)
        {
            if(m_PadPoints[ip].x < xMax)
            {
                iMax= ip;
                yMax= m_PadPoints[ip].y;
                xMax= m_PadPoints[ip].x;
            }
        }
        if(m_PadPoints[ip].y > yMin)
        {
            iMin= ip;
            yMin= m_PadPoints[ip].y;
            xMin= m_PadPoints[ip].x;
        }else if(yMin == m_PadPoints[ip].y)
        {
            if(m_PadPoints[ip].x > xMin)
            {
                iMin= ip;
                yMin= m_PadPoints[ip].y;
                xMin= m_PadPoints[ip].x;
            }
        }
    }
 
    m_iTopLeft= iMax;
    m_iBottomRight= iMin;
 
    CPadPoint *point;
 
    m_iTopRight= m_iTopLeft;
    for(ip= 0; ip < m_nPadPoint; ip++)
    {
        point= GetPadPoint_Round(m_iTopLeft+ ip);
        if(m_PadPoints[m_iTopLeft].y == point->y)
        {
            m_iTopRight= GetRoundIndex(m_iTopLeft+ ip);
        }else
        {
            break;
        }
    }
 
    m_iBottomLeft= m_iBottomRight;
    for(ip= 0; ip < m_nPadPoint; ip++)
    {
        point= GetPadPoint_Round(m_iBottomRight+ ip);
        if(m_PadPoints[m_iBottomRight].y == point->y)
        {
            m_iBottomLeft= GetRoundIndex(m_iBottomRight+ ip);
        }else
        {
            break;
        }
    }
 
    CalOutRect();
}
 
BOOL CPadPoligon::IsClockwise()
{
    if(m_nPadPoint < 3)    return FALSE;// °Ë»ç ¿µ¿ª ¾øÀ½, »ï°¢Çü ¸øÇØ...Àß¶ó¼­ ¿Í.
 
    CPadPoint pt1, pt2;
    int nCheck= m_nPadPoint- 2;
    int vr;
    for(int i= 0; i< nCheck; i++)
    {
        pt1= *GetPadPoint(i);
        pt2= *GetPadPoint(i+1);
        pt1.VectorTo(pt2);
        pt2.VectorTo(*GetPadPoint(i+2));
        vr= pt1.x*pt2.y- pt2.x*pt1.y;
        if(vr < 0)
            return FALSE;
    }
 
    return TRUE;
 
 
    pt1= *GetPadPoint(0);
    pt2= *GetPadPoint(1);
    pt1.VectorTo(pt2);
    pt2.VectorTo(*GetPadPoint(2));
 
 
    vr= pt1.x*pt2.y- pt2.x*pt1.y;
 
    if( vr >= 0)
    {
        return TRUE;
    }
    return FALSE;
}
BOOL CPadPoligon::Cut_TrapeziumVert(CTrapeziumStorage    *pTrapeziums)
{
    pTrapeziums->ResetTrapeziumCount();
    if(m_nPadPoint < 3)    return FALSE;// °Ë»ç ¿µ¿ª ¾øÀ½, »ï°¢Çü ¸øÇØ...Àß¶ó¼­ ¿Í.
    return TRUE;
}
 
BOOL CPadPoligon::Cut_TrapeziumHori(CTrapeziumStorage    *pTrapeziums)
{
    pTrapeziums->ResetTrapeziumCount();
    if(m_nPadPoint < 3)    return FALSE;// °Ë»ç ¿µ¿ª ¾øÀ½, »ï°¢Çü ¸øÇØ...Àß¶ó¼­ ¿Í.
 
    if(m_nPadPoint == 3)
    {
        int            i,j,nCount;
        CPadPoint    point;
        for(i=0;i<m_nPadPoint;i++)
        {
            nCount = 0;
            point = m_PadPoints[i];
            for(j=0;j<m_nPadPoint;j++)
            {
                if(point.x == m_PadPoints[j].x && point.y == m_PadPoints[j].y)
                    nCount++;
            }
 
            if(nCount >= 2)
                return FALSE;
        }
    }
 
    // if(nPoint == 4)    // »ç´Ù¸®²Ã È¤Àº rect ³×~~
 
    int        iLeft, iRight;
    CPadPoint TopLeft, TopRight;
 
    TopLeft= m_PadPoints[m_iTopLeft];
    TopRight= m_PadPoints[m_iTopRight];
 
 
    iLeft= GetPreIndex(m_iTopLeft);//(m_iTopLeft+ m_nPadPoint- 1)%m_nPadPoint;
    iRight= GetNextIndex(m_iTopRight);//(m_iTopRight+ 1)%m_nPadPoint;
 
    int endLeft, endRight;
    endLeft= GetPreIndex(m_iBottomLeft);
    endRight= GetNextIndex(m_iBottomRight);
 
    while(!(iLeft == endLeft && iRight == endRight))
    {
        pTrapeziums->OpenTrap(TopLeft, TopRight);
        if(m_PadPoints[iLeft].y < m_PadPoints[iRight].y)
        {
            TopLeft= m_PadPoints[iLeft];
            TopRight.ChangeCrossHori(m_PadPoints[iRight], TopLeft.y);
            pTrapeziums->CloseTrap(TopRight, TopLeft);
            //iLeft--;
            iLeft= GetPreIndex(iLeft);
        }else if(m_PadPoints[iLeft].y > m_PadPoints[iRight].y)
        {
            TopRight= m_PadPoints[iRight];
            TopLeft.ChangeCrossHori(m_PadPoints[iLeft], TopRight.y);
            pTrapeziums->CloseTrap(TopRight, TopLeft);
            //iRight++;
            iRight= GetNextIndex(iRight);
        }else // if( == )
        {
            TopLeft= m_PadPoints[iLeft];
            TopRight= m_PadPoints[iRight];
            pTrapeziums->CloseTrap(TopRight, TopLeft);
            //iLeft--;
            //iRight++;
            iLeft= GetPreIndex(iLeft);
            iRight= GetNextIndex(iRight);
        }
    }
    return TRUE;
}
 
void CPadPoligon::Cut_Margin(int left, int right, CPadPoligon &PadPoligon)
{
    int i;
    CPadPoint *pt1, *pt2;
    CPadPoint    point;
    int iPoint= 0;
    PadPoligon.ResetPoligon();
 
//    if(left > m_OutRect.left && left < m_OutRect.right)
    {
        for(i= 0; i< m_nPadPoint; i++)
        {
            pt1= GetPadPoint_Round(i);//m_PadPoints+ i;
            pt2= GetPadPoint_Round(i+ 1);//m_PadPoints+ (i+1)%m_nPadPoint;
 
            if(pt1->x < left)
            {
                if(left < pt2->x)
                {
                    point= *pt1;
                    point.ChangeCrossVert(*pt2, left);
                    PadPoligon.AddPoint(point);
                }
                if(right < pt2->x)
                {
                    point= *pt1;
                    point.ChangeCrossVert(*pt2, right);
                    PadPoligon.AddPoint(point);
                }
            }else if(right < pt1->x)
            {
                if(pt2->x < right)
                {
                    point= *pt1;
                    point.ChangeCrossVert(*pt2, right);
                    PadPoligon.AddPoint(point);
                }
                if(pt2->x < left)
                {
                    point= *pt1;
                    point.ChangeCrossVert(*pt2, left);
                    PadPoligon.AddPoint(point);
                }
            }else// if(left <= pt1->x && pt1->x < right)
            {
                PadPoligon.AddPoint(*pt1);
                if(pt2->x < left)
                {
                    point= *pt1;
                    point.ChangeCrossVert(*pt2, left);
                    PadPoligon.AddPoint(point);
                }else if(right < pt2->x)
                {
                    point= *pt1;
                    point.ChangeCrossVert(*pt2, right);
                    PadPoligon.AddPoint(point);
                }
            }
        }
    }
}
 
void CPadPoligon::Test()
{
    AddPoint(100, 150);
    AddPoint(400, 30);
    AddPoint(600, 100);
//    AddPoint(780, 200);
    AddPoint(800, 200);
    AddPoint(500, 400);    // bottom right
    AddPoint(400, 400);    // bottom left
    AddPoint(300, 400);    // bottom left
    AddPoint(200, 350);
    AddPoint(100, 200);
 
}
 
void CPadPoligon::OffsetPlus(int dx, int dy)
{
    int i;
    for(i=0; i< m_nPadPoint; i++)
    {
        m_PadPoints[i].x= m_PadPoints[i].x+ dx;
        m_PadPoints[i].y= m_PadPoints[i].y+ dy;
    }
}
 
void CPadPoligon::OffsetPlusIndex(int start)
{
    int i;
    CPadPoligon poligon= *this;
    for(i=0; i< m_nPadPoint; i++)
    {
        m_PadPoints[i]= *poligon.GetPadPoint_Round(start+ i);//poligon.m_PadPoints[GetPreIndex(m_nPadPoint+ test-i)];
    }
}
void CPadPoligon::ReverseIndex(int start)
{
    int i;
    CPadPoligon poligon= *this;
    for(i=0; i< m_nPadPoint; i++)
    {
        m_PadPoints[i]= *poligon.GetPadPoint_Round(m_nPadPoint+ start- i);//poligon.m_PadPoints[GetPreIndex(m_nPadPoint+ test-i)];
    }
}