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
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/* Simple box operations */

/*
 * Copyright (C) 2005, 2006 Elijah Newren
 * [meta_rectangle_intersect() is copyright the CTK+ Team according to Havoc,
 * see cdkrectangle.c.  As far as Havoc knows, he probably wrote
 * meta_rectangle_equal(), and I'm guessing it's (C) Red Hat.  So...]
 * Copyright (C) 1995-2000  CTK+ Team
 * Copyright (C) 2002 Red Hat, Inc.
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "boxes.h"
#include "util.h"
#include <X11/Xutil.h>  /* Just for the definition of the various gravities */

char*
meta_rectangle_to_string (const MetaRectangle *rect,
                          char                *output)
{
  /* 25 chars: 2 commas, space, plus, trailing \0 + 5 for each digit.
   * Should be more than enough space.  Note that of this space, the
   * trailing \0 will be overwritten for all but the last rectangle.
   */
  g_snprintf (output, RECT_LENGTH, "%d,%d +%d,%d",
              rect->x, rect->y, rect->width, rect->height);

  return output;
}

char*
meta_rectangle_region_to_string (GList      *region,
                                 const char *separator_string,
                                 char       *output)
{
  /* 27 chars: 2 commas, 2 square brackets, space, plus, trailing \0 + 5
   * for each digit.  Should be more than enough space.  Note that of this
   * space, the trailing \0 will be overwritten for all but the last
   * rectangle.
   */
  char rect_string[RECT_LENGTH];

  GList *tmp = region;
  char *cur = output;

  if (region == NULL)
    g_snprintf (output, 10, "(EMPTY)");

  while (tmp)
    {
      MetaRectangle *rect = tmp->data;
      g_snprintf (rect_string, RECT_LENGTH, "[%d,%d +%d,%d]",
                  rect->x, rect->y, rect->width, rect->height);
      cur = g_stpcpy (cur, rect_string);
      tmp = tmp->next;
      if (tmp)
        cur = g_stpcpy (cur, separator_string);
    }

  return output;
}

char*
meta_rectangle_edge_to_string (const MetaEdge *edge,
                               char           *output)
{
  /* 25 chars: 2 commas, space, plus, trailing \0 + 5 for each digit.
   * Should be more than enough space.  Note that of this space, the
   * trailing \0 will be overwritten for all but the last rectangle.
   *
   * Plus 2 for parenthesis, 4 for 2 more numbers, 2 more commas, and
   * 2 more spaces, for a total of 10 more.
   */
  g_snprintf (output, EDGE_LENGTH, "[%d,%d +%d,%d], %2d, %2d",
              edge->rect.x, edge->rect.y, edge->rect.width, edge->rect.height,
              edge->side_type, edge->edge_type);

  return output;
}

char*
meta_rectangle_edge_list_to_string (GList      *edge_list,
                                    const char *separator_string,
                                    char       *output)
{
  /* 27 chars: 2 commas, 2 square brackets, space, plus, trailing \0 + 5 for
   * each digit.  Should be more than enough space.  Note that of this
   * space, the trailing \0 will be overwritten for all but the last
   * rectangle.
   *
   * Plus 2 for parenthesis, 4 for 2 more numbers, 2 more commas, and
   * 2 more spaces, for a total of 10 more.
   */
  char rect_string[EDGE_LENGTH];

  char *cur = output;
  GList *tmp = edge_list;

  if (edge_list == NULL)
    g_snprintf (output, 10, "(EMPTY)");

  while (tmp)
    {
      MetaEdge      *edge = tmp->data;
      MetaRectangle *rect = &edge->rect;
      g_snprintf (rect_string, EDGE_LENGTH, "([%d,%d +%d,%d], %2d, %2d)",
                  rect->x, rect->y, rect->width, rect->height,
                  edge->side_type, edge->edge_type);
      cur = g_stpcpy (cur, rect_string);
      tmp = tmp->next;
      if (tmp)
        cur = g_stpcpy (cur, separator_string);
    }

  return output;
}

MetaRectangle
meta_rect (int x, int y, int width, int height)
{
  MetaRectangle temporary;
  temporary.x = x;
  temporary.y = y;
  temporary.width  = width;
  temporary.height = height;

  return temporary;
}

int
meta_rectangle_area (const MetaRectangle *rect)
{
  g_return_val_if_fail (rect != NULL, 0);
  return rect->width * rect->height;
}

gboolean
meta_rectangle_intersect (const MetaRectangle *src1,
			  const MetaRectangle *src2,
			  MetaRectangle *dest)
{
  int dest_x, dest_y;
  int dest_w, dest_h;
  int return_val;

  g_return_val_if_fail (src1 != NULL, FALSE);
  g_return_val_if_fail (src2 != NULL, FALSE);
  g_return_val_if_fail (dest != NULL, FALSE);

  return_val = FALSE;

  dest_x = MAX (src1->x, src2->x);
  dest_y = MAX (src1->y, src2->y);
  dest_w = MIN (src1->x + src1->width, src2->x + src2->width) - dest_x;
  dest_h = MIN (src1->y + src1->height, src2->y + src2->height) - dest_y;

  if (dest_w > 0 && dest_h > 0)
    {
      dest->x = dest_x;
      dest->y = dest_y;
      dest->width = dest_w;
      dest->height = dest_h;
      return_val = TRUE;
    }
  else
    {
      dest->width = 0;
      dest->height = 0;
    }

  return return_val;
}

gboolean
meta_rectangle_equal (const MetaRectangle *src1,
                      const MetaRectangle *src2)
{
  return ((src1->x == src2->x) &&
          (src1->y == src2->y) &&
          (src1->width == src2->width) &&
          (src1->height == src2->height));
}

void
meta_rectangle_union (const MetaRectangle *rect1,
                      const MetaRectangle *rect2,
                      MetaRectangle       *dest)
{
  int dest_x, dest_y;
  int dest_w, dest_h;

  dest_x = rect1->x;
  dest_y = rect1->y;
  dest_w = rect1->width;
  dest_h = rect1->height;

  if (rect2->x < dest_x)
    {
      dest_w += dest_x - rect2->x;
      dest_x = rect2->x;
    }
  if (rect2->y < dest_y)
    {
      dest_h += dest_y - rect2->y;
      dest_y = rect2->y;
    }
  if (rect2->x + rect2->width > dest_x + dest_w)
    dest_w = rect2->x + rect2->width - dest_x;
  if (rect2->y + rect2->height > dest_y + dest_h)
    dest_h = rect2->y + rect2->height - dest_y;

  dest->x = dest_x;
  dest->y = dest_y;
  dest->width = dest_w;
  dest->height = dest_h;
}

gboolean
meta_rectangle_overlap (const MetaRectangle *rect1,
                        const MetaRectangle *rect2)
{
  g_return_val_if_fail (rect1 != NULL, FALSE);
  g_return_val_if_fail (rect2 != NULL, FALSE);

  return !((rect1->x + rect1->width  <= rect2->x) ||
           (rect2->x + rect2->width  <= rect1->x) ||
           (rect1->y + rect1->height <= rect2->y) ||
           (rect2->y + rect2->height <= rect1->y));
}

gboolean
meta_rectangle_vert_overlap (const MetaRectangle *rect1,
                             const MetaRectangle *rect2)
{
  return (rect1->y < rect2->y + rect2->height &&
          rect2->y < rect1->y + rect1->height);
}

gboolean
meta_rectangle_horiz_overlap (const MetaRectangle *rect1,
                              const MetaRectangle *rect2)
{
  return (rect1->x < rect2->x + rect2->width &&
          rect2->x < rect1->x + rect1->width);
}

