Duncan's patch to correct orientation of triangles
[gpclib.git] / tests / reg-tests.R
blob44d5a55ff2754ab2f50ed652cb3c8996e204eeb7
1 library(gpclib)
3 ## Make some random polygons
4 set.seed(100)
5 a <- cbind(rnorm(100), rnorm(100))
6 a <- a[chull(a), ]
8 ## Convert `a' from matrix to "gpc.poly"
9 a <- as(a, "gpc.poly")
10 show(a)
12 b <- cbind(rnorm(100), rnorm(100))
13 b <- as(b[chull(b), ], "gpc.poly")
14 show(b)
16 ## More complex polygons with an intersection
17 p1 <- read.polyfile(system.file("poly-ex/ex-poly1.txt", package = "gpclib"))
18 p2 <- read.polyfile(system.file("poly-ex/ex-poly2.txt", package = "gpclib"))
20 ## Plot both polygons and highlight their intersection in red
21 plot(app <- append.poly(p1, p2))
22 show(app)
23 plot(int <- intersect(p1, p2), poly.args = list(col = 2), add = TRUE)
24 show(int)
26 ## Highlight the difference p1 \ p2 in green
27 plot(sdif <- setdiff(p1, p2), poly.args = list(col = 3), add = TRUE)
28 show(sdif)
30 ## Highlight the difference p2 \ p1 in blue
31 plot(sdif <- setdiff(p2, p1), poly.args = list(col = 4), add = TRUE)
32 str(sdif)
34 ## Plot the union of the two polygons
35 plot(un <- union(p1, p2))
36 str(un)
38 ## Take the non-intersect portions and create a new polygon
39 ## combining the two contours
40 p.comb <- append.poly(setdiff(p1, p2), setdiff(p2, p1))
41 str(p.comb)
45 ## Coerce from a matrix
46 x <- 
47 structure(c(0.0934073560027759, 0.192713393476752, 0.410062456627342, 
48 0.470020818875781, 0.41380985426787, 0.271408743927828, 0.100902151283831, 
49 0.0465648854961832, 0.63981588032221, 0.772382048331416,
50 0.753739930955121, 0.637744533947066, 0.455466052934407,
51 0.335327963176065, 0.399539700805524, 
52 0.600460299194476), .Dim = c(8, 2))
53 y <- 
54 structure(c(0.404441360166551, 0.338861901457321, 0.301387925052047, 
55 0.404441360166551, 0.531852879944483, 0.60117973629424, 0.625537820957668, 
56 0.179976985040276, 0.341542002301496, 0.445109321058688,
57 0.610817031070196, 0.596317606444189, 0.459608745684695,
58 0.215189873417722), .Dim = c(7, 2))
60 x1 <- as(x, "gpc.poly")
61 y1 <- as(y, "gpc.poly")
63 plot(append.poly(x1, y1))
64 plot(intersect(x1, y1), poly.args = list(col = 2), add = TRUE)
67 ## Show the triangulation
68 plot(append.poly(x1, y1))
69 triangles <- triangulate(append.poly(x1,y1))
70 print(triangles)