2 R : Copyright 2005, The R Foundation for Statistical Computing
3 Version 2.2.1 (2005-12-20 r36812)
6 R is free software and comes with ABSOLUTELY NO WARRANTY.
7 You are welcome to redistribute it under certain conditions.
8 Type 'license()' or 'licence()' for distribution details.
10 R is a collaborative project with many contributors.
11 Type 'contributors()' for more information and
12 'citation()' on how to cite R or R packages in publications.
14 Type 'demo()' for some demos, 'help()' for on-line help, or
15 'help.start()' for an HTML browser interface to help.
19 General Polygon Clipper Library for R (version 1.3-3)
20 Type 'class ? gpc.poly' for help
22 > ## Make some random polygons
24 > a <- cbind(rnorm(100), rnorm(100))
27 > ## Convert `a' from matrix to "gpc.poly"
28 > a <- as(a, "gpc.poly")
33 BBox (X): -2.271925 --> 2.581959
34 BBox (Y): -2.136494 --> 2.168600
36 > b <- cbind(rnorm(100), rnorm(100))
37 > b <- as(b[chull(b), ], "gpc.poly")
42 BBox (X): -3.020814 --> 2.727888
43 BBox (Y): -2.676379 --> 3.304151
45 > ## More complex polygons with an intersection
46 > p1 <- read.polyfile(system.file("poly-ex/ex-poly1.txt", package = "gpclib"))
47 > p2 <- read.polyfile(system.file("poly-ex/ex-poly2.txt", package = "gpclib"))
49 > ## Plot both polygons and highlight their intersection in red
50 > plot(app <- append.poly(p1, p2))
54 BBox (X): 316.1636 --> 319.2698
55 BBox (Y): 91.66837 --> 93.8952
56 > plot(int <- intersect(p1, p2), poly.args = list(col = 2), add = TRUE)
60 BBox (X): 317.0437 --> 318.0591
61 BBox (Y): 91.81092 --> 93.14354
63 > ## Highlight the difference p1 \ p2 in green
64 > plot(sdif <- setdiff(p1, p2), poly.args = list(col = 3), add = TRUE)
69 BBox (X): 316.1636 --> 317.9963
70 BBox (Y): 91.66837 --> 93.8952
72 > ## Highlight the difference p2 \ p1 in blue
73 > plot(sdif <- setdiff(p2, p1), poly.args = list(col = 4), add = TRUE)
75 Formal class 'gpc.poly' [package "gpclib"] with 1 slots
78 .. .. ..$ x : num [1:71] 319 319 319 319 319 ...
79 .. .. ..$ y : num [1:71] 93.3 93.2 93.0 92.9 92.8 ...
80 .. .. ..$ hole: logi FALSE
82 > ## Plot the union of the two polygons
83 > plot(un <- union(p1, p2))
85 Formal class 'gpc.poly' [package "gpclib"] with 1 slots
88 .. .. ..$ x : num [1:120] 318 318 318 318 318 ...
89 .. .. ..$ y : num [1:120] 93.8 93.7 93.6 93.4 93.3 ...
90 .. .. ..$ hole: logi FALSE
92 .. .. ..$ x : num [1:24] 318 318 318 318 318 ...
93 .. .. ..$ y : num [1:24] 93.1 93.0 93.0 92.9 92.9 ...
94 .. .. ..$ hole: logi TRUE
96 > ## Take the non-intersect portions and create a new polygon
97 > ## combining the two contours
98 > p.comb <- append.poly(setdiff(p1, p2), setdiff(p2, p1))
100 Formal class 'gpc.poly' [package "gpclib"] with 1 slots
103 .. .. ..$ x : num [1:112] 318 318 318 318 318 ...
104 .. .. ..$ y : num [1:112] 93.8 93.7 93.6 93.4 93.3 ...
105 .. .. ..$ hole: logi FALSE
107 .. .. ..$ x : num [1:71] 319 319 319 319 319 ...
108 .. .. ..$ y : num [1:71] 93.3 93.2 93.0 92.9 92.8 ...
109 .. .. ..$ hole: logi FALSE
113 > ## Coerce from a matrix
115 + structure(c(0.0934073560027759, 0.192713393476752, 0.410062456627342,
116 + 0.470020818875781, 0.41380985426787, 0.271408743927828, 0.100902151283831,
117 + 0.0465648854961832, 0.63981588032221, 0.772382048331416,
118 + 0.753739930955121, 0.637744533947066, 0.455466052934407,
119 + 0.335327963176065, 0.399539700805524,
120 + 0.600460299194476), .Dim = c(8, 2))
122 + structure(c(0.404441360166551, 0.338861901457321, 0.301387925052047,
123 + 0.404441360166551, 0.531852879944483, 0.60117973629424, 0.625537820957668,
124 + 0.179976985040276, 0.341542002301496, 0.445109321058688,
125 + 0.610817031070196, 0.596317606444189, 0.459608745684695,
126 + 0.215189873417722), .Dim = c(7, 2))
128 > x1 <- as(x, "gpc.poly")
129 > y1 <- as(y, "gpc.poly")
131 > plot(append.poly(x1, y1))
132 > plot(intersect(x1, y1), poly.args = list(col = 2), add = TRUE)