gboolean
meta_rectangle_could_fit_rect (const MetaRectangle *outer_rect,
                               const MetaRectangle *inner_rect)
{
  return (outer_rect->width  >= inner_rect->width &&
          outer_rect->height >= inner_rect->height);
}

gboolean
meta_rectangle_contains_rect  (const MetaRectangle *outer_rect,
                               const MetaRectangle *inner_rect)
{
  return
    inner_rect->x                      >= outer_rect->x &&
    inner_rect->y                      >= outer_rect->y &&
    inner_rect->x + inner_rect->width  <= outer_rect->x + outer_rect->width &&
    inner_rect->y + inner_rect->height <= outer_rect->y + outer_rect->height;
}

void
meta_rectangle_resize_with_gravity (const MetaRectangle *old_rect,
                                    MetaRectangle       *rect,
                                    int                  gravity,
                                    int                  new_width,
                                    int                  new_height)
{
  /* FIXME: I'm too deep into this to know whether the below comment is
   * still clear or not now that I've moved it out of constraints.c.
   * boxes.h has a good comment, but I'm not sure if the below info is also
   * helpful on top of that (or whether it has superfluous info).
   */

  /* These formulas may look overly simplistic at first but you can work
   * everything out with a left_frame_with, right_frame_width,
   * border_width, and old and new client area widths (instead of old total
   * width and new total width) and you come up with the same formulas.
   *
   * Also, note that the reason we can treat NorthWestGravity and
   * StaticGravity the same is because we're not given a location at
   * which to place the window--the window was already placed
   * appropriately before.  So, NorthWestGravity for this function
   * means to just leave the upper left corner of the outer window
   * where it already is, and StaticGravity for this function means to
   * just leave the upper left corner of the inner window where it
   * already is.  But leaving either of those two corners where they
   * already are will ensure that the other corner is fixed as well
   * (since frame size doesn't change)--thus making the two
   * equivalent.
   */

  /* First, the x direction */
  switch (gravity)
    {
    case NorthWestGravity:
    case WestGravity:
    case SouthWestGravity:
      rect->x = old_rect->x;
      break;

    case NorthGravity:
    case CenterGravity:
    case SouthGravity:
      /* FIXME: Needing to adjust new_width kind of sucks, but not doing so
       * would cause drift.
       */
      new_width -= (old_rect->width - new_width) % 2;
      rect->x = old_rect->x + (old_rect->width - new_width)/2;
      break;

    case NorthEastGravity:
    case EastGravity:
    case SouthEastGravity:
      rect->x = old_rect->x + (old_rect->width - new_width);
      break;

    case StaticGravity:
    default:
      rect->x = old_rect->x;
      break;
    }
  rect->width = new_width;

  /* Next, the y direction */
  switch (gravity)
    {
    case NorthWestGravity:
    case NorthGravity:
    case NorthEastGravity:
      rect->y = old_rect->y;
      break;

    case WestGravity:
    case CenterGravity:
    case EastGravity:
      /* FIXME: Needing to adjust new_height kind of sucks, but not doing so
       * would cause drift.
       */
      new_height -= (old_rect->height - new_height) % 2;
      rect->y = old_rect->y + (old_rect->height - new_height)/2;
      break;

    case SouthWestGravity:
    case SouthGravity:
    case SouthEastGravity:
      rect->y = old_rect->y + (old_rect->height - new_height);
      break;

    case StaticGravity:
    default:
      rect->y = old_rect->y;
      break;
    }
  rect->height = new_height;
}

/* Not so simple helper function for get_minimal_spanning_set_for_region() */
static GList*
merge_spanning_rects_in_region (GList *region)
{
  /* NOTE FOR ANY OPTIMIZATION PEOPLE OUT THERE: Please see the
   * documentation of get_minimal_spanning_set_for_region() for performance
   * considerations that also apply to this function.
   */

  GList* compare;
  compare = region;

  if (region == NULL)
    {
      meta_warning ("Region to merge was empty!  Either you have a some "
                    "pathological STRUT list or there's a bug somewhere!\n");
      return NULL;
    }

  while (compare && compare->next)
    {
      MetaRectangle *a = compare->data;
      GList *other = compare->next;

      g_assert (a->width > 0 && a->height > 0);

      while (other)
        {
          MetaRectangle *b = other->data;
          GList *delete_me = NULL;

          g_assert (b->width > 0 && b->height > 0);

          /* If a contains b, just remove b */
          if (meta_rectangle_contains_rect (a, b))
            {
              delete_me = other;
            }
          /* If b contains a, just remove a */
          else if (meta_rectangle_contains_rect (b, a))
            {
              delete_me = compare;
            }
          /* If a and b might be mergeable horizontally */
          else if (a->y == b->y && a->height == b->height)
            {
              /* If a and b overlap */
              if (meta_rectangle_overlap (a, b))
                {
                  int new_x = MIN (a->x, b->x);
                  a->width = MAX (a->x + a->width, b->x + b->width) - new_x;
                  a->x = new_x;
                  delete_me = other;
                }
              /* If a and b are adjacent */
              else if (a->x + a->width == b->x || a->x == b->x + b->width)
                {
                  int new_x = MIN (a->x, b->x);
                  a->width = MAX (a->x + a->width, b->x + b->width) - new_x;
                  a->x = new_x;
                  delete_me = other;
                }
            }
          /* If a and b might be mergeable vertically */
          else if (a->x == b->x && a->width == b->width)
            {
              /* If a and b overlap */
              if (meta_rectangle_overlap (a, b))
                {
                  int new_y = MIN (a->y, b->y);
                  a->height = MAX (a->y + a->height, b->y + b->height) - new_y;
                  a->y = new_y;
                  delete_me = other;
                }
              /* If a and b are adjacent */
              else if (a->y + a->height == b->y || a->y == b->y + b->height)
                {
                  int new_y = MIN (a->y, b->y);
                  a->height = MAX (a->y + a->height, b->y + b->height) - new_y;
                  a->y = new_y;
                  delete_me = other;
                }
            }

          other = other->next;

          /* Delete any rectangle in the list that is no longer wanted */
          if (delete_me != NULL)
            {
              /* Deleting the rect we compare others to is a little tricker */
              if (compare == delete_me)
                {
                  compare = compare->next;
                  other = compare->next;
                  a = compare->data;
                }

              /* avoid false positive warning in Clang static analyzer */
              g_assert (a != delete_me->data);

              /* Okay, we can free it now */
              g_free (delete_me->data);
              region = g_list_delete_link (region, delete_me);
            }

        }

      compare = compare->next;
    }

  return region;
}

/* Simple helper function for get_minimal_spanning_set_for_region()... */
static gint
compare_rect_areas (gconstpointer a, gconstpointer b)
{
  const MetaRectangle *a_rect = (gconstpointer) a;
  const MetaRectangle *b_rect = (gconstpointer) b;

  int a_area = meta_rectangle_area (a_rect);
  int b_area = meta_rectangle_area (b_rect);

  return b_area - a_area; /* positive ret value denotes b > a, ... */
}

/* This function is trying to find a "minimal spanning set (of rectangles)"
 * for a given region.
 *
 * The region is given by taking basic_rect, then removing the areas
 * covered by all the rectangles in the all_struts list, and then expanding
 * the resulting region by the given number of pixels in each direction.
 *
 * A "minimal spanning set (of rectangles)" is the best name I could come
 * up with for the concept I had in mind.  Basically, for a given region, I
 * want a set of rectangles with the property that a window is contained in
 * the region if and only if it is contained within at least one of the
 * rectangles.
 *
 * The GList* returned will be a list of (allocated) MetaRectangles.
 * The list will need to be freed by calling
 * meta_rectangle_free_spanning_set() on it (or by manually
 * implementing that function...)
 */
