This file is indexed.

/usr/share/common-lisp/source/mcclim/regions.lisp is in cl-mcclim 0.9.6.dfsg.cvs20100315-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: CLIM-INTERNALS; -*-
;;; --------------------------------------------------------------------------------------
;;;     Title: The CLIM Region Datatype
;;;   Created: 1998-12-02 19:26
;;;    Author: Gilbert Baumann <unk6@rz.uni-karlsruhe.de>
;;;   License: LGPL (See file COPYING for details).
;;;       $Id: regions.lisp,v 1.39 2009-06-03 20:33:16 ahefner Exp $
;;; --------------------------------------------------------------------------------------
;;;  (c) copyright 1998,1999,2001 by Gilbert Baumann
;;;  (c) copyright 2001 by Arnaud Rouanet (rouanet@emi.u-bordeaux.fr)

;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library 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
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the 
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
;;; Boston, MA  02111-1307  USA.

;;;; Changes

;;;  When        Who    What
;;; --------------------------------------------------------------------------------------
;;;  2002-06-27  GB     REGION-INTERSECTS-REGION-P has an :around method on bounding
;;;                     rectangles.
;;;  2002-06-04  APD    partially fixed (BOUNDING-RECTANGLE* STANDARD-ELLIPSE)
;;;  2001-07-16  GB     added (REGION-CONTAINS-POSITION-P STANDARD-ELLIPSE ..)
;;;                     added (BOUNDING-RECTANGLE* STANDARD-ELLIPSE)
;;;                     added (REGION-INTERSECTION LINE STANDARD-ELLIPSE) and vice versa
;;;  2001-07-12  GB     fixed bugs in
;;;                     (BOUNDING-RECTANGLE* STANDARD-REGION-UNION)
;;;                     (BOUNDING-RECTANGLE* STANDARD-REGION-INTERSECTION)
;;;  2001-07-09  GB     maybe fixed a bug in MAP-OVER-SCHNITT-GERADE/POLYGON.
;;;  2001-03-09  AR     fixed a bug in MAKE-ELLIPICAL-THING
;;;                     fixed STANDARD-ELLIPTICAL-ARC defclass
;;;  2001-03-06  AR     fixed bug in (REGION-EQUAL STANDARD-RECTANGLE STANDARD-RECTANGLE)
;;;                     REGION is now a subclass of DESIGN.
;;;  2001-01-21  GB     fixed bug in (TRANSFORM-REGION T RECTANGLE-SET)
;;;                     added some documentation

;;;  GB = Gilbert Baumann <unk6@rz.uni-karlsruhe.de>
;;;  AR = Arnaud Rouanet <rouanet@emi.u-bordeaux.fr>

;;; ---- TODO ----------------------------------------------------------------------------

;; - ellipses: The intersection of two ellipses is there, but
;;   handling the start/end angle is not implemented.

;; - This code is anything else than well organized.

;; - provide better (faster) implementations for REGION-EQUAL,
;;   REGION-CONTAINS-REGION-P, and REGION-INTERSECTS-REGION-P.

;; - Compute a union/intersection/difference of an union of polygon vs another
;;   polygon or union of polygons directly via POLYGON-OP.

;; - STANDARD-REGION-UNION should either become a subclass
;;   'STANDARD-DISJUNCT-REGION-UNION' or a flag. Some set operations could take
;;   advantage out the information, if the subregions of an union are disjunct.

;; - provide sensible PRINT-OBJECT methods.

;; - while you are are at it; provide a reasonable fast vertical scan routine.
;;   polygons should make use of the sweep line algorithm.

;; - implement bounding rectangle cache for polygons and polylines

;; - make REGION-CONTAINS-POSITION-P for polygons faster by handling the special
;;   case of the intersection of a horizontal line and the polygons

;; - MAKE-POLY{LINE,GON} should canonise its arguments; no edges of length 0 and
;;   no co-linear vertexes. Maybe: canonise rectangles? Also a polygon of less
;;   than three vertexes is to be considered empty aka +nowhere+.

(in-package :clim-internals)

(defclass nowhere-region (region nowhere-mixin) ())
(defclass everywhere-region (region everywhere-mixin) ())

;; coordinate is defined in coordinates.lisp

