################################################################## ## ## ## FUNCTION FOR GETTING POLYGON INFORMATION ## ## ## ################################################################## getPolyCoords = function() { load("kaleidoscope.rda") # topLevel contains group polygons # subLevel contains polygons for subgroups within each group top.poly.coords = topLevel$k ### the number of top level groups n.groups = length(top.poly.coords) ### the number of subgroups in each group n.sub = sapply(subLevel, function(grp) length(grp$k)) n.subsub = sapply(subsubLevel, function(grp) length(grp$k)) total.subsubPgons = sum(n.subsub) total.subPgons = sum(n.sub) # which group each subgroup belongs in (numeric id) group.sub = rep(1:n.groups, n.sub) # which group each subsubgroup belongs to subsubs.per.group = numeric(n.groups) j = 1 for (i in 1:n.groups) { subsubs.per.group[i] = sum(n.subsub[j:(j+n.sub[i]-1)]) j = j + n.sub[i] } group.subsub = rep(1:n.groups, subsubs.per.group) ### These are the coordinates of the subgroups polygons sub.poly.coords = vector("list", total.subPgons) j0 = 1 for (i in 1:n.groups) { subGroup = subLevel[[i]]$k j1 = j0 + n.sub[i] - 1 sub.poly.coords[j0:j1] = subGroup j0 = j1 + 1 } ### Coordinates of the subsubgroups polygons subsub.poly.coords = vector("list", total.subsubPgons) j0 = 1 for (i in 1:total.subPgons) { subsubGroup = subsubLevel[[i]]$k j1 = j0 + n.subsub[i] - 1 subsub.poly.coords[j0:j1] = subsubGroup j0 = j1 + 1 } list(top.poly.coords = top.poly.coords, sub.poly.coords = sub.poly.coords, subsub.poly.coords = subsub.poly.coords, n.groups = n.groups, n.sub = n.sub, n.subsub = n.subsub, total.subPgons = total.subPgons, total.subsubPgons = total.subsubPgons, group.sub = group.sub, group.subsub = group.subsub, subsubs.per.group = subsubs.per.group, rdaTopLevel = topLevel, rdaSubLevel = subLevel, rdaSubsubLevel = subsubLevel) } ################################################################# ## ## ## FUNCTIONS FOR ROTATING POLYGONS ## ## ## ################################################################# rotatePgon = function(pgon, theta) { coords = rbind(pgon@pts[[1]]$x, pgon@pts[[1]]$y) theta.rad = theta * pi/180 R = matrix(c(cos(theta.rad), -sin(theta.rad), sin(theta.rad), cos(theta.rad)), nr = 2, byrow = TRUE) rotated.coords = R %*% coords pgon@pts[[1]]$x = rotated.coords[1,] pgon@pts[[1]]$y = rotated.coords[2,] pgon } rotateImage = function(theta, polyVars) { tCoords = vector("list", polyVars$n.groups) sCoords = vector("list", polyVars$total.subPgons) for (i in seq(along = tCoords)) tCoords[[i]] = rotatePgon(polyVars$top.poly.coords[[i]], theta) for (i in seq(along = sCoords)) sCoords[[i]] = rotatePgon(polyVars$sub.poly.coords[[i]], theta) if (!is.null(polyVars$total.subsubPgons)) { ssCoords = vector("list", polyVars$total.subsubPgons) for (i in seq(along = ssCoords)) ssCoords[[i]] = rotatePgon(polyVars$subsub.poly.coords[[i]], theta) } else { ssCoords = NULL } list(top = tCoords, sub = sCoords, subsub = ssCoords) }