GList*
meta_rectangle_get_minimal_spanning_set_for_region (
  const MetaRectangle *basic_rect,
  const GSList  *all_struts)
{
  /* NOTE FOR OPTIMIZERS: This function *might* be somewhat slow,
   * especially due to the call to merge_spanning_rects_in_region() (which
   * is O(n^2) where n is the size of the list generated in this function).
   * This is made more onerous due to the fact that it involves a fair
   * number of memory allocation and deallocation calls.  However, n is 1
   * for default installations of Cafe (because partial struts aren't used
   * by default and only partial struts increase the size of the spanning
   * set generated).  With one partial strut, n will be 2 or 3.  With 2
   * partial struts, n will probably be 4 or 5.  So, n probably isn't large
   * enough to make this worth bothering.  Further, it is only called from
   * workspace.c:ensure_work_areas_validated (at least as of the time of
   * writing this comment), which in turn should only be called if the
   * strut list changes or the screen or xinerama size changes.  If it ever
   * does show up on profiles (most likely because people start using
   * ridiculously huge numbers of partial struts), possible optimizations
   * include:
   *
   * (1) rewrite merge_spanning_rects_in_region() to be O(n) or O(nlogn).
   *     I'm not totally sure it's possible, but with a couple copies of
   *     the list and sorting them appropriately, I believe it might be.
   * (2) only call merge_spanning_rects_in_region() with a subset of the
   *     full list of rectangles.  I believe from some of my preliminary
   *     debugging and thinking about it that it is possible to figure out
   *     apriori groups of rectangles which are only merge candidates with
   *     each other.  (See testboxes.c:get_screen_region() when which==2
   *     and track the steps of this function carefully to see what gave
   *     me the hint that this might work)
   * (3) figure out how to avoid merge_spanning_rects_in_region().  I think
   *     it might be possible to modify this function to make that
   *     possible, and I spent just a little while thinking about it, but n
   *     wasn't large enough to convince me to care yet.
   * (4) Some of the stuff Rob mentioned at http://mail.gnome.org/archives\
   *     /croma-devel-list/2005-November/msg00028.html.  (Sorry for the
   *     URL splitting.)
   */

  GList         *ret;
  GList         *tmp_list;
  const GSList  *strut_iter;
  MetaRectangle *temp_rect;

  /* The algorithm is basically as follows:
   *   Initialize rectangle_set to basic_rect
   *   Foreach strut:
   *     Foreach rectangle in rectangle_set:
   *       - Split the rectangle into new rectangles that don't overlap the
   *         strut (but which are as big as possible otherwise)
   *       - Remove the old (pre-split) rectangle from the rectangle_set,
   *         and replace it with the new rectangles generated from the
   *         splitting
   */

  temp_rect = g_new (MetaRectangle, 1);
  *temp_rect = *basic_rect;
  ret = g_list_prepend (NULL, temp_rect);

  for (strut_iter = all_struts; strut_iter; strut_iter = strut_iter->next)
    {
      GList *rect_iter;
      MetaRectangle *strut_rect = &((MetaStrut*)strut_iter->data)->rect;

      tmp_list = ret;
      ret = NULL;
      rect_iter = tmp_list;
      while (rect_iter)
        {
          MetaRectangle *rect = (MetaRectangle*) rect_iter->data;
          if (!meta_rectangle_overlap (rect, strut_rect))
            ret = g_list_prepend (ret, rect);
          else
            {
              /* If there is area in rect left of strut */
              if (BOX_LEFT (*rect) < BOX_LEFT (*strut_rect))
                {
                  temp_rect = g_new (MetaRectangle, 1);
                  *temp_rect = *rect;
                  temp_rect->width = BOX_LEFT (*strut_rect) - BOX_LEFT (*rect);
                  ret = g_list_prepend (ret, temp_rect);
                }
              /* If there is area in rect right of strut */
              if (BOX_RIGHT (*rect) > BOX_RIGHT (*strut_rect))
                {
                  int new_x;
                  temp_rect = g_new (MetaRectangle, 1);
                  *temp_rect = *rect;
                  new_x = BOX_RIGHT (*strut_rect);
                  temp_rect->width = BOX_RIGHT(*rect) - new_x;
                  temp_rect->x = new_x;
                  ret = g_list_prepend (ret, temp_rect);
                }
              /* If there is area in rect above strut */
              if (BOX_TOP (*rect) < BOX_TOP (*strut_rect))
                {
                  temp_rect = g_new (MetaRectangle, 1);
                  *temp_rect = *rect;
                  temp_rect->height = BOX_TOP (*strut_rect) - BOX_TOP (*rect);
                  ret = g_list_prepend (ret, temp_rect);
                }
              /* If there is area in rect below strut */
              if (BOX_BOTTOM (*rect) > BOX_BOTTOM (*strut_rect))
                {
                  int new_y;
                  temp_rect = g_new (MetaRectangle, 1);
                  *temp_rect = *rect;
                  new_y = BOX_BOTTOM (*strut_rect);
                  temp_rect->height = BOX_BOTTOM (*rect) - new_y;
                  temp_rect->y = new_y;
                  ret = g_list_prepend (ret, temp_rect);
                }
              g_free (rect);
            }
          rect_iter = rect_iter->next;
        }
      g_list_free (tmp_list);
    }

  /* Sort by maximal area, just because I feel like it... */
  ret = g_list_sort (ret, compare_rect_areas);

  /* Merge rectangles if possible so that the list really is minimal */
  ret = merge_spanning_rects_in_region (ret);

  return ret;
}

GList*
meta_rectangle_expand_region (GList     *region,
                              const int  left_expand,
                              const int  right_expand,
                              const int  top_expand,
                              const int  bottom_expand)
{
  return meta_rectangle_expand_region_conditionally (region,
                                                     left_expand,
                                                     right_expand,
                                                     top_expand,
                                                     bottom_expand,
                                                     0,
                                                     0);
}

GList*
meta_rectangle_expand_region_conditionally (GList     *region,
                                            const int  left_expand,
                                            const int  right_expand,
                                            const int  top_expand,
                                            const int  bottom_expand,
                                            const int  min_x,
                                            const int  min_y)
{
  GList *tmp_list = region;
  while (tmp_list)
    {
      MetaRectangle *rect = (MetaRectangle*) tmp_list->data;
      if (rect->width >= min_x)
        {
          rect->x      -= left_expand;
          rect->width  += (left_expand + right_expand);
        }
      if (rect->height >= min_y)
        {
          rect->y      -= top_expand;
          rect->height += (top_expand + bottom_expand);
        }
      tmp_list = tmp_list->next;
    }

  return region;
}

void
meta_rectangle_expand_to_avoiding_struts (MetaRectangle       *rect,
                                          const MetaRectangle *expand_to,
                                          const MetaDirection  direction,
                                          const GSList        *all_struts)
{
  const GSList *strut_iter;

  /* If someone wants this function to handle more fine-grained
   * direction expanding in the future (e.g. only left, or fully
   * horizontal plus upward), feel free.  But I'm hard-coding for both
   * horizontal directions (exclusive-)or both vertical directions.
   */
  g_assert ((direction == META_DIRECTION_HORIZONTAL) ^
            (direction == META_DIRECTION_VERTICAL  ));

  if (direction == META_DIRECTION_HORIZONTAL)
    {
      rect->x      = expand_to->x;
      rect->width  = expand_to->width;
    }
  else
    {
      rect->y      = expand_to->y;
      rect->height = expand_to->height;
    }


  /* Run over all struts */
  for (strut_iter = all_struts; strut_iter; strut_iter = strut_iter->next)
    {
      MetaStrut *strut = (MetaStrut*) strut_iter->data;

      /* Skip struts that don't overlap */
      if (!meta_rectangle_overlap (&strut->rect, rect))
        continue;

      if (direction == META_DIRECTION_HORIZONTAL)
        {
          if (strut->side == META_SIDE_LEFT)
            {
              int offset = BOX_RIGHT(strut->rect) - BOX_LEFT(*rect);
              rect->x     += offset;
              rect->width -= offset;
            }
          else if (strut->side == META_SIDE_RIGHT)
            {
              int offset = BOX_RIGHT (*rect) - BOX_LEFT(strut->rect);
              rect->width -= offset;
            }
          /* else ignore the strut */
        }
      else /* direction == META_DIRECTION_VERTICAL */
        {
          if (strut->side == META_SIDE_TOP)
            {
              int offset = BOX_BOTTOM(strut->rect) - BOX_TOP(*rect);
              rect->y      += offset;
              rect->height -= offset;
            }
          else if (strut->side == META_SIDE_BOTTOM)
            {
              int offset = BOX_BOTTOM(*rect) - BOX_TOP(strut->rect);
              rect->height -= offset;
            }
          /* else ignore the strut */
        }
    } /* end loop over struts */
} /* end meta_rectangle_expand_to_avoiding_struts */