(defvar +everywhere+ (make-instance 'everywhere-region))
(defvar +nowhere+ (make-instance 'nowhere-region))

(defmethod bounding-rectangle* ((x nowhere-region))
  (values 0 0 0 0))

;; 2.5.1.1 Region Predicates in CLIM

(defgeneric region-equal (region1 region2))
(defgeneric region-contains-region-p (region1 region2))
(defgeneric region-contains-position-p (region x y))
(defgeneric region-intersects-region-p (region1 region2))

;; 2.5.1.2 Composition of CLIM Regions

(defclass standard-region-union (region-set) 
  ((regions :initarg :regions :reader standard-region-set-regions)))

(defclass standard-region-intersection (region-set) 
  ((regions :initarg :regions :reader standard-region-set-regions)))

(defclass standard-region-difference (region-set) 
  ((a :initarg :a :reader standard-region-difference-a)
   (b :initarg :b :reader standard-region-difference-b)))

;; Protocol:
(defgeneric region-set-regions (region &key normalize))
(defgeneric map-over-region-set-regions (function region &key normalize))
(defgeneric region-union (region1 region2))
(defgeneric region-intersection (region1 region2))
(defgeneric region-difference (region1 region2))

;;; ---- 2.5.2 CLIM Point Objects --------------------------------------------------------

(defclass standard-point (point)
  ((x :type coordinate :initarg :x)
   (y :type coordinate :initarg :y)))

(defun make-point (x y)
  (make-instance 'standard-point :x (coerce x 'coordinate) :y (coerce y 'coordinate)))

(defmethod print-object ((self standard-point) sink)
  (with-slots (x y) self
    (format sink "#<~S ~S ~S>" 'standard-point x y)))

;; Point protocol: point-position

(defgeneric point-position (point))

(defmethod point-position ((self standard-point))
  (with-slots (x y) self
    (values x y)))

(defmethod point-x ((self point))
  (nth-value 0 (point-position self)))

(defmethod point-y ((self point))
  (nth-value 1 (point-position self)))

(defmethod transform-region (transformation (self standard-point))
  (with-slots (x y) self
    (multiple-value-bind (x* y*) (transform-position transformation x y)
      (make-point x* y*))))

(defmethod region-contains-position-p ((self standard-point) px py)
  (with-slots (x y) self
    (and (coordinate= x px) (coordinate= y py))))

;;; ---- 2.5.3 Polygons and Polylines in CLIM --------------------------------------------

;; Protocol: 
(defclass standard-polyline (polyline)
  ((points :initarg :points)
   (closed :initarg :closed)))

(defclass standard-polygon (polygon)
  ((points :initarg :points)) )

;;; ---- 2.5.3.1 Constructors for CLIM Polygons and Polylines  ---------------------------

(defun coord-seq->point-seq (sequence)
  (let ((res nil))
    (do-sequence ((x y) sequence)
      (push (make-point x y) res))
    (nreverse res)))

(defun make-polyline (point-seq &key closed)
  (assert (every #'pointp point-seq))
  (setq point-seq (coerce point-seq 'list))
  (cond ((every (lambda (x) (region-equal x (car point-seq)))
                (cdr point-seq))
         +nowhere+)
        (t
         (make-instance 'standard-polyline :points point-seq :closed closed))))

(defun make-polyline* (coord-seq &key closed)
  (make-polyline (coord-seq->point-seq coord-seq) :closed closed))

(defun make-polygon (point-seq)
  (assert (every #'pointp point-seq))
  (setq point-seq (coerce point-seq 'list))
  (cond ((every (lambda (x) (region-equal x (car point-seq)))
                (cdr point-seq))
         +nowhere+)
        (t
         (make-instance 'standard-polygon :points point-seq))))

(defun make-polygon* (coord-seq)
  (make-polygon (coord-seq->point-seq coord-seq)))

(defmethod polygon-points ((self standard-polygon))
  (with-slots (points) self
    points))

(defmethod map-over-polygon-coordinates (fun (self standard-polygon))
  (with-slots (points) self
    (mapc (lambda (p) (funcall fun (point-x p) (point-y p))) points)))

(defmethod map-over-polygon-segments (fun (self standard-polygon))
  (with-slots (points) self
    (do ((q points (cdr q)))
        ((null (cdr q))
         (funcall fun (point-x (car q)) (point-y (car q)) (point-x (car points)) (point-y (car points))))
      (funcall fun (point-x (car q)) (point-y (car q)) (point-x (cadr q)) (point-y (cadr q))))))

(defmethod polygon-points ((self standard-polyline))
  (with-slots (points) self
    points))

(defmethod map-over-polygon-coordinates (fun (self standard-polyline))
  (with-slots (points) self
    (mapc (lambda (p) (funcall fun (point-x p) (point-y p))) points)))

(defmethod map-over-polygon-segments (fun (self standard-polyline))
  (with-slots (points closed) self
    (do ((q points (cdr q)))
        ((null (cdr q))
         (when closed
           (funcall fun (point-x (car q)) (point-y (car q)) (point-x (car points)) (point-y (car points)))))
      (funcall fun (point-x (car q)) (point-y (car q)) (point-x (cadr q)) (point-y (cadr q))))))

(defmethod polyline-closed ((self standard-polyline))
  (with-slots (closed) self
    closed))

(defmethod transform-region (transformation (self standard-polyline))
  (with-slots (points closed) self
    (make-polyline (mapcar (lambda (p)
                             (multiple-value-bind (x* y*) (transform-position transformation (point-x p) (point-y p))
                               (make-point x* y*)))
                           points)
                   :closed closed)))

(defmethod transform-region (transformation (self standard-polygon))
  (with-slots (points) self
    (make-polygon (mapcar (lambda (p)
                            (multiple-value-bind (x* y*) (transform-position transformation (point-x p) (point-y p))
                              (make-point x* y*)))
                          points))))

(defmethod region-contains-position-p ((self standard-polyline) x y)
  (setf x (coerce x 'coordinate)
        y (coerce y 'coordinate))
  (block nil
    (map-over-polygon-segments (lambda (x1 y1 x2 y2)
                                 (when (line-contains-point-p* x1 y1 x2 y2 x y)
                                   (return t)))
                               self)
    nil))

(defun line-contains-point-p* (x1 y1 x2 y2 px py)
  (and (or (<= x1 px x2) (>= x1 px x2))
       (or (<= y1 py y2) (>= y1 py y2))
       (coordinate= (* (- py y1) (- x2 x1))
                    (* (- px x1) (- y2 y1)))))

(defun line-contains-point-p** (x1 y1 x2 y2 px py)
  (coordinate= (* (- py y1) (- x2 x1))
               (* (- px x1) (- y2 y1))))


;;; ---- 2.5.4 Lines in CLIM -------------------------------------------------------------

;; Line protocol: line-start-point* line-end-point* 

(defclass standard-line (line)
  ((x1 :type coordinate :initarg :x1)
   (y1 :type coordinate :initarg :y1)
   (x2 :type coordinate :initarg :x2)
   (y2 :type coordinate :initarg :y2)))

(defun make-line (start-point end-point)
  (make-line* (point-x start-point) (point-y start-point) (point-x end-point) (point-y end-point)))

(defun make-line* (start-x start-y end-x end-y)
  (setf start-x (coerce start-x 'coordinate)
        start-y (coerce start-y 'coordinate)
        end-x (coerce end-x 'coordinate)
        end-y (coerce end-y 'coordinate))
  (if (and (coordinate= start-x end-x)
           (coordinate= start-y end-y))
      +nowhere+
    (make-instance 'standard-line :x1 start-x :y1 start-y :x2 end-x :y2 end-y)))

(defmethod line-start-point* ((line standard-line))
  (with-slots (x1 y1 x2 y2) line
    (values x1 y1)))

(defmethod line-end-point* ((line standard-line))
  (with-slots (x1 y1 x2 y2) line
    (values x2 y2)))

(defmethod line-start-point ((line line))
  (multiple-value-bind (x y) (line-start-point* line)
    (make-point x y)))

(defmethod line-end-point ((line line))
  (multiple-value-bind (x y) (line-end-point* line)
    (make-point x y)))

;; polyline protocol for standard-line's:

(defmethod polygon-points ((line standard-line))
  (with-slots (x1 y1 x2 y2) line
    (list (make-point x1 y1) (make-point x2 y2))))

(defmethod map-over-polygon-coordinates (fun (line standard-line))
  (with-slots (x1 y1 x2 y2) line
    (funcall fun x1 y1)
    (funcall fun x2 y2)))

(defmethod map-over-polygon-segments (fun (line standard-line))
  (with-slots (x1 y1 x2 y2) line
    (funcall fun x1 y1 x2 y2)))

(defmethod polyline-closed ((line standard-line))
  nil)

(defmethod transform-region (transformation (line standard-line))
  (with-slots (x1 y1 x2 y2) line
    (multiple-value-bind (x1* y1*) (transform-position transformation x1 y1)
      (multiple-value-bind (x2* y2*) (transform-position transformation x2 y2)
        (make-line* x1* y1* x2* y2*)))))

(defmethod region-contains-position-p ((self standard-line) x y)
  (multiple-value-bind (x1 y1) (line-start-point* self)
    (multiple-value-bind (x2 y2) (line-end-point* self)
      (line-contains-point-p* x1 y1 x2 y2 x y))))

(defmethod print-object ((self standard-line) sink)
  (with-slots (x1 y1 x2 y2) self
    (format sink "#<~S ~D ~D ~D ~D>" (type-of self) x1 y1 x2 y2)))

;;; ---- 2.5.5 Rectangles in CLIM --------------------------------------------------------

;; protocol:
;;     rectangle-edges*

(defclass standard-rectangle (rectangle)
  ((coordinates :initform (make-array 4 :element-type 'coordinate))))

(defmethod initialize-instance :after ((obj standard-rectangle)
				       &key (x1 0.0d0) (y1 0.0d0)
				       (x2 0.0d0) (y2 0.0d0))
  (let ((coords (slot-value obj 'coordinates)))
    (setf (aref coords 0) x1)
    (setf (aref coords 1) y1)
    (setf (aref coords 2) x2)
    (setf (aref coords 3) y2)))

(defmacro with-standard-rectangle ((x1 y1 x2 y2) rectangle &body body)
  (with-gensyms (coords)
    `(let ((,coords (slot-value ,rectangle 'coordinates)))
       (declare (type (simple-array coordinate (4)) ,coords))
       (let ((,x1 (aref ,coords 0))
	     (,y1 (aref ,coords 1))
	     (,x2 (aref ,coords 2))
	     (,y2 (aref ,coords 3)))
	 (declare (type coordinate ,x1 ,y1 ,x2 ,y2))
	 ,@body))))

(defmacro with-standard-rectangle* ((&key x1 y1 x2 y2) rectangle &body body)
  (with-gensyms (coords)
    `(let ((,coords (slot-value ,rectangle 'coordinates)))
       (declare (type (simple-array coordinate (4)) ,coords))
       (let (,@(and x1 `((,x1 (aref ,coords 0))))
	     ,@(and y1 `((,y1 (aref ,coords 1))))
	     ,@(and x2 `((,x2 (aref ,coords 2))))
	     ,@(and y2 `((,y2 (aref ,coords 3)))))
	 (declare (type coordinate
			,@(and x1 `(,x1))
			,@(and y1 `(,y1))
			,@(and x2 `(,x2))
			,@(and y2 `(,y2))))
	 ,@body))))

(defun make-rectangle (point1 point2)
  (make-rectangle* (point-x point1) (point-y point1) (point-x point2) (point-y point2)))

(defun make-rectangle* (x1 y1 x2 y2)
  (psetq x1 (coerce (min x1 x2) 'coordinate)
         x2 (coerce (max x1 x2) 'coordinate)
         y1 (coerce (min y1 y2) 'coordinate)
	 y2 (coerce (max y1 y2) 'coordinate))
  (if (or (coordinate= x1 x2)
          (coordinate= y1 y2))
      +nowhere+
    (make-instance 'standard-rectangle :x1 x1 :x2 x2 :y1 y1 :y2 y2)))

(defmethod rectangle-edges* ((rect standard-rectangle))
  (with-standard-rectangle (x1 y1 x2 y2)
      rect
    (values x1 y1 x2 y2)))

;;; standard-rectangles are immutable and all that, but we still need to set
;;; their positions and dimensions (in output recording)
(defgeneric* (setf rectangle-edges*) (x1 y1 x2 y2 rectangle))

(defmethod* (setf rectangle-edges*)
  (x1 y1 x2 y2 (rectangle standard-rectangle))
  (let ((coords (slot-value rectangle 'coordinates)))
    (declare (type (simple-array coordinate (4)) coords))
    (setf (aref coords 0) x1)
    (setf (aref coords 1) y1)
    (setf (aref coords 2) x2)
    (setf (aref coords 3) y2))
  (values x1 y1 x2 y2))

(defmethod rectangle-min-point ((rect rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
    (declare (ignore x2 y2))
    (make-point x1 y1)))

(defmethod rectangle-min-point ((rect standard-rectangle))
  (with-standard-rectangle* (:x1 x1 :y1 y1)
      rect
    (make-point x1 y1)))

(defmethod rectangle-max-point ((rect rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
    (declare (ignore x1 y1))
    (make-point x2 y2)))

(defmethod rectangle-max-point ((rect standard-rectangle))
  (with-standard-rectangle* (:x2 x2 :y2 y2)
      rect
    (make-point x2 y2)))

(defmethod rectangle-min-x ((rect rectangle))
  (nth-value 0 (rectangle-edges* rect)))

(defmethod rectangle-min-x ((rect standard-rectangle))
  (with-standard-rectangle* (:x1 x1)
      rect
    x1))

(defmethod rectangle-min-y ((rect rectangle))
  (nth-value 1 (rectangle-edges* rect)))

(defmethod rectangle-min-y ((rect standard-rectangle))
  (with-standard-rectangle* (:y1 y1)
      rect
    y1))


(defmethod rectangle-max-x ((rect rectangle))
  (nth-value 2 (rectangle-edges* rect)))

(defmethod rectangle-max-x ((rect standard-rectangle))
  (with-standard-rectangle* (:x2 x2)
      rect
    x2))

(defmethod rectangle-max-y ((rect rectangle))
  (nth-value 3 (rectangle-edges* rect)))

(defmethod rectangle-max-y ((rect standard-rectangle))
  (with-standard-rectangle* (:y2 y2)
      rect
    y2))

(defmethod rectangle-width ((rect rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
    (declare (ignore y1 y2))
    (- x2 x1)))

(defmethod rectangle-width ((rect standard-rectangle))
  (with-standard-rectangle* (:x1 x1 :x2 x2)
      rect
    (- x2 x1)))

(defmethod rectangle-height ((rect rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
    (declare (ignore x1 x2))
    (- y2 y1)))

(defmethod rectangle-height ((rect standard-rectangle))
  (with-standard-rectangle* (:y1 y1 :y2 y2)
      rect
    (- y2 y1)))

(defmethod rectangle-size ((rect rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
    (values (- x2 x1) (- y2 y1))))

(defmethod rectangle-size ((rect standard-rectangle))
  (with-standard-rectangle (x1 y1 x2 y2)
      rect
    (values (- x2 x1) (- y2 y1))))

;; polyline/polygon protocol for standard-rectangle's

(defmethod polygon-points ((rect standard-rectangle))
  (with-standard-rectangle (x1 y1 x2 y2)
      rect
    (list (make-point x1 y1)
          (make-point x1 y2)
          (make-point x2 y2)
          (make-point x2 y1))))


(defmethod map-over-polygon-coordinates (fun (rect standard-rectangle))
  (with-standard-rectangle (x1 y1 x2 y2)
      rect
    (funcall fun x1 y1)
    (funcall fun x1 y2)
    (funcall fun x2 y2)
    (funcall fun x2 y1)))

(defmethod map-over-polygon-segments (fun (rect standard-rectangle))
  (with-standard-rectangle (x1 y1 x2 y2)
      rect
    (funcall fun x1 y1 x1 y2)
    (funcall fun x1 y2 x2 y2)
    (funcall fun x2 y2 x2 y1)
    (funcall fun x2 y1 x1 y1)))

(defmethod transform-region (transformation (rect standard-rectangle))
  (cond ((rectilinear-transformation-p transformation)
	 (with-standard-rectangle (x1 y1 x2 y2)
	       rect
           (multiple-value-bind (x1* y1*) (transform-position transformation x1 y1)
             (multiple-value-bind (x2* y2*) (transform-position transformation x2 y2)
               (make-rectangle* x1* y1* x2* y2*)))))
        (t
         (make-polygon (mapcar (lambda (p) (transform-region transformation p)) 
                               (polygon-points rect)))) ))

(defmethod region-contains-position-p ((self standard-rectangle) x y)
  (with-standard-rectangle (x1 y1 x2 y2)
      self
    (and (<= x1 (coerce x 'coordinate) x2)
         (<= y1 (coerce y 'coordinate) y2))))

;;; ---- 2.5.6 Ellipses and Elliptical Arcs in CLIM --------------------------------------

(defclass elliptical-thing ()
  ((start-angle :initarg :start-angle)
   (end-angle   :initarg :end-angle)
   (tr          :initarg :tr)))         ;a transformation from the unit circle to get the elliptical object

(defmethod print-object ((ell elliptical-thing) stream)
  (with-slots (start-angle end-angle tr) ell
    (format stream "#<~A [~A ~A] ~A>"
            (type-of ell)
            (and start-angle (* (/ 180 pi) start-angle))
            (and end-angle (* (/ 180 pi) end-angle))
            tr)))

(defclass standard-ellipse (elliptical-thing ellipse) ())
(defclass standard-elliptical-arc (elliptical-thing elliptical-arc) ())

;;; ---- 2.5.6.1 Constructor Functions for Ellipses and Elliptical Arcs in CLIM  ---------

(defun make-ellipse (center-point radius-1-dx radius-1-dy radius-2-dx radius-2-dy &key start-angle end-angle)
  (make-ellipse* (point-x center-point) (point-y center-point)
                 radius-1-dx radius-1-dy radius-2-dx radius-2-dy 
                 :start-angle start-angle 
                 :end-angle end-angle))

(defun make-ellipse* (center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy 
                      &key start-angle end-angle)
  (make-ellipical-thing 'standard-ellipse 
                        center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy 
                        start-angle end-angle))

(defun make-elliptical-arc (center-point radius-1-dx radius-1-dy radius-2-dx radius-2-dy &key start-angle end-angle)
  (make-elliptical-arc* (point-x center-point) (point-y center-point)
                        radius-1-dx radius-1-dy radius-2-dx radius-2-dy 
                        :start-angle start-angle 
                        :end-angle end-angle))

(defun make-elliptical-arc* (center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy 
                             &key start-angle end-angle)
  (make-ellipical-thing 'standard-elliptical-arc 
                        center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy 
                        start-angle end-angle))

(defun make-ellipical-thing (class 
                             center-x center-y radius-1-dx radius-1-dy radius-2-dx radius-2-dy
                             start-angle end-angle)
  (setf center-x (coerce center-x 'coordinate)
        center-y (coerce center-y 'coordinate)
        radius-1-dx (coerce radius-1-dx 'coordinate)
        radius-1-dy (coerce radius-1-dy 'coordinate)
        radius-2-dx (coerce radius-2-dx 'coordinate)
        radius-2-dy (coerce radius-2-dy 'coordinate)
        start-angle (and start-angle (coerce start-angle 'coordinate))
        end-angle (and end-angle (coerce end-angle 'coordinate)) )

  (let ((tr (make-3-point-transformation* 0 0 1 0 0 1
                                          center-x center-y
                                          (+ center-x radius-1-dx) (+ center-y radius-1-dy)
                                          (+ center-x radius-2-dx) (+ center-y radius-2-dy))))
    (cond ((and (null start-angle) (null end-angle)))
          ((null start-angle) (setf start-angle 0))
          ((null end-angle) (setf end-angle (* 2 pi))))
    (make-instance class :tr tr :start-angle start-angle :end-angle end-angle) ))

(defmethod transform-region (transformation (self elliptical-thing))
  (with-slots (start-angle end-angle tr) self
    ;; I think this should be untransform-angle below, as the ellipse angles
    ;; go counter-clockwise in screen coordinates, whereas our transformations
    ;; rotate clockwise..  -Hefner
    (let ((start-angle* (and start-angle (untransform-angle transformation start-angle)))
          (end-angle*   (and end-angle   (untransform-angle transformation end-angle))))      
      (when (reflection-transformation-p transformation)
        (rotatef start-angle* end-angle*))
      (make-instance (type-of self)
        :tr (compose-transformations transformation tr)
        :start-angle start-angle*
        :end-angle end-angle*))))

(defmethod region-contains-position-p ((self standard-ellipse) x y)
  ;; XXX start/end angle still missing
  (with-slots (tr) self
    (multiple-value-bind (x y) (untransform-position tr x y)
      (<= (+ (* x x) (* y y)) 1))))

(defmethod bounding-rectangle* ((region standard-ellipse))
  ;; XXX start/end angle still missing
  (with-slots (tr) region
    (flet ((contact-radius* (x y)
             "Returns coordinates of the radius of the point, in
              which the vector field (x y) touches the ellipse."
             (multiple-value-bind (xc yc) (untransform-distance tr x y)
               (let* ((d (sqrt (+ (* xc xc) (* yc yc))))
                      (xn (- (/ yc d)))
                      (yn (/ xc d)))
                 (transform-distance tr xn yn)))))
      (multiple-value-bind (cx cy) (ellipse-center-point* region)
        (if (zerop (ellipse-radii region))
            (values cx cy cx cy)
            (multiple-value-bind (vdx vdy) (contact-radius* 1 0)
              (declare (ignore vdx))
              (multiple-value-bind (hdx hdy) (contact-radius* 0 1)
                (declare (ignore hdy))
                (let ((rx (abs hdx))
                      (ry (abs vdy)))
                  (values (- cx rx) (- cy ry)
                          (+ cx rx) (+ cy ry))))))))))

(defun intersection-line/unit-circle (x1 y1 x2 y2)
  "Computes the intersection of the line from (x1,y1) to (x2,y2) and the unit circle.
   If the intersection is empty, NIL is returned.
   Otherwise four values are returned: x1, y1, x2, y2; the start and end point of the
   resulting line."
  (let* ((dx (- x2 x1))
         (dy (- y2 y1))
         (a (+ (expt dx 2) (expt dy 2)))
         (b (+ (* 2 x1 dx) (* 2 y1 dy)))
         (c (+ (expt x1 2) (expt y1 2) -1)))
    (let ((s1 (- (/ (+ (sqrt (- (expt b 2) (* 4 a c))) b) (* 2 a))))
          (s2 (- (/ (- b (sqrt (- (expt b 2) (* 4 a c)))) (* 2 a)))))
      (cond ((and (realp s1) (realp s2)
                  (not (and (< s1 0) (< s2 0)))
                  (not (and (> s1 1) (> s2 1))))
             (let ((s1 (max 0 (min 1 s1)))
                   (s2 (max 0 (min 1 s2))))
               (values (+ x1 (* s1 dx))
                       (+ y1 (* s1 dy))
                       (+ x1 (* s2 dx))
                       (+ y1 (* s2 dy)))))
            (t
             nil)))))

(defmethod region-intersection ((line line) (ellipse standard-ellipse))
  (with-slots (tr) ellipse
    (multiple-value-bind (x1 y1 x2 y2)
        (multiple-value-call #'intersection-line/unit-circle
                             (multiple-value-call #'untransform-position tr (line-start-point* line))
                             (multiple-value-call #'untransform-position tr (line-end-point* line)))
      (if x1
          (multiple-value-call #'make-line*
                               (transform-position tr x1 y1)
                               (transform-position tr x2 y2))
        +nowhere+))))

(defmethod region-intersection ((ellipse standard-ellipse) (line standard-line))
  (region-intersection ellipse line))

;;; ---- 2.5.6.2 Accessors for CLIM Elliptical Objects -----------------------------------

(defmethod ellipse-center-point* ((self elliptical-thing))
  (with-slots (tr) self
    (transform-position tr 0 0)))

(defmethod ellipse-center-point ((self elliptical-thing))
  (with-slots (tr) self
    (transform-region tr (make-point 0 0))))

(defmethod ellipse-radii ((self elliptical-thing))
  (with-slots (tr) self
    (multiple-value-bind (dx1 dy1) (transform-distance tr 1 0)
      (multiple-value-bind (dx2 dy2) (transform-distance tr 0 1)
        (values dx1 dy1 dx2 dy2)))))

(defmethod ellipse-start-angle ((self elliptical-thing))
  (with-slots (start-angle) self 
    start-angle))

(defmethod ellipse-end-angle ((self elliptical-thing))
  (with-slots (end-angle) self 
    end-angle))



(defun ellipse-coefficients (ell)
  ;; Returns the coefficients of the equation specifing the ellipse as in
  ;;  ax^2 + by^2 + cxy + dx + dy - f = 0

  ;; Note 1:
  ;;   The `f' here may seem to be superfluous, since you
  ;;   could simply multiply the whole equation by 1/f. But this is
  ;;   not the case, since `f' may as well be 0.

  ;; Note 2:
  ;;   In the literature you often find something like
  ;;   (x^2)/a + (y^2)/b - 1 = 0 for an axis aligned ellipse, but
  ;;   I rather choose to treat all coefficients as simple factors instead
  ;;   of denominators.

  (with-slots (tr) ell
    ;;warum die inverse hier?
    (multiple-value-bind (a b d e c f) (get-transformation (invert-transformation tr))
      (values
       (+ (* a a) (* d d))              ; x**2
       (+ (* b b) (* e e))              ; y**2
       (+ (* 2 a b) (* 2 d e))          ; xy
       (+ (* 2 a c) (* 2 d f))          ; x
       (+ (* 2 b c) (* 2 e f))          ; y
       (+ (* c c) (* f f) -1)))) )

;;; Straight from the horse's mouth -- moore
;;;
;;; Axis of an ellipse
;;; -------------------------

;; Given an ellipse with its center at the origin, as

;;    ax^2 + by^2 + cxy - 1 = 0

;; The two axis of an ellipse are characterized by minimizing and
;; maximizing the radius. Let (x,y) be a point on the delimiter of the
;; ellipse. It's radius (distance from the origin) then is:

;;    r^2 = x^2 + y^2

;; To find the axis can now be stated as an minimization problem with
;; constraints. So mechanically construct the auxiliarry function H:

;;   H = x^2 + y^2 - k(ax^2 + by^2 + cxy - 1)

;; So the following set of equations remain to be solved

;;   (I)   dH/dx = 0 = 2x + 2kax + kcy 
;;  (II)   dH/dy = 0 = 2y + 2kby + kcx
;; (III)   dH/dk = 0 = ax^2 + by^2 + cxy - 1

;; Unfortunately, as I always do the math work - hopelessly, even -
;; Maxima is the tool of my choice:

;; g1: 2*x + 2*k*a*x + k*c*y$
;; g2: 2*y + 2*k*b*y + k*c*x$
;; g3: a*x*x + b*y*y + c*x*y -1$

;; sol1: solve ([g1,g2],[k,y])$

;; /* This yields two solutions because of the squares with occur. The
;;  * last equation (G3) must therefore be handled for both solutions for
;;  * y.
;;  */

;; y1: rhs(first(rest(first(sol1))))$
;; y2: rhs(first(rest(first(rest(sol1)))))$

;; /* Substitute the 'y' found. */
;; sol2: solve(subst(y1,y,g3),x);
;; x11: rhs(first(sol2));
;; x12: rhs(first(rest(sol2)));

;; sol3: solve(subst(y2,y,g3),x);
;; x21: rhs(first(sol3));
;; x22: rhs(first(rest(sol3)));

;; /* dump everything */
;; dumpsol([[x=x11,y=y1], [x=x12,y=y1], [x=x21,y=y2], [x=x22,y=y2]]);

(defun ellipse-normal-radii* (ell)
  (multiple-value-bind (a b c) (ellipse-coefficients ell)
    (cond ((coordinate= 0 c)
           ;; this is the unit circle
           (values  0 (sqrt (/ 1 b))
                    (sqrt (/ 1 a)) 0))
          (t
	   (let* ((x1 (- (/ c
			    (sqrt (+ (- (* (* c c)
					   (sqrt (+ (* c c)
						    (* b b)
						    (- (* 2 a b)) (* a a)))))
				     (- (* 2 (* b b)
					   (sqrt (+ (* c c) (* b b)
						    (- (* 2 a b)) (* a a)))))
				     (* 2 a b (sqrt (+ (* c c) (* b b)
						       (- (* 2 a b))
						       (* a a))))
				     (* 2 b (* c c))
				     (* 2 (expt b 3))
				     (- (* 4 a (* b b))) (* 2 (* a a) b))))))
		  (y1 (- (/ (+ (* (sqrt (+ (* c c)
					   (* b b)
					   (- (* 2 a b))
					   (* a a)))
				  x1)
			       (- (* b x1)) (* a x1)) 
			    c)))
		  (x2 (- (/ c
			    (sqrt (+ (* (* c c)
					(sqrt (+ (* c c)
						 (* b b)
						 (- (* 2 a b))
						 (* a a))))
				     (* 2 (* b b) (sqrt (+ (* c c)
							   (* b b)
							   (- (* 2 a b))
							   (* a a))))
				     (- (* 2 a b (sqrt (+ (* c c)
							  (* b b)
							  (- (* 2 a b))
							  (* a a)))))
				     (* 2 b (* c c))
				     (* 2 (expt b 3))
				     (- (* 4 a (* b b))) (* 2 (* a a) b))))))
		  (y2 (- (/ (+ (- (* (sqrt (+ (* c c)
					      (* b b)
					      (- (* 2 a b))
					      (* a a)))
				     x2))
			       (- (* b x2)) (* a x2))
			    c))))
	     (values x1 y1 x2 y2))))))

;;; ---- Intersection of Ellipse vs. Ellipse ---------------------------------------------

;; Das ganze ist so unverstaendlich, ich muss noch mal nach meinen Notizen
;; fanden, um die Herleitung der Loesung fuer das Schnittproblem praesentieren
;; zu koennen.

(defun intersection-ellipse/ellipse (e1 e2)
  ;; Eine der beiden Ellipsen fuehren wir zuerst auf den Einheitskreis zurueck.
  (let ((a (invert-transformation (slot-value e1 'tr))))
    (let ((r (intersection-ellipse/unit-circle (transform-region a e2))))
      (if (atom r)
          r
        (mapcar (lambda (p)
                  (multiple-value-bind (x y) (transform-position (slot-value e1 'tr) (car p) (cdr p))
                    (make-point x y)))
                r)))))

(defun intersection-ellipse/unit-circle (ell)
  (multiple-value-bind (a b c d e f) (ellipse-coefficients ell)
    (let ((pn (elli-polynom ell)))
      (cond ((= (length pn) 0)
             :coincident)
            (t
             (let ((ys (newton-iteration pn 0d0))
                   (res nil))
               (dolist (y ys)
                 (let ((x (sqrt (- 1 (* y y)))))
                   (when (realp x)
                     (when (coordinate= 0 (ellipse-equation a b c d e f x y))
                       (pushnew (cons x y) res :test #'equal))
                     (when (coordinate= 0 (ellipse-equation a b c d e f (- x) y))
                       (pushnew (cons (- x) y) res :test #'equal)) )))
               res)) ))))

(defun ellipse-equation (a b c d e f x y)
  (+ (* a x x) (* b y y) (* c x y) (* d x) (* e y) f))

(defun elli-polynom (ell)
  ;; Was ganz lustig ist, ist dass wir bei Kreisen immer ein Polynom
  ;; vom Grade zwei bekommen.
  (multiple-value-bind (a b c d e f) (ellipse-coefficients ell)
    (canonize-polynom
     (vector (+ (* (- b a) (- b a)) (* c c))
             (+ (* 2 b e) (* -2 a e) (* 2 c d))
             (+ (* e e) (* 2 (- b a) (+ a f)) (* -1 c c) (* d d))
             (+ (* 2 e a) (* 2 e f) (* -2 c d))
             (+ (* (+ a f) (+ a f)) (* -1 d d)) ))) )

;; Wir basteln uns mal eine einfache Newtoniteration. Manchmal
;; scheitern wir noch hoffungslos an lokalen Minima. Ansonsten ist das
;; Konvergenzverhalten fuer unsere Aufgabe schon ganz gut. Aber wir
;; handeln uns durch das Abdividieren der Nullstellen z.T. noch
;; beachtliche Fehler ein; ich versuche das zu mildern in dem ich nach
;; Finden einer Nullstell noch eine paar Newtonschritte mit dem
;; Original-Polynom mache (newton-ziel-gerade).

;; Ich sollte man nicht so faul sein und die reichhaltige Literatur zu
;; Rate ziehen tun; es muss auch etwas bessers als Newtoniteration
;; geben. Ich habe da noch so vage Erinnerungen an die
;; Numerik-Vorlesung ...

(defun newton-ziel-gerade (pn x &optional (n 4))
  (cond ((= n 0) x)
        ((multiple-value-bind (f p2) (horner-schema pn x)
           (multiple-value-bind (f*) (horner-schema p2 x)
             (newton-ziel-gerade pn (- x (/ f f*)) (- n 1)))))))

(defun solve-p1 (b c)
  (if (= b 0)
      nil
    (list (- (/ c b)))))

(defun solve-p2 (a b c)
  (cond ((= a 0)
         (solve-p1 b c))
        (t
         (let* ((p (/ b a))
                (q (/ c a))
                (d (- (/ (* p p) 4) q)))
           (cond ((< d 0)
                  nil)
                 ((= d 0)
                  (list (/ p 2)))
                 (t
                  (list (+ (/ p 2) (sqrt d))
                        (- (/ p 2) (sqrt d))))))) ))

(defun maybe-solve-polynom-trivially (pn)
  (case (length pn)
    (0 (values nil t))
    (1 (values nil t))
    (2 (values (solve-p1 (aref pn 0) (aref pn 1)) t))
    (3 (values (solve-p2 (aref pn 0) (aref pn 1) (aref pn 2)) t))
    (t (values nil nil))))

(defun canonize-polynom (pn)
  (cond ((= (length pn) 0) pn)
        ((coordinate= (aref pn 0) 0)
         (canonize-polynom (subseq pn 1)))
        (t pn)))

(defun newton-iteration (polynom x-start)
  ;; ACHTUNG: Speziell auf unser problem angepasst, nicht ohne lesen uebernehmen!
  (multiple-value-bind (sol done?) (maybe-solve-polynom-trivially polynom)
    (cond (done?
           sol)
          (t
           (let ((x x-start)
                 x1
                 (n 0)
                 (pn polynom)
                 (eps-f 0d0)
                 (eps-f* 0d-16)
                 (eps-x 1d-20)
                 (m 20)                 ;maximal zahl schritte
                 (res nil) )
             (loop
               (cond ((> n m)
                      (return)))
               (multiple-value-bind (f p2) (horner-schema pn x)
                 (multiple-value-bind (f*) (horner-schema p2 x)
                   (cond ((<= (abs f*) eps-f*)
                          ;; Wir haengen an einer Extremstelle fest -- mit zufaelligem Startwert weiter.
                          (setf x1 (+ 1d0 (random 2d0))))
                         (t
                          (setf x1 (- x (/ f f*)))
                          (cond ((or (<= (abs f) eps-f) 
                                     (<= (abs (- x1 x)) eps-x))
                                 ;; noch ein paar newton schritte, um das ergebnis zu verbessern
                                 (setf x1 (newton-ziel-gerade polynom x1))
                                 (push x1 res)
                                 ;; abdividieren
                                 (multiple-value-bind (f p2) (horner-schema pn x1)
                                   f
                                   (setq pn (canonize-polynom p2))
                                   (multiple-value-bind (sol done?) (maybe-solve-polynom-trivially pn)
                                     (when done?
                                       ;; Hier trotzdem noch nachiterieren -- ist das eine gute Idee?
                                       (setf sol (mapcar (lambda (x) (newton-ziel-gerade polynom x)) sol))
                                       (setf res (nconc sol res))
                                       (return))))
                                 (setf x1 x-start)
                                 (setq n 0)) ))))
                 (setf x (min 1d0 (max -1d0 x1)))        ;Darf man das machen?
                 (incf n)))
             res)) )))

(defun horner-schema (polynom x)
  ;; Wertet das polynom `polynom' mit Hilfe des Hornerschemas an der
  ;; Stelle `x' aus; Gibt zwei Werte zurueck:
  ;;  - den Funktionswert
  ;;  - die letzte Zeile des Hornerschemas (Divisionsergebnis)
  (let ((n (length polynom)))
    (cond ((= n 0) (values 0))
          ((= n 1) (values (aref polynom 0) '#()))
          (t
           (let ((b (make-array (1- n))))
             (setf (aref b 0) (aref polynom 0))
             (do ((i 1 (+ i 1)))
                 ((= i (- n 1)) 
                  (values 
                   (+ (* (aref b (- i 1)) x) (aref polynom i))
                   b))
               (setf (aref b i) (+ (* (aref b (- i 1)) x) (aref polynom i))))))) ))



;;;; ====================================================================================================

(defmethod region-union ((a point) (b point))
  (cond ((region-equal a b)
         a)
        (t
         (make-instance 'standard-region-union :regions (list a b)))))

(defmethod region-intersection ((a point) (b point))
  (cond
    ((region-equal a b) a)
    (t +nowhere+)))

(defmethod region-equal ((a point) (b point))
  (and (coordinate= (point-x a) (point-x b))
       (coordinate= (point-y a) (point-y b))))


;;; ====================================================================================================

;;; ---- Rectangle Sets ---------------------------------------------------------------------------------

(defclass standard-rectangle-set (region-set bounding-rectangle)
  ((bands
    ;; Represents the set of rectangles. This is list like:
    ;;
    ;;  ((<y_1> . <x_band_1>)        
    ;;   (<y_2> . <x_band_2>) 
    ;;   :
    ;;   (<y_n>))
    ;;
    ;; <x_band_i> := (x_i_1 u_i_1  x_i_2 u_i_2 ... x_i_m u_i_m)
    ;;
    ;; Now a point (x,y) is member of the rectangle set, if there is an
    ;; i, such that y member of [y_i, y_(i+1)] and x member of x_band_i.
    ;;
    ;; An x is member of an band i, if there is an j, such that x
    ;; member [x_i_j, u_i_j].
    ;;
    ;; That is <x_band_i> describes the possible x-coordinates in the
    ;; y-range [y_i, y_(i+1)].
    ;;
    :initarg :bands
    :reader  standard-rectangle-set-bands)
   ;;
   (bounding-rectangle 
    ;; Caches the regions bounding-rectangle. Is either NIL or the
    ;; bounding-rectangle, represented by a list (x1 y1 x2 y2).
    :initform nil)))

(defmethod map-over-region-set-regions (fun (self standard-rectangle-set) &key normalize)
  (with-slots (bands) self
    (cond ((or (null normalize) (eql normalize :x-banding))
           (map-over-bands-rectangles (lambda (x1 y1 x2 y2)
                                        (funcall fun (make-rectangle* x1 y1 x2 y2)))
                                      bands))
          ((eql normalize :y-banding)
           (map-over-bands-rectangles (lambda (y1 x1 y2 x2)
                                        (funcall fun (make-rectangle* x1 y1 x2 y2)))
                                      (xy-bands->yx-bands bands)))
          (t
           (error "Bad ~S argument to ~S: ~S"
                  :normalize 'map-over-region-set-regions normalize)) )))

(defmethod region-set-regions ((self standard-rectangle-set) &key normalize)
  (let ((res nil))
    (map-over-region-set-regions (lambda (r) (push r res)) self :normalize normalize)
    res))

(defun make-standard-rectangle-set (bands)
  (cond ((null bands) +nowhere+)
        ((and (= (length bands) 2)
              (null (cdr (second bands)))
              (= (length (cdr (first bands))) 2))
         (make-rectangle* (first (cdar bands)) (caar bands)
                          (second (cdar bands)) (caadr bands)))
        ((= (length (first bands)) 1)
         (make-standard-rectangle-set (rest bands)))
        (t
         (make-instance 'standard-rectangle-set :bands bands)) ))
    
;;; rectangle-set vs. rectangle-set

(defmethod region-union ((xs standard-rectangle-set) (ys standard-rectangle-set))
  (make-standard-rectangle-set (bands-union (standard-rectangle-set-bands xs)
                                            (standard-rectangle-set-bands ys))))

(defmethod region-intersection ((xs standard-rectangle-set) (ys standard-rectangle-set))
  (make-standard-rectangle-set (bands-intersection (standard-rectangle-set-bands xs)
                                                   (standard-rectangle-set-bands ys))))

(defmethod region-difference ((xs standard-rectangle-set) (ys standard-rectangle-set))
  (make-standard-rectangle-set (bands-difference (standard-rectangle-set-bands xs)
                                                 (standard-rectangle-set-bands ys))))
         
;;; rectangle-set vs. rectangle and vice versa

(defmethod region-union ((xs standard-rectangle-set) (ys standard-rectangle))
  (region-union xs (rectangle->standard-rectangle-set ys)))

(defmethod region-union ((xs standard-rectangle) (ys standard-rectangle-set))
  (region-union (rectangle->standard-rectangle-set xs) ys))

(defmethod region-difference ((xs standard-rectangle-set) (ys standard-rectangle))
  (region-difference xs (rectangle->standard-rectangle-set ys)))

(defmethod region-difference ((xs standard-rectangle) (ys standard-rectangle-set))
  (region-difference (rectangle->standard-rectangle-set xs) ys))

(defmethod region-intersection ((xs standard-rectangle-set) (ys standard-rectangle))
  (region-intersection xs (rectangle->standard-rectangle-set ys)))

(defmethod region-intersection ((xs standard-rectangle) (ys standard-rectangle-set))
  (region-intersection (rectangle->standard-rectangle-set xs) ys))

;;; rectangle vs rectangle

(defmethod region-union ((xs standard-rectangle) (ys standard-rectangle))
  (region-union (rectangle->standard-rectangle-set xs) (rectangle->standard-rectangle-set ys)))

(defmethod region-difference ((xs standard-rectangle) (ys standard-rectangle))
  (region-difference (rectangle->standard-rectangle-set xs) (rectangle->standard-rectangle-set ys)))

(defmethod region-intersection ((xs standard-rectangle) (ys standard-rectangle))
  (region-intersection (rectangle->standard-rectangle-set xs) (rectangle->standard-rectangle-set ys)))

(defmethod region-intersection ((xr rectangle) (yr rectangle))
  (region-intersection (rectangle->standard-rectangle-set xr)
		       (rectangle->standard-rectangle-set yr)))

;;;

(defmethod region-equal ((xs standard-rectangle-set) (ys standard-rectangle-set))
  ;; Our bands representation is canonic
  (equal (standard-rectangle-set-bands xs)
         (standard-rectangle-set-bands ys)))

(defmethod region-contains-position-p ((self standard-rectangle-set) x y)
  (block nil
    (map-over-bands (lambda (y1 y2 isum)
                      (when (<= y1 y y2)
                        (when (isum-member x isum)
                          (return t)))
                      (when (> y y2)
                        (return nil)))
                    (standard-rectangle-set-bands self))
    nil))

(defmethod region-contains-region-p ((xs standard-rectangle-set) (point point))
  (multiple-value-bind (x y) (point-position point)
    (region-contains-position-p xs x y)))

;;; ---- interval sums ----------------------------------------------------------------------------------

(defun isum-union* (xs ys)        (isum-op xs ys boole-ior   0 0 nil))
(defun isum-difference* (xs ys)   (isum-op xs ys boole-andc2 0 0 nil))
(defun isum-intersection* (xs ys) (isum-op xs ys boole-and   0 0 nil))

;; You could optimize all this like hell, but I better let the code
;; alone.
;; BTW this is the first time I make use of boole-xyz

(defun isum-op (as bs boole-op in-a in-b x0)
  (let (x)
    (cond ((and (null as) (null bs))
           nil)
          (t
           (cond ((null bs)
                  (setq in-a (- 1 in-a))
                  (setq x (pop as)))

                 ((null as)
                  (setq in-b (- 1 in-b))
                  (setq x (pop bs)))

                 ((< (first as) (first bs))
                  (setq in-a (- 1 in-a))
                  (setq x (pop as)))
                 
                 ((< (first bs) (first as))
                  (setq in-b (- 1 in-b))
                  (setq x (pop bs)))

                 (t
                  (setq in-a (- 1 in-a)
                        in-b (- 1 in-b))
                  (setq x (pop as))
                  (pop bs)))
           
           (cond ((zerop (boole boole-op in-a in-b))
                  (if x0 
                      (list* x0 x (isum-op as bs boole-op in-a in-b nil))
                    (isum-op as bs boole-op in-a in-b x0)))
                 (t
                  (if (null x0)
                      (isum-op as bs boole-op in-a in-b x)
                    (isum-op as bs boole-op in-a in-b x0))))))))

;;; ---- Bands ------------------------------------------------------------------------------------------


;; A band list is represented by

;;  ((x_0 . a_0) (x_1 . a_1) ... (x_n . nil))

;; The a_i are the relevant interval sums for x in [x_i, x_(i+1)].

;; The empty band could have been representated as
;;  ((x . nil))  x arbitrary
;; But to get a cononic representation, I'll choose simply NIL.

;; A better representation would be
;;  (x_0 a_0 x_1 a_1 ... x_n)
;; Pro: Unlimited bands could be represented by simply skipping the
;; first or last 'x'. So similar representation could apply to
;; interval sums also. But I let the representation as it is, since
;; this version is well tested.

(defun bands-op (as bs isum-op z0 a b)
  (let (z1)
    (cond ((and (null as) (null bs))
           (if z0
               (list (cons z0 nil))
             nil))
          (t
           (setq z1 (cond ((null as) (caar bs))
                          ((null bs) (caar as))
                          (t (min (caar as) (caar bs)))))
           (let ((rest (bands-op (if (and as (= z1 (caar as))) (cdr as) as)
                                 (if (and bs (= z1 (caar bs))) (cdr bs) bs)
                                 isum-op
                                 z1
                                 (if (and as (= z1 (caar as))) (cdar as) a)
                                 (if (and bs (= z1 (caar bs))) (cdar bs) b)))
                 (isum (funcall isum-op a b)))
             (if z0  
                 (if (and rest (equal isum (cdar rest)))
                     (cons (cons z0 isum)
                           (cdr rest))
                   (cons (cons z0 isum)
                         rest))
               rest))) )))

(defun canon-empty-bands (x)
  (cond ((null (cdr x)) nil)
        (t x)))

(defun bands-union (as bs)
  (canon-empty-bands (bands-op as bs #'isum-union* nil nil nil)))

(defun bands-intersection (as bs)
  (canon-empty-bands (bands-op as bs #'isum-intersection* nil nil nil)))

(defun bands-difference (as bs)
  (canon-empty-bands (bands-op as bs #'isum-difference* nil nil nil)))


(defun rectangle->xy-bands* (x1 y1 x2 y2)
  (list (list y1 x1 x2)
        (cons y2 nil)))

(defun rectangle->yx-bands* (x1 y1 x2 y2)
  (list (list x1 y1 y2)
        (cons x2 nil)))

(defun xy-bands->yx-bands (bands)
  ;; Das kann man sicherlich noch viel geschicker machen ...
  (let ((res nil))
    (map-over-bands-rectangles (lambda (x1 y1 x2 y2)
                                 (setf res (bands-union res (rectangle->yx-bands* x1 y1 x2 y2))))
                               bands)
    res))

(defun map-over-bands-rectangles (fun bands)
  (map-over-bands (lambda (y1 y2 isum)
                    (do ((p isum (cddr p)))
                        ((null p))
                      (funcall fun (car p) y1 (cadr p) y2)))
                  bands))

(defun map-over-bands (fun bands)
  (do ((q bands (cdr q)))
      ((null (cdr q)))
    (funcall fun (caar q) (caadr q) (cdar q))))

(defun isum-member (elt isum)
  (cond ((null isum) nil)
        ((< elt (car isum)) nil)
        ((<= elt (cadr isum)) t)
        (t (isum-member elt (cddr isum)))))

(defun rectangle->standard-rectangle-set (rect)
  (multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* rect)
    (make-instance 'standard-rectangle-set :bands (rectangle->xy-bands* x1 y1 x2 y2))))

(defmethod transform-region (tr (self standard-rectangle-set))
  (cond ((scaling-transformation-p tr)
         (multiple-value-bind (mxx mxy myx myy tx ty)
             (get-transformation tr)
           (declare (ignore mxy myx))
           (let ((rev-x-p (< mxx 0))
                 (rev-y-p (< myy 0)))
             (flet ((correct (bands)
                      (loop for ((y . nil) (nil . xs)) on (nreverse bands)
                         collect `(,y . ,xs))))
               (make-standard-rectangle-set
                (loop for band in (standard-rectangle-set-bands self)
                   for new-band = (loop for x in (cdr band)
                                     collect (+ (* mxx x) tx) into new-xs
                                     finally (return (cons (+ (* myy (car band)) ty)
                                                           (if rev-x-p
                                                               (nreverse new-xs)
                                                               new-xs))))
                   collect new-band into new-bands
                   finally (return (if rev-y-p
                                       (correct new-bands)
                                       new-bands))))))))
        (t
         ;; We have insufficient knowledge about the transformation,
         ;; so we have to take the union of all transformed rectangles.
         ;; Maybe there is a faster way to do this.
         (let ((res +nowhere+))
           (map-over-region-set-regions
	    (lambda (rect)
	      (setf res (region-union res (transform-region tr rect))))
	    self)
           res)) ))

;;; ====================================================================================================

(defclass standard-bounding-rectangle (standard-rectangle) ())

(defmethod region-equal ((a everywhere-region) (b everywhere-region))
  t)

(defmethod region-equal ((a nowhere-region) (b nowhere-region))
  t)

(defmethod region-equal ((a everywhere-region) (b region))
  nil)

(defmethod region-equal ((a nowhere-region) (b region))
  nil)

(defmethod region-equal ((a region) (b everywhere-region))
  nil)

(defmethod region-equal ((a region) (b nowhere-region))
  nil)

(defmethod region-equal ((a standard-rectangle) (b standard-rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* a)
    (multiple-value-bind (u1 v1 u2 v2) (rectangle-edges* b)
      (and (coordinate= x1 u1)
           (coordinate= y1 v1)
           (coordinate= x2 u2)
           (coordinate= y2 v2)))))

(defmethod region-equal ((a standard-rectangle) (b path)) nil)
(defmethod region-equal ((a path) (b standard-rectangle)) nil)

(defmethod transform-region (tr (self everywhere-region)) (declare (ignore tr)) +everywhere+)
(defmethod transform-region (tr (self nowhere-region)) (declare (ignore tr)) +nowhere+)

(defmethod region-contains-position-p ((self everywhere-region) x y)
  (declare (ignore x y))
  t)

(defmethod region-contains-position-p ((self nowhere-region) x y)
  (declare (ignore x y))
  nil)

(defmethod region-contains-position-p ((self standard-region-union) x y)
  (some (lambda (r) (region-contains-position-p r x y))
        (standard-region-set-regions self)))

(defmethod region-contains-position-p ((self standard-region-intersection) x y)
  (every (lambda (r) (region-contains-position-p r x y))
         (standard-region-set-regions self)))

(defmethod region-contains-position-p ((self standard-region-difference) x y)
  (and (region-contains-position-p (standard-region-difference-a self) x y)
       (not (region-contains-position-p (standard-region-difference-b self) x y))))

;; Trivial set operations

(defmethod region-union ((a everywhere-region) (b region)) +everywhere+)
(defmethod region-union ((a region) (b everywhere-region)) +everywhere+)
(defmethod region-union ((a nowhere-region) (b region)) b)
(defmethod region-union ((a region) (b nowhere-region)) a)

(defmethod region-intersection ((a everywhere-region) (b region)) b)
(defmethod region-intersection ((a region) (b everywhere-region)) a)
(defmethod region-intersection ((a nowhere-region) (b region)) +nowhere+)
(defmethod region-intersection ((a region) (b nowhere-region)) +nowhere+)

;;;(defmethod region-difference ((a everywhere-region) (b region)) b)
(defmethod region-difference ((a region) (b everywhere-region)) +nowhere+)   ;mit ohne alles
(defmethod region-difference ((a nowhere-region) (b region)) +nowhere+)
(defmethod region-difference ((a region) (b nowhere-region)) a)


;; dimensionally rule
(defmethod region-union ((a area) (b path)) a)
(defmethod region-union ((a path) (b point)) a)
(defmethod region-union ((a area) (b point)) a)
(defmethod region-union ((a path) (b area)) b)
(defmethod region-union ((a point) (b path)) b)
(defmethod region-union ((a point) (b area)) b)

(defmethod transform-region (tr (self standard-region-difference))
  (with-slots (a b) self
    (make-instance 'standard-region-difference
      :a (transform-region tr a)
      :b (transform-region tr b))))

(defmethod transform-region (tr (self standard-region-union))
  (with-slots (regions) self
    (make-instance 'standard-region-union :regions (mapcar (lambda (r) (transform-region tr r)) regions))))

(defmethod transform-region (tr (self standard-region-intersection))
  (with-slots (regions) self
    (make-instance 'standard-region-intersection :regions (mapcar (lambda (r) (transform-region tr r)) regions))))


(defmethod region-set-regions ((self standard-region-union) &key normalize)
  (declare (ignorable normalize))
  (standard-region-set-regions self))

(defmethod region-set-regions ((self standard-region-intersection) &key normalize)
  (declare (ignorable normalize))
  (standard-region-set-regions self))

(defmethod region-set-regions ((self standard-region-difference) &key normalize)
  (declare (ignorable normalize))
  (list (standard-region-difference-a self)
        (standard-region-difference-b self)))

(defmethod region-set-regions ((self region) &key normalize)
  (declare (ignorable normalize))
  (list self))

(defmethod map-over-region-set-regions (fun (self standard-region-union) &key normalize)
  (declare (ignorable normalize))
  (mapc fun (standard-region-set-regions self)))

(defmethod map-over-region-set-regions (fun (self standard-region-intersection) &key normalize)
  (declare (ignorable normalize))
  (mapc fun (standard-region-set-regions self)))

(defmethod map-over-region-set-regions (fun (self standard-region-difference) &key normalize)
  (declare (ignorable normalize))
  (funcall fun (standard-region-difference-a self))
  (funcall fun (standard-region-difference-b self)))

(defmethod map-over-region-set-regions (fun (self region) &key normalize)
  (declare (ignorable normalize))
  (funcall fun self))

(defun line-intersection* (x1 y1 x2 y2 u1 v1 u2 v2)
  (let ((dx (- x2 x1)) (dy (- y2 y1))
        (du (- u2 u1)) (dv (- v2 v1)))
    (let ((q (- (* dx dv) (* du dy))))
      (cond ((not (and (<= (min x1 x2) (max u1 u2)) (<= (min u1 u2) (max x1 x2))
                       (<= (min y1 y2) (max v1 v2)) (<= (min v1 v2) (max y1 y2))))
             nil)
            ((coordinate= 0 q)
             (cond ((coordinate= (* (- v1 y1) dx) (* (- u1 x1) dy))
                    ;; koninzident
                    (cond ((> (abs dx) (abs dy))
                           (let* ((sx1 (max (min x1 x2) (min u1 u2)))
                                  (sx2 (min (max x1 x2) (max u1 u2)))
                                  (sy1 (+ (* (- sx1 x1) (/ dy dx)) x1))
                                  (sy2 (+ (* (- sx2 x1) (/ dy dx)) x1)))
                             (values :coincident sx1 sy1 sx2 sy2)))
                          (t
                           (let* ((sy1 (max (min y1 y2) (min v1 v2)))
                                  (sy2 (min (max y1 y2) (max v1 v2)))
                                  (sx1 (+ (* (- sy1 y1) (/ dx dy)) y1))
                                  (sx2 (+ (* (- sy2 y1) (/ dx dy)) y1)))
                             (values :coincident sx1 sy1 sx2 sy2)))))
                   (t
                    ;;paralell -- kein Schnitt
                    nil)))
            (t
             (let ((x (/ (+ (* dx (- (* u1 dv) (* v1 du))) (* du (- (* y1 dx) (* x1 dy)))) q))
                   (y (/ (+ (* dy (- (* u1 dv) (* v1 du))) (* dv (- (* y1 dx) (* x1 dy)))) q)))
               (if (and (or (<= x1 x x2) (<= x2 x x1))
                        (or (<= u1 x u2) (<= u2 x u1))
                        (or (<= y1 y y2) (<= y2 y y1))
                        (or (<= v1 y v2) (<= v2 y v1)))
                   (values :hit x y)
                 nil)) ) )) ))

(defmethod region-intersection ((a standard-line) (b standard-line))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (multiple-value-bind (u1 v1) (line-start-point* b)
        (multiple-value-bind (u2 v2) (line-end-point* b)
          (multiple-value-bind (r sx1 sy1 sx2 sy2) (line-intersection* x1 y1 x2 y2 u1 v1 u2 v2)
            (case r
              (:hit (make-point sx1 sy1))
              (:coincident (make-line* sx1 sy1 sx2 sy2))
              ((nil) +nowhere+))))))))

;; IHMO the CLIM dimensionality rule is brain dead!

(defmethod region-intersection ((a standard-polyline) (b region))
  (let ((res +nowhere+))
    ;; hack alert
    (map-over-polygon-segments 
     (lambda (x1 y1 x2 y2)
       (setf res (region-union res (region-intersection (make-line* x1 y1 x2 y2) b))))
     a)
    res))

(defmethod region-difference ((a standard-polyline) (b region))
  (let ((res +nowhere+))
    (map-over-polygon-segments 
     (lambda (x1 y1 x2 y2)
       (setf res (region-union res (region-difference (make-line* x1 y1 x2 y2) b))))
     a)
    res))

(defmethod region-difference ((a region) (b standard-polyline))
  (map-over-polygon-segments 
     (lambda (x1 y1 x2 y2)
       (setf a (region-difference a (make-line* x1 y1 x2 y2))))
     b)
  a)

(defmethod region-intersection ((b region) (a standard-polyline))
  (region-intersection a b))

(defmethod region-intersection ((a region) (p point))
  (multiple-value-bind (x y) (point-position p)
    (if (region-contains-position-p a x y)
        p
      +nowhere+)))

(defmethod region-intersection ((p point) (a region))
  (region-intersection a p))

(defmethod region-intersection ((a standard-region-union) (b region))
  (let ((res +nowhere+))
    (map-over-region-set-regions (lambda (r) (setf res (region-union res (region-intersection r b)))) a)
    res))

(defmethod region-intersection ((a region) (b standard-region-union))
  (region-intersection b a))

(defmethod region-intersection ((a standard-rectangle-set) (b region))
  (let ((res +nowhere+))
    (map-over-region-set-regions (lambda (r) (setf res (region-union res (region-intersection r b)))) a)
    res))

(defmethod region-intersection ((a region) (b standard-rectangle-set))
  (region-intersection b a))

(defmethod region-intersection ((a region) (b standard-region-intersection))
  (map-over-region-set-regions (lambda (r) (setf a (region-intersection a r))) b)
  a)

(defmethod region-intersection ((a standard-region-intersection) (b region))
  (region-intersection b a))

(defmethod region-intersection ((a region) (b region))
  (make-instance 'standard-region-intersection :regions (list a b)))


(defmethod region-intersection ((x region) (y standard-region-difference))
  (with-slots (a b) y
    (region-difference (region-intersection x a) b)))

(defmethod region-intersection ((x standard-region-difference) (y region))
  (with-slots (a b) x
    (region-difference (region-intersection y a) b)))



(defmethod region-difference ((x area) (y path)) x)
(defmethod region-difference ((x area) (y point)) x)
(defmethod region-difference ((x path) (y point)) x)

(defmethod region-difference ((x everywhere-region) (y region))
  (make-instance 'standard-region-difference :a x :b y))

(defmethod region-difference ((x everywhere-region) (y nowhere-region))
  x)

(defmethod region-difference ((x everywhere-region) (y everywhere-region))
  +nowhere+)

(defmethod region-difference ((x region) (y standard-region-difference))
  (with-slots (a b) y
    (region-union (region-difference x a) (region-intersection x b))))

(defmethod region-difference ((x region) (y standard-region-union))
  ;; A \ (B1 u B2 .. u Bn) = ((((A \ B1) \ B2) ... ) \ Bn)
  (let ((res x))
    (map-over-region-set-regions (lambda (a)
                                   (setf res (region-difference res a)))
                                 y)
    res))

(defmethod region-difference ((x standard-region-union) (y region))
  ;; (A u B) \ C = A\C u B\C
  (let ((res +nowhere+))
    (map-over-region-set-regions (lambda (a)
                                   (setf res (region-union res (region-difference a y))))
                                 x)
    res))

(defmethod region-difference ((x region) (y standard-rectangle-set))
  (let ((res x))
    (map-over-region-set-regions (lambda (a)
                                   (setf res (region-difference res a)))
                                 y)
    res))

(defmethod region-difference ((x standard-rectangle-set) (y region))
  (let ((res +nowhere+))
    (map-over-region-set-regions (lambda (a)
                                   (setf res (region-union res (region-difference a y))))
                                 x)
    res))

(defmethod region-difference ((x point) (y region))
  (multiple-value-bind (px py) (point-position x)
    (if (region-contains-position-p y px py)
        +nowhere+
      x)))

(defmethod region-difference ((x standard-region-difference) (y region))
  ;; (A\B)\C = A \ (B u C)
  (with-slots (a b) x
    (region-difference a (region-union b y))))

(defmethod region-difference ((x region) (y standard-region-intersection))
  (let ((res +nowhere+))
    (map-over-region-set-regions (lambda (b)
                                   (setf res (region-union res (region-difference x b))))
                                 y)
    res))

;; Diese CLIM dimensionality rule ist in hoechsten ma?e inkonsistent
;; und bringt mehr probleme als sie beseitigt.



;;; ---- Set operations on polygons ---------------------------------------------------------------------

(defstruct (pg-edge (:constructor make-pg-edge* (x1 y1 x2 y2 extra)))
  x1 y1 x2 y2 extra)

(defstruct pg-splitter
  links                                 ;liste von punkten
  rechts)                               ; von unten nach oben

(defun make-pg-edge (p1 p2 extra)
  (multiple-value-bind (x1 y1) (point-position p1)
    (multiple-value-bind (x2 y2) (point-position p2)
      (make-pg-edge* x1 y1 x2 y2 extra))))


(defmethod region-intersection ((a standard-polygon) (b standard-polygon))
  (polygon-op a b #'logand))

(defmethod region-union ((a standard-polygon) (b standard-polygon))
  (polygon-op a b #'logior))

(defmethod region-difference ((a standard-polygon) (b standard-polygon))
  (polygon-op a b #'logandc2))

(defmethod region-intersection ((a standard-polygon) (b standard-rectangle))
  (polygon-op a b #'logand))

(defmethod region-union ((a standard-polygon) (b standard-rectangle))
  (polygon-op a b #'logior))

(defmethod region-difference ((a standard-polygon) (b standard-rectangle))
  (polygon-op a b #'logandc2))

(defmethod region-intersection ((a standard-rectangle) (b standard-polygon))
  (polygon-op a b #'logand))

(defmethod region-union ((a standard-rectangle) (b standard-polygon))
  (polygon-op a b #'logior))

(defmethod region-difference ((a standard-rectangle) (b standard-polygon))
  (polygon-op a b #'logandc2))

(defun polygon-op (pg1 pg2 &optional logop)
  (let ((sps nil))
    (over-sweep-bands pg1 pg2
                      (lambda (sy0 sy1 S &aux (ys nil))
                        (setq ys (list sy0 sy1))
                        (dolist (k1 S)
                          (dolist (k2 S)
                            (multiple-value-bind (px py)
                                (line-intersection** (pg-edge-x1 k1) (pg-edge-y1 k1)
                                                     (pg-edge-x2 k1) (pg-edge-y2 k1)
                                                     (pg-edge-x1 k2) (pg-edge-y1 k2)
                                                     (pg-edge-x2 k2) (pg-edge-y2 k2))
                              (when (and px (< sy0 py sy1))
                                (pushnew py ys :test #'coordinate=)))))
                        (setq ys (sort ys #'<))
                        (do ((q ys (cdr q)))
                            ((null (cdr q)))
                          (let ((by0 (car q)) (by1 (cadr q))
                                (R nil))
                            (dolist (k S)
                              (when (> (pg-edge-y2 k) (pg-edge-y1 k))
                                (multiple-value-bind (x1 y1 x2 y2) 
                                    (restrict-line-on-y-interval* (pg-edge-x1 k) (pg-edge-y1 k)
                                                                  (pg-edge-x2 k) (pg-edge-y2 k)
                                                                  by0 by1)
                                  (declare (ignore y1 y2))
                                  (push (list x1 x2 (pg-edge-extra k)) R))))
                            (setq R (sort R #'< :key (lambda (x) (+ (first x) (second x)))))
                            (labels
                                ((add (lo lu ro ru)
                                   (dolist (s sps
                                             ;; ansonsten
                                             (push (make-pg-splitter :links  (list lu lo) 
                                                                     :rechts (list ru ro))
                                                   sps) )
                                     (when (and (region-equal lo (car (pg-splitter-links s)))
                                                (region-equal ro (car (pg-splitter-rechts s))))
                                       (push lu (pg-splitter-links s))
                                       (push ru (pg-splitter-rechts s))
                                       (return))) ))
                              (let ((eintritt nil)
                                    (ina 0)
                                    (inb 0))
                                (dolist (k R)
                                  (ecase (third k)
                                    (:a (setq ina (- 1 ina)))
                                    (:b (setq inb (- 1 inb))))
                                  (cond ((/= 0 (funcall logop ina inb))
                                         (when (null eintritt)
                                           (setq eintritt k)))
                                        (t
                                         (when eintritt
                                           (add (make-point (first eintritt) by0)
                                                (make-point (second eintritt) by1)
                                                (make-point (first k) by0)
                                                (make-point (second k) by1))
                                           (setq eintritt nil)) )))) ) )) ) )
    (setq sps (delete +nowhere+ (mapcar #'pg-splitter->polygon sps)))
    (cond ((null sps) +nowhere+)
          ((null (cdr sps))
           (car sps))
          ((make-instance 'standard-region-union :regions sps))) ))

(defun over-sweep-bands (pg1 pg2 fun)
  (let ((es (nconc (polygon->pg-edges pg1 :a) (polygon->pg-edges pg2 :b))))
    (setq es (sort es #'< :key #'pg-edge-y1))
    (let ((ep es)
          (sy (pg-edge-y1 (car es)))
          (S nil))
      (do () ((null ep))
        (setq S (delete-if (lambda (e)
                             (<= (pg-edge-y2 e) sy))
                           S))
      
        (do () ((or (null ep) (/= sy (pg-edge-y1 (car ep)))))
          (push (pop ep) S))

        (let ((sy2 (or (and ep (pg-edge-y1 (car ep)))
                       (reduce #'max (mapcar #'pg-edge-y2 S)))))
          
          (funcall fun sy sy2 S)
          (setq sy sy2)) ))))

(defun polygon->pg-edges (pg extra)
  (let ((pts (polygon-points pg))
        (res nil))
    (let ((prev pts)
          (cur (cdr pts))
          (next (cddr pts)))
      (loop
        (when (or (> (point-y (car next)) (point-y (car cur)))
                  (and (= (point-y (car next)) (point-y (car cur)))
                       (> (point-x (car next)) (point-x (car cur)))))
          (push (make-pg-edge (car cur) (car next) extra) res))
        (when (or (> (point-y (car prev)) (point-y (car cur)))
                  (and (= (point-y (car prev)) (point-y (car cur)))
                       (> (point-x (car prev)) (point-x (car cur)))))
          (push (make-pg-edge (car cur) (car prev) extra) res))
        (when (not (or (> (point-y (car next)) (point-y (car cur)))
                       (and (= (point-y (car next)) (point-y (car cur)))
                            (> (point-x (car next)) (point-x (car cur))))
                       (> (point-y (car next)) (point-y (car cur)))
                       (and (= (point-y (car next)) (point-y (car cur)))
                            (> (point-x (car next)) (point-x (car cur))))))
          (push (make-pg-edge (car cur) (car cur) extra) res))
        (psetq prev cur
               cur next
               next (or (cdr next) pts))
        (when (eq prev pts)
          (return)) ))
    res))

(defun restrict-line-on-y-interval* (x1 y1 x2 y2 ry0 ry1)
  (let ((dx (- x2 x1))
        (dy (- y2 y1)))
    (values (+ (* (- ry0 y1) (/ dx dy)) x1) ry0
            (+ (* (- ry1 y1) (/ dx dy)) x1) ry1)))

(defun pg-splitter->polygon (s)
  (make-polygon (clean-up-point-sequence (nconc (pg-splitter-links s) (reverse (pg-splitter-rechts s))))))

(defun clean-up-point-sequence (pts)
  (cond ((null (cdr pts)) pts)
        ((region-equal (car pts) (cadr pts))
         (clean-up-point-sequence (cdr pts)))
        ((null (cddr pts)) pts)
        ((colinear-p (car pts) (cadr pts) (caddr pts))
         (clean-up-point-sequence (list* (car pts) (caddr pts) (cdddr pts))))
        (t
         (cons (car pts) (clean-up-point-sequence (cdr pts)))) ))

(defun colinear-p (p1 p2 p3)
  (multiple-value-bind (x1 y1) (point-position p1)
    (multiple-value-bind (x2 y2) (point-position p2)
      (multiple-value-bind (x3 y3) (point-position p3)
        (coordinate= (* (- x2 x1) (- y3 y2))
                     (* (- x3 x2) (- y2 y1)))))))

(defun line-intersection** (x1 y1 x2 y2 u1 v1 u2 v2)
  (let ((dx (- x2 x1)) (dy (- y2 y1))
        (du (- u2 u1)) (dv (- v2 v1)))
    (let ((q (- (* dx dv) (* du dy))))
      (cond ((coordinate= 0 q)
             nil)
            (t
             (let ((x (/ (+ (* dx (- (* u1 dv) (* v1 du))) (* du (- (* y1 dx) (* x1 dy)))) q))
                   (y (/ (+ (* dy (- (* u1 dv) (* v1 du))) (* dv (- (* y1 dx) (* x1 dy)))) q)))
               (values x y)))))))

;;; -----------------------------------------------------------------------------------------------------

(defmethod region-union ((a standard-region-union) (b nowhere-region))
  a)

(defmethod region-union ((b nowhere-region) (a standard-region-union))
  a)

(defmethod region-union ((a standard-region-union) (b region))
  (assert (not (eq b +nowhere+)))
  (make-instance 'standard-region-union :regions (cons b (standard-region-set-regions a))))

(defmethod region-union ((b region) (a standard-region-union))
  (assert (not (eq b +nowhere+)))
  (make-instance 'standard-region-union :regions (cons b (standard-region-set-regions a))))

(defmethod region-union ((a standard-region-union) (b standard-region-union))
  (assert (not (eq b +nowhere+)))
  (assert (not (eq a +nowhere+)))
  (make-instance 'standard-region-union 
    :regions (append (standard-region-set-regions a) (standard-region-set-regions b))))

(defmethod region-union ((a region) (b region))
  (make-instance 'standard-region-union :regions (list a b)))

(defmethod region-union ((a standard-rectangle-set) (b path)) a)
(defmethod region-union ((b path) (a standard-rectangle-set)) a)
(defmethod region-union ((a standard-rectangle-set) (b point)) a)
(defmethod region-union ((b point) (a standard-rectangle-set)) a)

;;; ---- Intersection Line/Polygon ----------------------------------------------------------------------

(defun geraden-schnitt/prim (x1 y1 x12 y12  x2 y2 x22 y22)
  (let ((dx1 (- x12 x1)) (dy1 (- y12 y1))
        (dx2 (- x22 x2)) (dy2 (- y22 y2)))
    ;; zwei geraden gegeben als
    ;; g : s -> (x1 + s*dx1, y1 + s*dy1)
    ;; h : t -> (x2 + t*dx2, y2 + t*dy2)
    ;; -> NIL | (s ; t)
    (let ((quot (- (* DX2 DY1) (* DX1 DY2))))
      (if (coordinate= quot 0)
          nil
        (values
         (- (/ (+ (* DX2 (- Y1 Y2)) (* DY2 X2) (- (* DY2 X1))) quot))
         (- (/ (+ (* DX1 (- Y1 Y2)) (* DY1 X2) (- (* DY1 X1))) quot)))) )) )

(defun geraden-gleichung (x0 y0 x1 y1 px py)
  ;; ??? This somehow tries to calculate the distance between a point
  ;; and a line. The sign of the result depends upon the side the point
  ;; is on wrt to the line. --GB
  (- (* (- py y0) (- x1 x0))
     (* (- px x0) (- y1 y0))))

(defun position->geraden-fktn-parameter (x0 y0 x1 y1 px py)
  (let ((dx (- x1 x0)) (dy (- y1 y0)))
    (if (> (abs dx) (abs dy))
        (/ (- px x0) dx)
        (/ (- py y0) dy))))

(defun map-over-schnitt-gerade/polygon (fun x1 y1 x2 y2 points)
  ;; This calles 'fun' with the "Geradenfunktionsparameter" of each
  ;; intersection of the line (x1,y1),(x2,y2) and the polygon denoted
  ;; by 'points' in a "sensible" way. --GB
  (let ((n (length points)))
    (dotimes (i n)
      (let ((pv  (elt points (mod (- i 1) n)))          ;the point before
            (po  (elt points (mod i n)))                ;the "current" point
            (pn  (elt points (mod (+ i 1) n)))          ;the point after
            (pnn (elt points (mod (+ i 2) n))))         ;the point after**2
        (cond
         ;; The line goes directly thru' po
         ((line-contains-point-p** x1 y1 x2 y2 (point-x po) (point-y po))
           (let ((sign-1 (geraden-gleichung x1 y1 x2 y2 (point-x pn) (point-y pn)))
                 (sign-2 (geraden-gleichung x1 y1 x2 y2 (point-x pv) (point-y pv))))
             (cond ((or (and (> sign-1 0) (< sign-2 0))
                        (and (< sign-1 0) (> sign-2 0)))
                    ;; clear cases: the line croses the polygon's border
                    (funcall fun (position->geraden-fktn-parameter x1 y1 x2 y2 (point-x po) (point-y po)) ))
                   ((= sign-1 0)
                    ;; more difficult:
                    ;; The line is coincident with the edge po/pn
                    (let ((sign-1 (geraden-gleichung x1 y1 x2 y2 (point-x pnn) (point-y pnn))))
                      (cond ((or (and (> sign-1 0) (< sign-2 0))
                                 (and (< sign-1 0) (> sign-2 0)))
                             ;; The line goes through the polygons border, by edge po/pn
                             (funcall fun (position->geraden-fktn-parameter x1 y1 x2 y2 (point-x po) (point-y po)) ))
                            (t
                             ;; otherwise the line touches the polygon at the edge po/pn,
                             ;; return both points
                             (funcall fun (position->geraden-fktn-parameter x1 y1 x2 y2 (point-x po) (point-y po)) )
                             (funcall fun (position->geraden-fktn-parameter x1 y1 x2 y2 (point-x pn) (point-y pn)) ) ))))
                   (t
                    ;; all other cases: Line either touches polygon in
                    ;; a point or in an edge [handled above]. --GB
                    nil) )))
         ((line-contains-point-p** x1 y1 x2 y2 (point-x pn) (point-y pn))
          nil)
         (t
          (multiple-value-bind (k m) 
              (geraden-schnitt/prim x1 y1 x2 y2 (point-x po) (point-y po) (point-x pn) (point-y pn))
            (when (and k (<= 0 m 1))                    ;Moegliche numerische Instabilitaet
              (funcall fun k)))))))))

(defun schnitt-gerade/polygon-prim (x1 y1 x2 y2 points)
  (let ((res nil))
    (map-over-schnitt-gerade/polygon (lambda (k) (push k res)) x1 y1 x2 y2 points)
    (sort res #'<)))

(defun schnitt-line/polygon (x1 y1 x2 y2 polygon)
  (let ((ks (schnitt-gerade/polygon-prim x1 y1 x2 y2 (polygon-points polygon))))
    (assert (evenp (length ks)))
    (let ((res nil))
      (do ((q ks (cddr q)))
          ((null q))
        (let ((k1 (max 0d0 (min 1d0 (car q))))
              (k2 (max 0d0 (min 1d0 (cadr q)))))
          (when (/= k1 k2)
            (push (make-line* (+ x1 (* k1 (- x2 x1))) (+ y1 (* k1 (- y2 y1)))
                              (+ x1 (* k2 (- x2 x1))) (+ y1 (* k2 (- y2 y1))))
                  res))))
      (cond ((null res) +nowhere+)
            ((null (cdr res)) (car res))
            (t (make-instance 'standard-region-union :regions res)) ))))

(defmethod region-contains-position-p ((pg polygon) x y)
  (setf x (coerce x 'coordinate))
  (setf y (coerce y 'coordinate))
  (let ((n 0) (m 0))
    (map-over-schnitt-gerade/polygon (lambda (k) 
                                       (when (>= k 0) (incf n))
                                       (incf m))
                                     x y (+ x 1) y (polygon-points pg))
    (assert (evenp m))
    (oddp n)))

(defmethod region-intersection ((a standard-line) (b standard-polygon))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (schnitt-line/polygon x1 y1 x2 y2 b))))

(defmethod region-intersection ((b standard-polygon) (a standard-line))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (schnitt-line/polygon x1 y1 x2 y2 b))))

(defmethod region-intersection ((a standard-line) (b standard-rectangle))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (schnitt-line/polygon x1 y1 x2 y2 b))))

(defmethod region-intersection ((b standard-rectangle) (a standard-line))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (schnitt-line/polygon x1 y1 x2 y2 b))))



(defmethod region-difference ((a standard-line) (b standard-polygon))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (differenz-line/polygon x1 y1 x2 y2 b))))

(defmethod region-difference ((a standard-line) (b standard-rectangle))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (differenz-line/polygon x1 y1 x2 y2 b))))

(defun differenz-line/polygon (x1 y1 x2 y2 polygon)
  (let ((ks (schnitt-gerade/polygon-prim x1 y1 x2 y2 (polygon-points polygon))))
    (assert (evenp (length ks)))
    (let ((res nil)
          (res2 nil))
      (push 0d0 res)
      (do ((q ks (cddr q)))
          ((null q))
        (let ((k1 (max 0d0 (min 1d0 (car q))))
              (k2 (max 0d0 (min 1d0 (cadr q)))))
          (when (/= k1 k2)
            (push k1 res)
            (push k2 res))))
      (push 1d0 res)
      (setf res (nreverse res))
      (do ((q res (cddr q)))
          ((null q))
        (let ((k1 (car q))
              (k2 (cadr q)))
          (when (/= k1 k2)
            (push (make-line* (+ x1 (* k1 (- x2 x1))) (+ y1 (* k1 (- y2 y1)))
                              (+ x1 (* k2 (- x2 x1))) (+ y1 (* k2 (- y2 y1))))
                  res2))))
      (cond ((null res2) +nowhere+)
            ((null (cdr res2)) (car res2))
            (t (make-instance 'standard-region-union :regions res2)) ))))


(defmethod region-difference ((a standard-line) (b standard-line))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (multiple-value-bind (u1 v1) (line-start-point* b)
        (multiple-value-bind (u2 v2) (line-end-point* b)
          (cond ((and (coordinate= 0 (geraden-gleichung x1 y1 x2 y2 u1 v1))
                      (coordinate= 0 (geraden-gleichung x1 y1 x2 y2 u2 v2)))
                 (let ((k1 (position->geraden-fktn-parameter x1 y1 x2 y2 u1 v1))
                       (k2 (position->geraden-fktn-parameter x1 y1 x2 y2 u2 v2)))
                   (psetq k1 (max 0 (min k1 k2))
                          k2 (min 1 (max k1 k2)))
                   (let ((r (nconc (if (> k1 0)
                                       (list (make-line* x1 y1 (+ x1 (* k1 (- x2 x1))) (+ y1 (* k1 (- y2 y1)))))
                                     nil)
                                   (if (< k2 1)
                                       (list (make-line* (+ x1 (* k2 (- x2 x1))) (+ y1 (* k2 (- y2 y1))) x2 y2))
                                     nil))))
                     (cond ((null r) +nowhere+)
                           ((null (cdr r)) (car r))
                           (t (make-instance 'standard-region-union :regions r)) ))))
                (t
                 a)))))))

(defmethod region-union ((a standard-line) (b standard-line))
  (multiple-value-bind (x1 y1) (line-start-point* a)
    (multiple-value-bind (x2 y2) (line-end-point* a)
      (multiple-value-bind (u1 v1) (line-start-point* b)
        (multiple-value-bind (u2 v2) (line-end-point* b)
          (cond ((and (coordinate= 0 (geraden-gleichung x1 y1 x2 y2 u1 v1))
                      (coordinate= 0 (geraden-gleichung x1 y1 x2 y2 u2 v2)))
                 (let ((k1 (position->geraden-fktn-parameter x1 y1 x2 y2 u1 v1))
                       (k2 (position->geraden-fktn-parameter x1 y1 x2 y2 u2 v2)))
                   (psetq k1 (min k1 k2)
                          k2 (max k1 k2))
                   (cond ((and (<= k1 1) (>= k2 0))
                          (let ((k1 (min 0 k1))
                                (k2 (max 1 k2)))
                            (make-line* (+ x1 (* k1 (- x2 x1))) (+ y1 (* k1 (- y2 y1)))
                                        (+ x1 (* k2 (- x2 x1))) (+ y1 (* k2 (- y2 y1))))))
                         (t
                          (make-instance 'standard-region-union :regions (list a b))))))
                ((and (coordinate= x1 u1) (coordinate= y1 v1))
                 (make-polyline* (list u2 v2 x1 y1 x2 y2)))
                ((and (coordinate= x2 u2) (coordinate= y2 v2))
                 (make-polyline* (list x1 y1 x2 y2 u1 v1)))
                ((and (coordinate= x1 u2) (coordinate= y1 v2))
                 (make-polyline* (list u1 v1 x1 y1 x2 y2)))
                ((and (coordinate= x2 u1) (coordinate= y2 v1))
                 (make-polyline* (list x1 y1 x2 y2 u2 v2)))
                (t
                 (make-instance 'standard-region-union :regions (list a b))) ))))))

(defmethod region-union ((a standard-polyline) (b standard-line))
  (with-slots (points) a
    (cond ((polyline-closed a)
           (make-instance 'standard-region-union :regions (list a b)))
          ((region-equal (car points) (line-end-point b))
           (make-polyline (cons (line-start-point b) points)))
          ((region-equal (car points) (line-start-point b))
           (make-polyline (cons (line-end-point b) points)))
          ((region-equal (car (last points)) (line-end-point b))
           (make-polyline (append points (list (line-start-point b)))))
          ((region-equal (car (last points)) (line-start-point b))
           (make-polyline (append points (list (line-end-point b)))))
          (t
           (make-instance 'standard-region-union :regions (list a b))))))

(defmethod region-union ((a standard-line) (b standard-polyline))
  (region-union b a))

(defmethod region-union ((a standard-polyline) (b standard-polyline))
  (with-slots ((a-points points)) a
    (with-slots ((b-points points)) b
      (cond ((polyline-closed a)
             (make-instance 'standard-region-union :regions (list a b)))
            ((polyline-closed b)
             (make-instance 'standard-region-union :regions (list a b)))
            ((region-equal (car a-points) (car b-points))
             (make-polyline (append (reverse (cdr a-points)) b-points)))
            ((region-equal (car (last a-points)) (car (last b-points)))
             (make-polyline (append a-points (reverse (cdr b-points)))))
            ((region-equal (car a-points) (car (last b-points)))
             (make-polyline (append b-points (cdr a-points))))
            ((region-equal (car (last a-points)) (car b-points))
             (make-polyline (append a-points (cdr b-points))))
            (t
             (make-instance 'standard-region-union :regions (list a b)))))))

(defmethod region-union ((a standard-rectangle-set) (b polygon))
  (region-union (rectangle-set->polygon-union a) b))

(defmethod region-union ((a polygon) (b standard-rectangle-set))
  (region-union a (rectangle-set->polygon-union b)))

(defun rectangle-set->polygon-union (rs)
  (let ((res nil))
    (map-over-region-set-regions (lambda (r) (push r res)) rs)
    (make-instance 'standard-region-union :regions res)))

(defmethod region-union ((a standard-region-difference) (b region))
  (make-instance 'standard-region-union :regions (list a b)))

(defmethod region-union ((a region) (b standard-region-difference))
  (make-instance 'standard-region-union :regions (list a b)))



(defmethod region-equal ((a standard-line) (b standard-line))
  (or (and (region-equal (line-start-point a) (line-start-point b))
           (region-equal (line-end-point a) (line-end-point b)))
      (and (region-equal (line-start-point a) (line-end-point b))
           (region-equal (line-end-point a) (line-start-point b)))))

(defmethod region-union ((a nowhere-region) (b nowhere-region))
  +nowhere+)


(defmethod region-exclusive-or ((a region) (b region))
  (region-union (region-difference a b) (region-difference b a)))
  

(defmethod region-contains-region-p ((a region) (b point))
  (region-contains-position-p a (point-x b) (point-y b)))

;; xxx was ist mit (region-contains-region-p x +nowhere+) ?

(defmethod region-contains-region-p ((a everywhere-region) (b region))
  t)

(defmethod region-contains-region-p ((a nowhere-region) (b region))
  nil)

(defmethod region-contains-region-p ((a everywhere-region) (b everywhere-region))
  t)

(defmethod region-contains-region-p ((a region) (b everywhere-region))
  ;; ??? was ist mit
  ;; (region-union (region-difference +everywhere+ X) X) ???
  nil)

(defmethod region-contains-region-p ((a region) (b nowhere-region))
  t)

;;   REGION-CONTAINS-REGION-P region1 region2
;;
;;        Returns t if all points in the region region2 are members of the
;;        region region1; otherwise, it returns nil. 
;;    
;;        aka region2 ist teilmenge von region1  aka B\A = 0
;;
;;   REGION-INTERSECTS-REGION-P region1 region2
;;
;;        Returns nil if region-intersection of the two regions region1 and
;;        region2 would be +nowhere+; otherwise, it returns t. 
;;
;;        aka region1 und region2 sind nicht disjunkt  aka AB /= 0
;;


;; generic versions
(defmethod region-equal ((a region) (b region))
  (region-equal +nowhere+ (region-exclusive-or a b)))

(defmethod region-intersects-region-p ((a region) (b region))
  (not (region-equal +nowhere+ (region-intersection a b))))

(defmethod region-contains-region-p ((a region) (b region))
  (or (eq a b)
      (region-equal +nowhere+ (region-difference b a))))
  
;;;; ====================================================================================================

(defmethod bounding-rectangle* ((a standard-line))
  (with-slots (x1 y1 x2 y2) a
    (values (min x1 x2) (min y1 y2) (max x1 x2) (max y1 y2))))

(defmethod bounding-rectangle* ((a standard-rectangle))
  (with-standard-rectangle (x1 y1 x2 y2)
      a
    (values x1 y1 x2 y2)))

(defmethod bounding-rectangle* ((self standard-rectangle-set))
  (with-slots (bands bounding-rectangle) self
    (values-list (or bounding-rectangle
                     (setf bounding-rectangle
                       (let (bx1 by1 bx2 by2)
                         (map-over-bands-rectangles (lambda (x1 y1 x2 y2)
                                                      (setf bx1 (min (or bx1 x1) x1)
                                                            bx2 (max (or bx2 x2) x2)
                                                            by1 (min (or by1 y1) y1)
                                                            by2 (max (or by2 y2) y2)))
                                                    bands)
                         (list bx1 by1 bx2 by2)))))))

(defmethod bounding-rectangle* ((self standard-polygon))
  (values (reduce #'min (mapcar #'point-x (polygon-points self)))
          (reduce #'min (mapcar #'point-y (polygon-points self)))
          (reduce #'max (mapcar #'point-x (polygon-points self)))
          (reduce #'max (mapcar #'point-y (polygon-points self)))))

(defmethod bounding-rectangle* ((self standard-polyline))
  (values (reduce #'min (mapcar #'point-x (polygon-points self)))
          (reduce #'min (mapcar #'point-y (polygon-points self)))
          (reduce #'max (mapcar #'point-x (polygon-points self)))
          (reduce #'max (mapcar #'point-y (polygon-points self)))))
          
(defmethod bounding-rectangle* ((self standard-point))
  (with-slots (x y) self
    (values x y x y)))

(defmethod bounding-rectangle* ((self standard-region-union))
  (let (bx1 by1 bx2 by2)
    (map-over-region-set-regions (lambda (r)
                                   (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* r)
                                     (setf bx1 (min (or bx1 x1) x1)
                                           bx2 (max (or bx2 x2) x2)
                                           by1 (min (or by1 y1) y1)
                                           by2 (max (or by2 y2) y2))))
                                 self)
    (values bx1 by1 bx2 by2)))

(defmethod bounding-rectangle* ((self standard-region-difference))
  (with-slots (a b) self
    (cond ((eq a +everywhere+)
           (bounding-rectangle* b))
          (t
           (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* a)
             (multiple-value-bind (u1 v1 u2 v2) (bounding-rectangle* b)
               (values (min x1 u1) (min y1 v1)
                       (max x2 u2) (min y2 v2))))) )))

(defmethod bounding-rectangle* ((self standard-region-intersection))
  ;; kill+yank alert
  (let (bx1 by1 bx2 by2)
    (map-over-region-set-regions (lambda (r)
                                   (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* r)
                                     (setf bx1 (min (or bx1 x1) x1)
                                           bx2 (max (or bx2 x2) x2)
                                           by1 (min (or by1 y1) y1)
                                           by2 (max (or by2 y2) y2))))
                                 self)
    (values bx1 by1 bx2 by2)))

;;;; ====================================================================================================

(defun make-bounding-rectangle (x1 y1 x2 y2)
  (setf x1 (coerce x1 'coordinate)
        y1 (coerce y1 'coordinate)
        x2 (coerce x2 'coordinate)
        y2 (coerce y2 'coordinate))
  (make-instance 'standard-bounding-rectangle :x1 (min x1 x2) :y1 (min y1 y2) :x2 (max x1 x2) :y2 (max y1 y2)))

(defmethod bounding-rectangle ((region rectangle))
  region)

(defmethod bounding-rectangle ((region region))
  (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* region)
    (make-bounding-rectangle x1 y1 x2 y2)))

(defmacro with-bounding-rectangle* ((min-x min-y max-x max-y) region &body body)
  ;; What is the purpose of this macro; IHMO m.-v.-b. looks as nice as with-b.-.r. .
  `(multiple-value-bind (,min-x ,min-y ,max-x ,max-y) (bounding-rectangle* ,region)
     ,@body))

(defmethod bounding-rectangle-position ((self bounding-rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* self)
    (declare (ignore x2 y2))
    (values x1 y1)))

(defmethod set-bounding-rectangle-position ((self standard-rectangle) x y)
  ;;(error "DO NOT CALL ME")
  ;;Yes, but... output records are based on rectangles
  (with-standard-rectangle (x1 y1 x2 y2)
      self
    (setf (rectangle-edges* self)
	  (values x y (+ x (- x2 x1)) (+ y (- y2 y1))))))

(defmethod bounding-rectangle-min-x ((self bounding-rectangle)) 
  (nth-value 0 (bounding-rectangle* self)))

(defmethod bounding-rectangle-min-y ((self bounding-rectangle))
  (nth-value 1 (bounding-rectangle* self)))

(defmethod bounding-rectangle-max-x ((self bounding-rectangle))
  (nth-value 2 (bounding-rectangle* self)))

(defmethod bounding-rectangle-max-y ((self bounding-rectangle))
  (nth-value 3 (bounding-rectangle* self)))

(defmethod bounding-rectangle-width ((self bounding-rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* self)
    (declare (ignore y1 y2))
    (- x2 x1)))

(defmethod bounding-rectangle-height ((self bounding-rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* self)
    (declare (ignore x1 x2))
    (- y2 y1)))

(defmethod bounding-rectangle-size ((self bounding-rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* self)
    (values (- x2 x1) (- y2 y1))))

;;;

(defmethod print-object ((self standard-rectangle) stream)
  (print-unreadable-object (self stream :type t :identity t)
    (with-standard-rectangle (x1 y1 x2 y2)
      self
      (format stream "X ~S:~S Y ~S:~S" x1 x2 y1 y2))))

;;;;


(defmethod region-intersects-region-p :around ((a bounding-rectangle) (b bounding-rectangle))
  (multiple-value-bind (x1 y1 x2 y2) (bounding-rectangle* a)
    (multiple-value-bind (u1 v1 u2 v2) (bounding-rectangle* b)
      (cond ((and (<= u1 x2) (<= x1 u2)
                  (<= v1 y2) (<= y1 v2))
             (call-next-method))
            (t
             nil)))))

(defmethod region-intersects-region-p ((a standard-rectangle) (b standard-rectangle))
  (declare (ignorable a b))
  ;; for rectangles, the bounding rectangle test is correct, so if we
  ;; wind up here, we just can return T.
  t
  ;;(multiple-value-bind (x1 y1 x2 y2) (rectangle-edges* a)
  ;;  (multiple-value-bind (u1 v1 u2 v2) (rectangle-edges* b)
  ;;    (and (<= u1 x2) (<= x1 u2)
  ;;         (<= v1 y2) (<= y1 v2))))
  )

;;; Internal helpers

(defmacro with-grown-rectangle* (((out-x1 out-y1 out-x2 out-y2)
                                  (in-x1 in-y1 in-x2 in-y2)
                                  &key
                                  radius
                                  (radius-x radius)
                                  (radius-y radius)
                                  (radius-left  radius-x)
                                  (radius-right radius-x)
                                  (radius-top    radius-y)
                                  (radius-bottom radius-y))
                                  &body body)
  `(multiple-value-bind (,out-x1 ,out-y1 ,out-x2 ,out-y2)
    (values (- ,in-x1 ,radius-left)
     (- ,in-y1 ,radius-top)
     (+ ,in-x2 ,radius-right)
     (+ ,in-y2 ,radius-bottom))
    ,@body))