coerce op to integer
[gpclib.git] / tests / reg-tests.Rout.save
blob1cfbbc1c379797403878c5ad480e2ec769a6f597
2 R : Copyright 2005, The R Foundation for Statistical Computing
3 Version 2.2.1  (2005-12-20 r36812)
4 ISBN 3-900051-07-0
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.
16 Type 'q()' to quit R.
18 > library(gpclib)
19 General Polygon Clipper Library for R (version 1.3-3)
20         Type 'class ? gpc.poly' for help
21
22 > ## Make some random polygons
23 > set.seed(100)
24 > a <- cbind(rnorm(100), rnorm(100))
25 > a <- a[chull(a), ]
26
27 > ## Convert `a' from matrix to "gpc.poly"
28 > a <- as(a, "gpc.poly")
29 > show(a)
30 GPC Polygon
31    Num. Contours:  1 
32    Num. Vertices:  9 
33    BBox (X):  -2.271925 --> 2.581959 
34    BBox (Y):  -2.136494 --> 2.168600 
35
36 > b <- cbind(rnorm(100), rnorm(100))
37 > b <- as(b[chull(b), ], "gpc.poly")
38 > show(b)
39 GPC Polygon
40    Num. Contours:  1 
41    Num. Vertices:  9 
42    BBox (X):  -3.020814 --> 2.727888 
43    BBox (Y):  -2.676379 --> 3.304151 
44
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"))
48
49 > ## Plot both polygons and highlight their intersection in red
50 > plot(app <- append.poly(p1, p2))
51 > show(app)
52 GPC Polygon
53    Num. Contours:  2 
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)
57 > show(int)
58 GPC Polygon
59    Num. Contours:  2 
60    BBox (X):  317.0437 --> 318.0591 
61    BBox (Y):  91.81092 --> 93.14354 
62
63 > ## Highlight the difference p1 \ p2 in green
64 > plot(sdif <- setdiff(p1, p2), poly.args = list(col = 3), add = TRUE)
65 > show(sdif)
66 GPC Polygon
67    Num. Contours:  1 
68    Num. Vertices:  112 
69    BBox (X):  316.1636 --> 317.9963 
70    BBox (Y):  91.66837 --> 93.8952 
71
72 > ## Highlight the difference p2 \ p1 in blue
73 > plot(sdif <- setdiff(p2, p1), poly.args = list(col = 4), add = TRUE)
74 > str(sdif)
75 Formal class 'gpc.poly' [package "gpclib"] with 1 slots
76   ..@ pts:List of 1
77   .. ..$ :List of 3
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
81
82 > ## Plot the union of the two polygons
83 > plot(un <- union(p1, p2))
84 > str(un)
85 Formal class 'gpc.poly' [package "gpclib"] with 1 slots
86   ..@ pts:List of 2
87   .. ..$ :List of 3
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
91   .. ..$ :List of 3
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
95
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))
99 > str(p.comb)
100 Formal class 'gpc.poly' [package "gpclib"] with 1 slots
101   ..@ pts:List of 2
102   .. ..$ :List of 3
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
106   .. ..$ :List of 3
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
114 > x <- 
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))
121 > y <- 
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)