gboolean
meta_rectangle_could_fit_in_region (const GList         *spanning_rects,
                                    const MetaRectangle *rect)
{
  const GList *temp;
  gboolean     could_fit;

  temp = spanning_rects;
  could_fit = FALSE;
  while (!could_fit && temp != NULL)
    {
      could_fit = could_fit || meta_rectangle_could_fit_rect (temp->data, rect);
      temp = temp->next;
    }

  return could_fit;
}

gboolean
meta_rectangle_contained_in_region (const GList         *spanning_rects,
                                    const MetaRectangle *rect)
{
  const GList *temp;
  gboolean     contained;

  temp = spanning_rects;
  contained = FALSE;
  while (!contained && temp != NULL)
    {
      contained = contained || meta_rectangle_contains_rect (temp->data, rect);
      temp = temp->next;
    }

  return contained;
}

gboolean
meta_rectangle_overlaps_with_region (const GList         *spanning_rects,
                                     const MetaRectangle *rect)
{
  const GList *temp;
  gboolean     overlaps;

  temp = spanning_rects;
  overlaps = FALSE;
  while (!overlaps && temp != NULL)
    {
      overlaps = overlaps || meta_rectangle_overlap (temp->data, rect);
      temp = temp->next;
    }

  return overlaps;
}


void
meta_rectangle_clamp_to_fit_into_region (const GList         *spanning_rects,
                                         FixedDirections      fixed_directions,
                                         MetaRectangle       *rect,
                                         const MetaRectangle *min_size)
{
  const GList *temp;
  const MetaRectangle *best_rect = NULL;
  int                  best_overlap = 0;

  /* First, find best rectangle from spanning_rects to which we can clamp
   * rect to fit into.
   */
  for (temp = spanning_rects; temp; temp = temp->next)
    {
      MetaRectangle *compare_rect = temp->data;
      int            maximal_overlap_amount_for_compare;

      /* If x is fixed and the entire width of rect doesn't fit in compare,
       * skip this rectangle.
       */
      if ((fixed_directions & FIXED_DIRECTION_X) &&
          (compare_rect->x > rect->x ||
           compare_rect->x + compare_rect->width < rect->x + rect->width))
        continue;

      /* If y is fixed and the entire height of rect doesn't fit in compare,
       * skip this rectangle.
       */
      if ((fixed_directions & FIXED_DIRECTION_Y) &&
          (compare_rect->y > rect->y ||
           compare_rect->y + compare_rect->height < rect->y + rect->height))
        continue;

      /* If compare can't hold the min_size window, skip this rectangle. */
      if (compare_rect->width  < min_size->width ||
          compare_rect->height < min_size->height)
        continue;

      /* Determine maximal overlap amount */
      maximal_overlap_amount_for_compare =
        MIN (rect->width,  compare_rect->width) *
        MIN (rect->height, compare_rect->height);

      /* See if this is the best rect so far */
      if (maximal_overlap_amount_for_compare > best_overlap)
        {
          best_rect    = compare_rect;
          best_overlap = maximal_overlap_amount_for_compare;
        }
    }

  /* Clamp rect appropriately */
  if (best_rect == NULL)
    {
      meta_warning ("No rect whose size to clamp to found!\n");

      /* If it doesn't fit, at least make it no bigger than it has to be */
      if (!(fixed_directions & FIXED_DIRECTION_X))
        rect->width  = min_size->width;
      if (!(fixed_directions & FIXED_DIRECTION_Y))
        rect->height = min_size->height;
    }
  else
    {
      rect->width  = MIN (rect->width,  best_rect->width);
      rect->height = MIN (rect->height, best_rect->height);
    }
}

void
meta_rectangle_clip_to_region (const GList         *spanning_rects,
                               FixedDirections      fixed_directions,
                               MetaRectangle       *rect)
{
  const GList *temp;
  const MetaRectangle *best_rect = NULL;
  int                  best_overlap = 0;

  /* First, find best rectangle from spanning_rects to which we will clip
   * rect into.
   */
  for (temp = spanning_rects; temp; temp = temp->next)
    {
      MetaRectangle *compare_rect = temp->data;
      MetaRectangle  overlap;
      int            maximal_overlap_amount_for_compare;

      /* If x is fixed and the entire width of rect doesn't fit in compare,
       * skip the rectangle.
       */
      if ((fixed_directions & FIXED_DIRECTION_X) &&
          (compare_rect->x > rect->x ||
           compare_rect->x + compare_rect->width < rect->x + rect->width))
        continue;

      /* If y is fixed and the entire height of rect doesn't fit in compare,
       * skip the rectangle.
       */
      if ((fixed_directions & FIXED_DIRECTION_Y) &&
          (compare_rect->y > rect->y ||
           compare_rect->y + compare_rect->height < rect->y + rect->height))
        continue;

      /* Determine maximal overlap amount */
      meta_rectangle_intersect (rect, compare_rect, &overlap);
      maximal_overlap_amount_for_compare = meta_rectangle_area (&overlap);

      /* See if this is the best rect so far */
      if (maximal_overlap_amount_for_compare > best_overlap)
        {
          best_rect    = compare_rect;
          best_overlap = maximal_overlap_amount_for_compare;
        }
    }

  /* Clip rect appropriately */
  if (best_rect == NULL)
    meta_warning ("No rect to clip to found!\n");
  else
    {
      /* Extra precaution with checking fixed direction shouldn't be needed
       * due to logic above, but it shouldn't hurt either.
       */
      if (!(fixed_directions & FIXED_DIRECTION_X))
        {
          /* Find the new left and right */
          int new_x = MAX (rect->x, best_rect->x);
          rect->width = MIN ((rect->x + rect->width)           - new_x,
                             (best_rect->x + best_rect->width) - new_x);
          rect->x = new_x;
        }

      /* Extra precaution with checking fixed direction shouldn't be needed
       * due to logic above, but it shouldn't hurt either.
       */
      if (!(fixed_directions & FIXED_DIRECTION_Y))
        {
          /* Clip the top, if needed */
          int new_y = MAX (rect->y, best_rect->y);
          rect->height = MIN ((rect->y + rect->height)           - new_y,
                              (best_rect->y + best_rect->height) - new_y);
          rect->y = new_y;
        }
    }
}

void
meta_rectangle_shove_into_region (const GList         *spanning_rects,
                                  FixedDirections      fixed_directions,
                                  MetaRectangle       *rect)
{
  const GList *temp;
  const MetaRectangle *best_rect = NULL;
  int                  best_overlap = 0;
  int                  shortest_distance = G_MAXINT;

  /* First, find best rectangle from spanning_rects to which we will shove
   * rect into.
   */

  for (temp = spanning_rects; temp; temp = temp->next)
    {
      MetaRectangle *compare_rect = temp->data;
      int            maximal_overlap_amount_for_compare;
      int            dist_to_compare;

      /* If x is fixed and the entire width of rect doesn't fit in compare,
       * skip this rectangle.
       */
      if ((fixed_directions & FIXED_DIRECTION_X) &&
          (compare_rect->x > rect->x ||
           compare_rect->x + compare_rect->width < rect->x + rect->width))
        continue;

      /* If y is fixed and the entire height of rect doesn't fit in compare,
       * skip this rectangle.
       */
      if ((fixed_directions & FIXED_DIRECTION_Y) &&
          (compare_rect->y > rect->y ||
           compare_rect->y + compare_rect->height < rect->y + rect->height))
        continue;

      /* Determine maximal overlap amount between rect & compare_rect */
      maximal_overlap_amount_for_compare =
        MIN (rect->width,  compare_rect->width) *
        MIN (rect->height, compare_rect->height);

      /* Determine distance necessary to put rect into compare_rect */
      dist_to_compare = 0;
      if (compare_rect->x > rect->x)
        dist_to_compare += compare_rect->x - rect->x;
      if (compare_rect->x + compare_rect->width < rect->x + rect->width)
        dist_to_compare += (rect->x + rect->width) -
                           (compare_rect->x + compare_rect->width);
      if (compare_rect->y > rect->y)
        dist_to_compare += compare_rect->y - rect->y;
      if (compare_rect->y + compare_rect->height < rect->y + rect->height)
        dist_to_compare += (rect->y + rect->height) -
                           (compare_rect->y + compare_rect->height);

      /* See if this is the best rect so far */
      if ((maximal_overlap_amount_for_compare > best_overlap) ||
          (maximal_overlap_amount_for_compare == best_overlap &&
           dist_to_compare                    <  shortest_distance))
        {
          best_rect         = compare_rect;
          best_overlap      = maximal_overlap_amount_for_compare;
          shortest_distance = dist_to_compare;
        }
    }

  /* Shove rect appropriately */
  if (best_rect == NULL)
    meta_warning ("No rect to shove into found!\n");
  else
    {
      /* Extra precaution with checking fixed direction shouldn't be needed
       * due to logic above, but it shouldn't hurt either.
       */
      if (!(fixed_directions & FIXED_DIRECTION_X))
        {
          /* Shove to the right, if needed */
          if (best_rect->x > rect->x)
            rect->x = best_rect->x;

          /* Shove to the left, if needed */
          if (best_rect->x + best_rect->width < rect->x + rect->width)
            rect->x = (best_rect->x + best_rect->width) - rect->width;
        }

      /* Extra precaution with checking fixed direction shouldn't be needed
       * due to logic above, but it shouldn't hurt either.
       */
      if (!(fixed_directions & FIXED_DIRECTION_Y))
        {
          /* Shove down, if needed */
          if (best_rect->y > rect->y)
            rect->y = best_rect->y;

          /* Shove up, if needed */
          if (best_rect->y + best_rect->height < rect->y + rect->height)
            rect->y = (best_rect->y + best_rect->height) - rect->height;
        }
    }
}

void
meta_rectangle_find_linepoint_closest_to_point (double x1,
                                                double y1,
                                                double x2,
                                                double y2,
                                                double px,
                                                double py,
                                                double *valx,
                                                double *valy)
{
  /* I'll use the shorthand rx, ry for the return values, valx & valy.
   * Now, we need (rx,ry) to be on the line between (x1,y1) and (x2,y2).
   * For that to happen, we first need the slope of the line from (x1,y1)
   * to (rx,ry) must match the slope of (x1,y1) to (x2,y2), i.e.:
   *   (ry-y1)   (y2-y1)
   *   ------- = -------
   *   (rx-x1)   (x2-x1)
   * If x1==x2, though, this gives divide by zero errors, so we want to
   * rewrite the equation by multiplying both sides by (rx-x1)*(x2-x1):
   *   (ry-y1)(x2-x1) = (y2-y1)(rx-x1)
   * This is a valid requirement even when x1==x2 (when x1==x2, this latter
   * equation will basically just mean that rx must be equal to both x1 and
   * x2)
   *
   * The other requirement that we have is that the line from (rx,ry) to
   * (px,py) must be perpendicular to the line from (x1,y1) to (x2,y2).  So
   * we just need to get a vector in the direction of each line, take the
   * dot product of the two, and ensure that the result is 0:
   *   (rx-px)*(x2-x1) + (ry-py)*(y2-y1) = 0.
   *
   * This gives us two equations and two unknowns:
   *
   *   (ry-y1)(x2-x1) = (y2-y1)(rx-x1)
   *   (rx-px)*(x2-x1) + (ry-py)*(y2-y1) = 0.
   *
   * This particular pair of equations is always solvable so long as
   * (x1,y1) and (x2,y2) are not the same point (and note that anyone who
   * calls this function that way is braindead because it means that they
   * really didn't specify a line after all).  However, the caller should
   * be careful to avoid making (x1,y1) and (x2,y2) too close (e.g. like
   * 10^{-8} apart in each coordinate), otherwise roundoff error could
   * cause issues.  Solving these equations by hand (or using Maple(TM) or
   * Mathematica(TM) or whatever) results in slightly messy expressions,
   * but that's all the below few lines do.
   */

  double diffx, diffy, den;
  diffx = x2 - x1;
  diffy = y2 - y1;
  den = diffx * diffx + diffy * diffy;

  *valx = (py * diffx * diffy + px * diffx * diffx +
           y2 * x1 * diffy - y1 * x2 * diffy) / den;
  *valy = (px * diffx * diffy + py * diffy * diffy +
           x2 * y1 * diffx - x1 * y2 * diffx) / den;
}

/***************************************************************************/
/*                                                                         */
/* Switching gears to code for edges instead of just rectangles            */
/*                                                                         */
/***************************************************************************/

gboolean
meta_rectangle_edge_aligns (const MetaRectangle *rect, const MetaEdge *edge)
{
  /* The reason for the usage of <= below instead of < is because we are
   * interested in in-the-way-or-adject'ness.  So, a left (i.e. vertical
   * edge) occupying y positions 0-9 (which has a y of 0 and a height of
   * 10) and a rectangle with top at y=10 would be considered to "align" by
   * this function.
   */
  switch (edge->side_type)
    {
    case META_SIDE_LEFT:
    case META_SIDE_RIGHT:
      return BOX_TOP (*rect)      <= BOX_BOTTOM (edge->rect) &&
             BOX_TOP (edge->rect) <= BOX_BOTTOM (*rect);
    case META_SIDE_TOP:
    case META_SIDE_BOTTOM:
      return BOX_LEFT (*rect)      <= BOX_RIGHT (edge->rect) &&
             BOX_LEFT (edge->rect) <= BOX_RIGHT (*rect);
    default:
      g_assert_not_reached ();
    }
}

static GList*
get_rect_minus_overlap (const GList   *rect_in_list,
                        MetaRectangle *overlap)
{
  MetaRectangle *temp;
  MetaRectangle *rect = rect_in_list->data;
  GList *ret = NULL;

  if (BOX_LEFT (*rect) < BOX_LEFT (*overlap))
    {
      temp = g_new (MetaRectangle, 1);
      *temp = *rect;
      temp->width = BOX_LEFT (*overlap) - BOX_LEFT (*rect);
      ret = g_list_prepend (ret, temp);
    }
  if (BOX_RIGHT (*rect) > BOX_RIGHT (*overlap))
    {
      temp = g_new (MetaRectangle, 1);
      *temp = *rect;
      temp->x = BOX_RIGHT (*overlap);
      temp->width = BOX_RIGHT (*rect) - BOX_RIGHT (*overlap);
      ret = g_list_prepend (ret, temp);
    }
  if (BOX_TOP (*rect) < BOX_TOP (*overlap))
    {
      temp = g_new (MetaRectangle, 1);
      temp->x      = overlap->x;
      temp->width  = overlap->width;
      temp->y      = BOX_TOP (*rect);
      temp->height = BOX_TOP (*overlap) - BOX_TOP (*rect);
      ret = g_list_prepend (ret, temp);
    }
  if (BOX_BOTTOM (*rect) > BOX_BOTTOM (*overlap))
    {
      temp = g_new (MetaRectangle, 1);
      temp->x      = overlap->x;
      temp->width  = overlap->width;
      temp->y      = BOX_BOTTOM (*overlap);
      temp->height = BOX_BOTTOM (*rect) - BOX_BOTTOM (*overlap);
      ret = g_list_prepend (ret, temp);
    }

  return ret;
}

static GList*
replace_rect_with_list (GList *old_element,
                        GList *new_list)
{
  GList *ret;
  g_assert (old_element != NULL);

  if (!new_list)
    {
      /* If there is no new list, just remove the old_element */
      ret = g_list_remove_link (old_element, old_element);
    }
  else
    {
      /* Fix up the prev and next pointers everywhere */
      ret = new_list;
      if (old_element->prev)
        {
          old_element->prev->next = new_list;
          new_list->prev = old_element->prev;
        }
      if (old_element->next)
        {
          GList *tmp = g_list_last (new_list);
          old_element->next->prev = tmp;
          tmp->next = old_element->next;
        }
    }

  /* Free the old_element and return the appropriate "next" point */
  g_free (old_element->data);
  g_list_free_1 (old_element);
  return ret;
}

/* Make a copy of the strut list, make sure that copy only contains parts
 * of the old_struts that intersect with the region rect, and then do some
 * magic to make all the new struts disjoint (okay, we we break up struts
 * that aren't disjoint in a way that the overlapping part is only included
 * once, so it's not really magic...).
 */
static GList*
get_disjoint_strut_rect_list_in_region (const GSList        *old_struts,
                                        const MetaRectangle *region)
{
  GList *strut_rects;
  GList *tmp;

  /* First, copy the list */
  strut_rects = NULL;
  while (old_struts)
    {
      MetaRectangle *cur = &((MetaStrut*)old_struts->data)->rect;<--- Variable 'cur' can be declared as pointer to const
      MetaRectangle *copy = g_new (MetaRectangle, 1);
      *copy = *cur;
      if (meta_rectangle_intersect (copy, region, copy))
        strut_rects = g_list_prepend (strut_rects, copy);
      else
        g_free (copy);

      old_struts = old_struts->next;
    }

  /* Now, loop over the list and check for intersections, fixing things up
   * where they do intersect.
   */
  tmp = strut_rects;
  while (tmp)
    {
      GList *compare;

      MetaRectangle *cur = tmp->data;

      compare = tmp->next;
      while (compare)
        {
          MetaRectangle *comp = compare->data;<--- Variable 'comp' can be declared as pointer to const
          MetaRectangle overlap;

          if (meta_rectangle_intersect (cur, comp, &overlap))
            {
              /* Get a list of rectangles for each strut that don't overlap
               * the intersection region.
               */
              GList *cur_leftover  = get_rect_minus_overlap (tmp,  &overlap);
              GList *comp_leftover = get_rect_minus_overlap (compare, &overlap);

              /* Add the intersection region to cur_leftover */
              MetaRectangle *overlap_allocated = g_new (MetaRectangle, 1);
              *overlap_allocated = overlap;
              cur_leftover = g_list_prepend (cur_leftover, overlap_allocated);

              /* Fix up tmp, compare, and cur -- maybe struts too */
              if (strut_rects == tmp)
                {
                  strut_rects = replace_rect_with_list (tmp, cur_leftover);
                  tmp = strut_rects;
                }
              else
                tmp   = replace_rect_with_list (tmp,     cur_leftover);
              compare = replace_rect_with_list (compare, comp_leftover);

              if (compare == NULL)
                break;

              cur = tmp->data;
            }

          compare = compare->next;
        }

      tmp = tmp->next;
    }

  return strut_rects;
}

gint
meta_rectangle_edge_cmp_ignore_type (gconstpointer a, gconstpointer b)
{
  const MetaEdge *a_edge_rect = (gconstpointer) a;
  const MetaEdge *b_edge_rect = (gconstpointer) b;
  int a_compare, b_compare;

  /* Edges must be both vertical or both horizontal, or it doesn't make
   * sense to compare them.
   */
  g_assert ((a_edge_rect->rect.width  == 0 && b_edge_rect->rect.width == 0) ||
            (a_edge_rect->rect.height == 0 && b_edge_rect->rect.height == 0));

  a_compare = b_compare = 0;  /* gcc-3.4.2 sucks at figuring initialized'ness */

  if (a_edge_rect->side_type == META_SIDE_LEFT ||
      a_edge_rect->side_type == META_SIDE_RIGHT)
    {
      a_compare = a_edge_rect->rect.x;
      b_compare = b_edge_rect->rect.x;
      if (a_compare == b_compare)
        {
          a_compare = a_edge_rect->rect.y;
          b_compare = b_edge_rect->rect.y;
        }
    }
  else if (a_edge_rect->side_type == META_SIDE_TOP ||
           a_edge_rect->side_type == META_SIDE_BOTTOM)
    {
      a_compare = a_edge_rect->rect.y;
      b_compare = b_edge_rect->rect.y;
      if (a_compare == b_compare)
        {
          a_compare = a_edge_rect->rect.x;
          b_compare = b_edge_rect->rect.x;
        }
    }
  else
    g_assert ("Some idiot wanted to sort sides of different types.\n");

  return a_compare - b_compare; /* positive value denotes a > b ... */
}

/* To make things easily testable, provide a nice way of sorting edges */
gint
meta_rectangle_edge_cmp (gconstpointer a, gconstpointer b)
{
  const MetaEdge *a_edge_rect = (gconstpointer) a;
  const MetaEdge *b_edge_rect = (gconstpointer) b;

  int a_compare, b_compare;

  a_compare = a_edge_rect->side_type;
  b_compare = b_edge_rect->side_type;

  if (a_compare == b_compare)
    return meta_rectangle_edge_cmp_ignore_type (a, b);

  return a_compare - b_compare; /* positive value denotes a > b ... */
}

/* Determine whether two given edges overlap */
static gboolean
edges_overlap (const MetaEdge *edge1,
               const MetaEdge *edge2)
{
  if (edge1->rect.width == 0 && edge2->rect.width == 0)
    {
      return meta_rectangle_vert_overlap (&edge1->rect, &edge2->rect) &&
             edge1->rect.x == edge2->rect.x;
    }
  else if (edge1->rect.height == 0 && edge2->rect.height == 0)
    {
      return meta_rectangle_horiz_overlap (&edge1->rect, &edge2->rect) &&
             edge1->rect.y == edge2->rect.y;
    }
  else
    {
      return FALSE;
    }
}

static gboolean
rectangle_and_edge_intersection (const MetaRectangle *rect,
                                 const MetaEdge      *edge,
                                 MetaEdge            *overlap,
                                 int                 *handle_type)
{
  const MetaRectangle *rect2  = &edge->rect;
  MetaRectangle *result = &overlap->rect;
  gboolean intersect = TRUE;

  /* We don't know how to set these, so set them to invalid values */
  overlap->edge_type = -1;
  overlap->side_type = -1;

  /* Figure out what the intersection is */
  result->x = MAX (rect->x, rect2->x);
  result->y = MAX (rect->y, rect2->y);
  result->width  = MIN (BOX_RIGHT (*rect),  BOX_RIGHT (*rect2))  - result->x;
  result->height = MIN (BOX_BOTTOM (*rect), BOX_BOTTOM (*rect2)) - result->y;

  /* Find out if the intersection is empty; have to do it this way since
   * edges have a thickness of 0
   */
  if ((result->width <  0 || result->height <  0) ||
      (result->width == 0 && result->height == 0))
    {
      result->width = 0;
      result->height = 0;
      intersect = FALSE;
    }
  else
    {
      /* Need to figure out the handle_type, a somewhat weird quantity:
       *   0 - overlap is in middle of rect
       *  -1 - overlap is at the side of rect, and is on the opposite side
       *       of rect than the edge->side_type side
       *   1 - overlap is at the side of rect, and the side of rect it is
       *       on is the edge->side_type side
       */
      switch (edge->side_type)
        {
        case META_SIDE_LEFT:
          if (result->x == rect->x)
            *handle_type = 1;
          else if (result->x == BOX_RIGHT (*rect))
            *handle_type = -1;
          else
            *handle_type = 0;
          break;
        case META_SIDE_RIGHT:
          if (result->x == rect->x)
            *handle_type = -1;
          else if (result->x == BOX_RIGHT (*rect))
            *handle_type = 1;
          else
            *handle_type = 0;
          break;
        case META_SIDE_TOP:
          if (result->y == rect->y)
            *handle_type = 1;
          else if (result->y == BOX_BOTTOM (*rect))
            *handle_type = -1;
          else
            *handle_type = 0;
          break;
        case META_SIDE_BOTTOM:
          if (result->y == rect->y)
            *handle_type = -1;
          else if (result->y == BOX_BOTTOM (*rect))
            *handle_type = 1;
          else
            *handle_type = 0;
          break;
        default:
          g_assert_not_reached ();
        }
    }
  return intersect;
}

/* Add all edges of the given rect to cur_edges and return the result.  If
 * rect_is_internal is false, the side types are switched (LEFT<->RIGHT and
 * TOP<->BOTTOM).
 */
static GList*
add_edges (GList               *cur_edges,
           const MetaRectangle *rect,
           gboolean             rect_is_internal)
{
  int i;

  for (i=0; i<4; i++)
    {
      MetaEdge *temp_edge;

      temp_edge = g_new (MetaEdge, 1);
      temp_edge->rect = *rect;
      switch (i)
        {
        case 0:
          temp_edge->side_type =
            rect_is_internal ? META_SIDE_LEFT : META_SIDE_RIGHT;
          temp_edge->rect.width = 0;
          break;
        case 1:
          temp_edge->side_type =
            rect_is_internal ? META_SIDE_RIGHT : META_SIDE_LEFT;
          temp_edge->rect.x     += temp_edge->rect.width;
          temp_edge->rect.width  = 0;
          break;
        case 2:
          temp_edge->side_type =
            rect_is_internal ? META_SIDE_TOP : META_SIDE_BOTTOM;
          temp_edge->rect.height = 0;
          break;
        case 3:
          temp_edge->side_type =
            rect_is_internal ? META_SIDE_BOTTOM : META_SIDE_TOP;
          temp_edge->rect.y      += temp_edge->rect.height;
          temp_edge->rect.height  = 0;
          break;
        }
      temp_edge->edge_type = META_EDGE_SCREEN;
      cur_edges = g_list_prepend (cur_edges, temp_edge);
    }

  return cur_edges;
}

/* Remove any part of old_edge that intersects remove and add any resulting
 * edges to cur_list.  Return cur_list when finished.
 */
static GList*
split_edge (GList *cur_list,
            const MetaEdge *old_edge,
            const MetaEdge *remove)
{
  MetaEdge *temp_edge;
  switch (old_edge->side_type)
    {
    case META_SIDE_LEFT:
    case META_SIDE_RIGHT:
      g_assert (meta_rectangle_vert_overlap (&old_edge->rect, &remove->rect));
      if (BOX_TOP (old_edge->rect)  < BOX_TOP (remove->rect))
        {
          temp_edge = g_new (MetaEdge, 1);
          *temp_edge = *old_edge;
          temp_edge->rect.height = BOX_TOP (remove->rect)
                                 - BOX_TOP (old_edge->rect);
          cur_list = g_list_prepend (cur_list, temp_edge);
        }
      if (BOX_BOTTOM (old_edge->rect) > BOX_BOTTOM (remove->rect))
        {
          temp_edge = g_new (MetaEdge, 1);
          *temp_edge = *old_edge;
          temp_edge->rect.y      = BOX_BOTTOM (remove->rect);
          temp_edge->rect.height = BOX_BOTTOM (old_edge->rect)
                                 - BOX_BOTTOM (remove->rect);
          cur_list = g_list_prepend (cur_list, temp_edge);
        }
      break;
    case META_SIDE_TOP:
    case META_SIDE_BOTTOM:
      g_assert (meta_rectangle_horiz_overlap (&old_edge->rect, &remove->rect));
      if (BOX_LEFT (old_edge->rect)  < BOX_LEFT (remove->rect))
        {
          temp_edge = g_new (MetaEdge, 1);
          *temp_edge = *old_edge;
          temp_edge->rect.width = BOX_LEFT (remove->rect)
                                - BOX_LEFT (old_edge->rect);
          cur_list = g_list_prepend (cur_list, temp_edge);
        }
      if (BOX_RIGHT (old_edge->rect) > BOX_RIGHT (remove->rect))
        {
          temp_edge = g_new (MetaEdge, 1);
          *temp_edge = *old_edge;
          temp_edge->rect.x     = BOX_RIGHT (remove->rect);
          temp_edge->rect.width = BOX_RIGHT (old_edge->rect)
                                - BOX_RIGHT (remove->rect);
          cur_list = g_list_prepend (cur_list, temp_edge);
        }
      break;
    default:
      g_assert_not_reached ();
    }

  return cur_list;
}

/* Split up edge and remove preliminary edges from strut_edges depending on
 * if and how rect and edge intersect.
 */
static void
fix_up_edges (MetaRectangle *rect,         MetaEdge *edge,<--- Parameter 'rect' can be declared as pointer to const<--- Parameter 'edge' can be declared as pointer to const
              GList         **strut_edges, GList    **edge_splits,
              gboolean      *edge_needs_removal)
{
  MetaEdge overlap;
  int      handle_type;

  if (!rectangle_and_edge_intersection (rect, edge, &overlap, &handle_type))
    return;

  if (handle_type == 0 || handle_type == 1)
    {
      /* Put the result of removing overlap from edge into edge_splits */
      *edge_splits = split_edge (*edge_splits, edge, &overlap);
      *edge_needs_removal = TRUE;
    }

  if (handle_type == -1 || handle_type == 1)
    {
      /* Remove the overlap from strut_edges */
      /* First, loop over the edges of the strut */
      GList *tmp = *strut_edges;
      while (tmp)
        {
          MetaEdge *cur = tmp->data;
          /* If this is the edge that overlaps, then we need to split it */
          if (edges_overlap (cur, &overlap))
            {
              GList *delete_me = tmp;

              /* Split this edge into some new ones */
              *strut_edges = split_edge (*strut_edges, cur, &overlap);

              /* Delete the old one */
              tmp = tmp->next;
              g_free (cur);
              *strut_edges = g_list_delete_link (*strut_edges, delete_me);
            }
          else
            tmp = tmp->next;
        }
    }
}

/* This function removes intersections of edges with the rectangles from the
 * list of edges.
 */
GList*
meta_rectangle_remove_intersections_with_boxes_from_edges (
  GList        *edges,
  const GSList *rectangles)
{
  const GSList *rect_iter;
  const int opposing = 1;

  /* Now remove all intersections of rectangles with the edge list */
  rect_iter = rectangles;
  while (rect_iter)
    {
      MetaRectangle *rect = rect_iter->data;<--- Variable 'rect' can be declared as pointer to const
      GList *edge_iter = edges;
      while (edge_iter)
        {
          MetaEdge *edge = edge_iter->data;
          MetaEdge overlap;
          int      handle;
          gboolean edge_iter_advanced = FALSE;

          /* If this edge overlaps with this rect... */
          if (rectangle_and_edge_intersection (rect, edge, &overlap, &handle))
            {

              /* "Intersections" where the edges touch but are opposite
               * sides (e.g. a left edge against the right edge) should not
               * be split.  Note that the comments in
               * rectangle_and_edge_intersection() say that opposing edges
               * occur when handle is -1, BUT you need to remember that we
               * treat the left side of a window as a right edge because
               * it's what the right side of the window being moved should
               * be-resisted-by/snap-to.  So opposing is really 1.  Anyway,
               * we just keep track of it in the opposing constant set up
               * above and if handle isn't equal to that, then we know the
               * edge should be split.
               */
              if (handle != opposing)
                {
                  /* Keep track of this edge so we can delete it below */
                  GList *delete_me = edge_iter;
                  edge_iter = edge_iter->next;
                  edge_iter_advanced = TRUE;

                  /* Split the edge and add the result to beginning of edges */
                  edges = split_edge (edges, edge, &overlap);

                  /* Now free the edge... */
                  g_free (edge);
                  edges = g_list_delete_link (edges, delete_me);
                }
            }

          if (!edge_iter_advanced)
            edge_iter = edge_iter->next;
        }

      rect_iter = rect_iter->next;
    }

  return edges;
}

/* This function is trying to find all the edges of an onscreen region. */
GList*
meta_rectangle_find_onscreen_edges (const MetaRectangle *basic_rect,
                                    const GSList        *all_struts)
{
  GList        *ret;
  GList        *fixed_strut_rects;
  GList        *edge_iter;
  const GList  *strut_rect_iter;

  /* The algorithm is basically as follows:
   *   Make sure the struts are disjoint
   *   Initialize the edge_set to the edges of basic_rect
   *   Foreach strut:
   *     Put together a preliminary new edge from the edges of the strut
   *     Foreach edge in edge_set:
   *       - Split the edge if it is partially contained inside the strut
   *       - If the edge matches an edge of the strut (i.e. a strut just
   *         against the edge of the screen or a not-next-to-edge-of-screen
   *         strut adjacent to another), then both the edge from the
   *         edge_set and the preliminary edge for the strut will need to
   *         be split
   *     Add any remaining "preliminary" strut edges to the edge_set
   */

  /* Make sure the struts are disjoint */
  fixed_strut_rects =
    get_disjoint_strut_rect_list_in_region (all_struts, basic_rect);

  /* Start off the list with the edges of basic_rect */
  ret = add_edges (NULL, basic_rect, TRUE);

  strut_rect_iter = fixed_strut_rects;
  while (strut_rect_iter)
    {
      MetaRectangle *strut_rect = (MetaRectangle*) strut_rect_iter->data;

      /* Get the new possible edges we may need to add from the strut */
      GList *new_strut_edges = add_edges (NULL, strut_rect, FALSE);

      edge_iter = ret;
      while (edge_iter)
        {
          MetaEdge *cur_edge = edge_iter->data;
          GList *splits_of_cur_edge = NULL;
          gboolean edge_needs_removal = FALSE;

          fix_up_edges (strut_rect,       cur_edge,
                        &new_strut_edges, &splits_of_cur_edge,
                        &edge_needs_removal);

          if (edge_needs_removal)
            {
              /* Delete the old edge */
              GList *delete_me = edge_iter;
              edge_iter = edge_iter->next;
              g_free (cur_edge);
              ret = g_list_delete_link (ret, delete_me);

              /* Add the new split parts of the edge */
              ret = g_list_concat (splits_of_cur_edge, ret);
            }
          else
            {
              edge_iter = edge_iter->next;
            }

          /* edge_iter was already advanced above */
        }

      ret = g_list_concat (new_strut_edges, ret);
      strut_rect_iter = strut_rect_iter->next;
    }

  /* Sort the list */
  ret = g_list_sort (ret, meta_rectangle_edge_cmp);

  /* Free the fixed struts list */
  g_list_free_full (fixed_strut_rects, g_free);

  return ret;
}

GList*
meta_rectangle_find_nonintersected_xinerama_edges (
                                    const MetaRectangle *screen_rect,
                                    const GList         *xinerama_rects,
                                    const GSList        *all_struts)
{
  /* This function cannot easily be merged with
   * meta_rectangle_find_onscreen_edges() because real screen edges
   * and strut edges both are of the type "there ain't anything
   * immediately on the other side"; xinerama edges are different.
   */
  GList *ret;
  const GList  *cur;
  GSList *temp_rects;

  /* Initialize the return list to be empty */
  ret = NULL;

  /* start of ret with all the edges of xineramas that are adjacent to
   * another xinerama.
   */
  cur = xinerama_rects;
  while (cur)
    {
      MetaRectangle *cur_rect = cur->data;
      MetaEdge *new_edge;
      if (BOX_LEFT(*cur_rect) != BOX_LEFT(*screen_rect))
        {
          new_edge  = g_new (MetaEdge, 1);
          new_edge->rect = meta_rect (BOX_LEFT (*cur_rect), BOX_TOP (*cur_rect), 0, cur_rect->height);
          new_edge->side_type = META_SIDE_LEFT;
          new_edge->edge_type = META_EDGE_XINERAMA;
          ret = g_list_prepend (ret, new_edge);
        }
      if (BOX_RIGHT(*cur_rect) != BOX_RIGHT(*screen_rect))
        {
          new_edge  = g_new (MetaEdge, 1);
          new_edge->rect = meta_rect (BOX_RIGHT (*cur_rect), BOX_TOP (*cur_rect), 0, cur_rect->height);
          new_edge->side_type = META_SIDE_RIGHT;
          new_edge->edge_type = META_EDGE_XINERAMA;
          ret = g_list_prepend (ret, new_edge);
        }
      if (BOX_TOP(*cur_rect) != BOX_TOP(*screen_rect))
        {
          new_edge  = g_new (MetaEdge, 1);
          new_edge->rect = meta_rect (BOX_LEFT (*cur_rect), BOX_TOP (*cur_rect), cur_rect->width, 0);
          new_edge->side_type = META_SIDE_TOP;
          new_edge->edge_type = META_EDGE_XINERAMA;
          ret = g_list_prepend (ret, new_edge);
        }
      if (BOX_BOTTOM(*cur_rect) != BOX_BOTTOM(*screen_rect))
        {
          new_edge  = g_new (MetaEdge, 1);
          new_edge->rect = meta_rect (BOX_LEFT (*cur_rect), BOX_BOTTOM (*cur_rect), cur_rect->width, 0);
          new_edge->side_type = META_SIDE_BOTTOM;
          new_edge->edge_type = META_EDGE_XINERAMA;
          ret = g_list_prepend (ret, new_edge);
        }
      cur = cur->next;
    }

  temp_rects = NULL;
  for (; all_struts; all_struts = all_struts->next)
    temp_rects = g_slist_prepend (temp_rects,
                                  &((MetaStrut*)all_struts->data)->rect);
  ret = meta_rectangle_remove_intersections_with_boxes_from_edges (ret,
                                                                   temp_rects);
  g_slist_free (temp_rects);

  /* Sort the list */
  ret = g_list_sort (ret, meta_rectangle_edge_cmp);

  return ret;
}