a = 5; b = 3
h = 0; k = 0
curve((b/a)*sqrt(a^2-((x-h)^2)),
      from = h-a, to = h+a, ylim=c(k-b,k+b))
curve(-(b/a)*sqrt(a^2-((x-h)^2)),
      from = h-a, to = h+a, add = T)

A = pi*a*b; A
## [1] 47.12389
#####
set.seed(1964)
x = runif(100, 0, 1)
y = runif(100, 0, 1)
plot(y~x)

colores = ifelse(((x^2+y)/(x-y))<1,'red','blue')
par(mar = c(0,0,0,0), xpd = T)
plot(x,y, pch = 19, col = colores)
legend('topright', legend = c('sana','enferma'),
       col = colores, title = 'Estado', text.font = 4,
       bg = 'lightblue', inset = c(0,0), fill = colores)

#####
# Lote 1: 450 plantas, el viento tumbo 40
# Lote 2: 420 plantas, el viento tumbo 30
# Lote 3: 400 plantas, el viento tumbo 25
p.l1 = (40/450)*100
p.l2 = (30/400)*100
p.l3 = (25/120)*100
p.l1;p.l2;p.l3
## [1] 8.888889
## [1] 7.5
## [1] 20.83333
mean(c(p.l1,p.l2,p.l3))
## [1] 12.40741
(pm.t = ((40+30+25)/(450+400+120))*100)
## [1] 9.793814
###
# library(rspatial)
library(digest)
library(spatstat)
## Loading required package: spatstat.data
## Loading required package: nlme
## Loading required package: rpart
## 
## spatstat 1.59-0       (nickname: 'J'ai omis les oeufs de caille') 
## For an introduction to spatstat, type 'beginner'

nclust <- function(x0, y0, radius, n){
  return(runifdisc(n, radius, centre=c(x0,y0)))
}
plot(rPoissonCluster(5,0.2, nclust, radius = 0.2, n=100))

plot(rPoissonCluster(5,0.2, nclust, radius = 0.5, n=100))

x3 = as.numeric(gl(10,1,100))
y3 = as.numeric(gl(10,10,100))
plot(x3,y3)

xy = expand.grid(x = seq(1,90,9), y = seq(1,70,7))
xy
##      x  y
## 1    1  1
## 2   10  1
## 3   19  1
## 4   28  1
## 5   37  1
## 6   46  1
## 7   55  1
## 8   64  1
## 9   73  1
## 10  82  1
## 11   1  8
## 12  10  8
## 13  19  8
## 14  28  8
## 15  37  8
## 16  46  8
## 17  55  8
## 18  64  8
## 19  73  8
## 20  82  8
## 21   1 15
## 22  10 15
## 23  19 15
## 24  28 15
## 25  37 15
## 26  46 15
## 27  55 15
## 28  64 15
## 29  73 15
## 30  82 15
## 31   1 22
## 32  10 22
## 33  19 22
## 34  28 22
## 35  37 22
## 36  46 22
## 37  55 22
## 38  64 22
## 39  73 22
## 40  82 22
## 41   1 29
## 42  10 29
## 43  19 29
## 44  28 29
## 45  37 29
## 46  46 29
## 47  55 29
## 48  64 29
## 49  73 29
## 50  82 29
## 51   1 36
## 52  10 36
## 53  19 36
## 54  28 36
## 55  37 36
## 56  46 36
## 57  55 36
## 58  64 36
## 59  73 36
## 60  82 36
## 61   1 43
## 62  10 43
## 63  19 43
## 64  28 43
## 65  37 43
## 66  46 43
## 67  55 43
## 68  64 43
## 69  73 43
## 70  82 43
## 71   1 50
## 72  10 50
## 73  19 50
## 74  28 50
## 75  37 50
## 76  46 50
## 77  55 50
## 78  64 50
## 79  73 50
## 80  82 50
## 81   1 57
## 82  10 57
## 83  19 57
## 84  28 57
## 85  37 57
## 86  46 57
## 87  55 57
## 88  64 57
## 89  73 57
## 90  82 57
## 91   1 64
## 92  10 64
## 93  19 64
## 94  28 64
## 95  37 64
## 96  46 64
## 97  55 64
## 98  64 64
## 99  73 64
## 100 82 64
plot(xy, cex = 0.5, pch = 16)

###
set.seed(1964)
x4 = runif(20)
y4 = runif(20)
plot(x4, y4, cex = 0.5, pch = 16)

cont.conv = chull(x4,y4)

set.seed(1964)
dat <- matrix(rnorm(200), ncol = 2)
(ch <- chull(dat))
## [1] 79 44 81 52 83 96 56 99 55
coords <- dat[c(ch, ch[1]),]; coords
##             [,1]       [,2]
##  [1,]  2.8181775 -0.6573410
##  [2,]  0.6885724 -1.9870463
##  [3,] -0.3649199 -2.1106867
##  [4,] -1.8953622 -1.9202198
##  [5,] -2.2157494 -1.2504596
##  [6,] -2.9116270  0.3833158
##  [7,] -3.2962041  2.7097300
##  [8,]  1.8287782  2.0858646
##  [9,]  2.6259778  0.5570128
## [10,]  2.8181775 -0.6573410
plot(dat, pch=19, cex = 0.5)
lines(coords, col="red")

#####
library(spatstat)
# demo(spatstat)
data("swedishpines")
(x = swedishpines)
## Planar point pattern: 71 points
## window: rectangle = [0, 96] x [0, 100] units (one unit = 0.1 metres)
windows(7,7)
plot(x)

class(x)
## [1] "ppp"
#####
library(spatstat)
set.seed(1964)
x5 = runif(40)
y5 = runif(40)
plot(x5,y5, col='darkblue',pch=19)
grid(22,col = 'gray60')

dataxy = ppp(x5,y5)
(q = t(quadratcount(dataxy, nx = 22, ny = 22)))
##                  y
## x                 [0.955,1] [0.909,0.955) [0.864,0.909) [0.818,0.864)
##   [0,0.0455)              0             0             0             0
##   [0.0455,0.0909)         0             0             0             0
##   [0.0909,0.136)          0             0             0             0
##   [0.136,0.182)           0             0             0             0
##   [0.182,0.227)           1             0             0             0
##   [0.227,0.273)           0             0             0             0
##   [0.273,0.318)           0             0             1             0
##   [0.318,0.364)           0             0             0             0
##   [0.364,0.409)           0             0             0             0
##   [0.409,0.455)           0             0             0             0
##   [0.455,0.5)             0             0             0             0
##   [0.5,0.545)             0             1             0             0
##   [0.545,0.591)           0             0             0             0
##   [0.591,0.636)           0             0             0             0
##   [0.636,0.682)           0             0             0             0
##   [0.682,0.727)           0             0             0             0
##   [0.727,0.773)           0             0             0             1
##   [0.773,0.818)           0             0             0             0
##   [0.818,0.864)           0             0             0             0
##   [0.864,0.909)           0             0             0             0
##   [0.909,0.955)           0             0             0             1
##   [0.955,1]               0             0             0             1
##                  y
## x                 [0.773,0.818) [0.727,0.773) [0.682,0.727) [0.636,0.682)
##   [0,0.0455)                  0             0             0             0
##   [0.0455,0.0909)             0             0             0             0
##   [0.0909,0.136)              0             1             0             0
##   [0.136,0.182)               1             0             0             1
##   [0.182,0.227)               0             0             0             0
##   [0.227,0.273)               0             0             0             0
##   [0.273,0.318)               0             0             0             0
##   [0.318,0.364)               0             0             0             0
##   [0.364,0.409)               0             0             0             0
##   [0.409,0.455)               0             0             0             0
##   [0.455,0.5)                 0             0             0             0
##   [0.5,0.545)                 0             1             1             0
##   [0.545,0.591)               0             0             0             0
##   [0.591,0.636)               0             0             0             0
##   [0.636,0.682)               0             0             0             0
##   [0.682,0.727)               0             0             0             0
##   [0.727,0.773)               0             0             0             0
##   [0.773,0.818)               0             0             0             1
##   [0.818,0.864)               0             0             0             0
##   [0.864,0.909)               0             0             0             0
##   [0.909,0.955)               0             1             1             0
##   [0.955,1]                   0             1             0             0
##                  y
## x                 [0.591,0.636) [0.545,0.591) [0.5,0.545) [0.455,0.5)
##   [0,0.0455)                  0             0           0           0
##   [0.0455,0.0909)             0             0           0           0
##   [0.0909,0.136)              0             0           1           0
##   [0.136,0.182)               0             0           0           0
##   [0.182,0.227)               0             0           0           0
##   [0.227,0.273)               0             0           0           0
##   [0.273,0.318)               1             0           0           0
##   [0.318,0.364)               0             0           0           0
##   [0.364,0.409)               0             0           0           0
##   [0.409,0.455)               0             0           0           0
##   [0.455,0.5)                 0             0           0           0
##   [0.5,0.545)                 0             0           0           0
##   [0.545,0.591)               0             0           0           0
##   [0.591,0.636)               0             0           1           0
##   [0.636,0.682)               0             1           0           0
##   [0.682,0.727)               0             0           0           0
##   [0.727,0.773)               0             0           0           0
##   [0.773,0.818)               0             0           0           0
##   [0.818,0.864)               0             0           0           0
##   [0.864,0.909)               0             0           0           0
##   [0.909,0.955)               0             0           0           0
##   [0.955,1]                   0             0           1           0
##                  y
## x                 [0.409,0.455) [0.364,0.409) [0.318,0.364) [0.273,0.318)
##   [0,0.0455)                  0             0             0             0
##   [0.0455,0.0909)             0             0             0             0
##   [0.0909,0.136)              0             0             0             0
##   [0.136,0.182)               0             0             1             0
##   [0.182,0.227)               0             0             0             0
##   [0.227,0.273)               0             0             0             0
##   [0.273,0.318)               0             1             0             0
##   [0.318,0.364)               0             0             0             0
##   [0.364,0.409)               0             1             0             0
##   [0.409,0.455)               0             1             0             0
##   [0.455,0.5)                 0             0             0             0
##   [0.5,0.545)                 0             1             0             0
##   [0.545,0.591)               0             0             0             0
##   [0.591,0.636)               0             0             0             0
##   [0.636,0.682)               0             0             0             0
##   [0.682,0.727)               0             0             0             0
##   [0.727,0.773)               0             0             0             0
##   [0.773,0.818)               0             0             0             0
##   [0.818,0.864)               1             0             0             0
##   [0.864,0.909)               0             0             0             0
##   [0.909,0.955)               0             0             0             0
##   [0.955,1]                   0             0             0             0
##                  y
## x                 [0.227,0.273) [0.182,0.227) [0.136,0.182) [0.0909,0.136)
##   [0,0.0455)                  0             0             0              0
##   [0.0455,0.0909)             1             0             0              0
##   [0.0909,0.136)              0             0             0              0
##   [0.136,0.182)               0             0             0              1
##   [0.182,0.227)               0             1             0              0
##   [0.227,0.273)               0             1             0              0
##   [0.273,0.318)               1             0             0              0
##   [0.318,0.364)               0             0             0              0
##   [0.364,0.409)               0             0             0              0
##   [0.409,0.455)               0             1             0              0
##   [0.455,0.5)                 0             0             0              0
##   [0.5,0.545)                 0             0             1              0
##   [0.545,0.591)               0             0             0              0
##   [0.591,0.636)               0             0             0              0
##   [0.636,0.682)               0             0             0              0
##   [0.682,0.727)               0             0             0              0
##   [0.727,0.773)               0             0             0              0
##   [0.773,0.818)               0             0             0              0
##   [0.818,0.864)               0             0             0              1
##   [0.864,0.909)               0             0             0              0
##   [0.909,0.955)               0             0             1              0
##   [0.955,1]                   0             0             0              0
##                  y
## x                 [0.0455,0.0909) [0,0.0455)
##   [0,0.0455)                    0          0
##   [0.0455,0.0909)               0          0
##   [0.0909,0.136)                0          0
##   [0.136,0.182)                 0          1
##   [0.182,0.227)                 0          0
##   [0.227,0.273)                 0          0
##   [0.273,0.318)                 0          0
##   [0.318,0.364)                 0          0
##   [0.364,0.409)                 0          0
##   [0.409,0.455)                 1          1
##   [0.455,0.5)                   0          0
##   [0.5,0.545)                   0          1
##   [0.545,0.591)                 0          0
##   [0.591,0.636)                 0          0
##   [0.636,0.682)                 0          1
##   [0.682,0.727)                 0          0
##   [0.727,0.773)                 0          0
##   [0.773,0.818)                 0          0
##   [0.818,0.864)                 0          0
##   [0.864,0.909)                 0          0
##   [0.909,0.955)                 0          0
##   [0.955,1]                     0          0
(q2 = t(quadratcount(dataxy, nx = 20, ny = 20)))
##             y
## x            [0.95,1] [0.9,0.95) [0.85,0.9) [0.8,0.85) [0.75,0.8)
##   [0,0.05)          0          0          0          0          0
##   [0.05,0.1)        0          0          0          0          0
##   [0.1,0.15)        0          0          0          0          1
##   [0.15,0.2)        0          0          0          0          0
##   [0.2,0.25)        1          0          0          0          0
##   [0.25,0.3)        0          0          1          0          0
##   [0.3,0.35)        0          0          0          0          0
##   [0.35,0.4)        0          0          0          0          0
##   [0.4,0.45)        0          0          0          0          0
##   [0.45,0.5)        0          0          0          0          0
##   [0.5,0.55)        0          1          0          0          0
##   [0.55,0.6)        0          0          0          0          0
##   [0.6,0.65)        0          0          0          0          0
##   [0.65,0.7)        0          0          0          0          0
##   [0.7,0.75)        0          0          0          1          0
##   [0.75,0.8)        0          0          0          0          0
##   [0.8,0.85)        0          0          0          0          0
##   [0.85,0.9)        0          0          0          0          0
##   [0.9,0.95)        0          0          0          1          0
##   [0.95,1]          0          0          0          1          1
##             y
## x            [0.7,0.75) [0.65,0.7) [0.6,0.65) [0.55,0.6) [0.5,0.55)
##   [0,0.05)            0          0          0          0          0
##   [0.05,0.1)          0          0          0          0          0
##   [0.1,0.15)          1          0          0          0          1
##   [0.15,0.2)          0          1          0          0          0
##   [0.2,0.25)          0          0          0          0          0
##   [0.25,0.3)          0          0          0          0          0
##   [0.3,0.35)          0          0          1          0          0
##   [0.35,0.4)          0          0          0          0          0
##   [0.4,0.45)          0          0          0          0          0
##   [0.45,0.5)          0          0          0          0          0
##   [0.5,0.55)          2          0          0          0          0
##   [0.55,0.6)          0          0          0          0          1
##   [0.6,0.65)          0          0          0          1          0
##   [0.65,0.7)          0          0          0          0          0
##   [0.7,0.75)          0          0          0          0          0
##   [0.75,0.8)          0          1          0          0          0
##   [0.8,0.85)          0          0          0          0          0
##   [0.85,0.9)          0          0          0          0          0
##   [0.9,0.95)          1          1          0          0          0
##   [0.95,1]            0          0          0          0          1
##             y
## x            [0.45,0.5) [0.4,0.45) [0.35,0.4) [0.3,0.35) [0.25,0.3)
##   [0,0.05)            0          0          0          0          0
##   [0.05,0.1)          0          0          0          0          0
##   [0.1,0.15)          0          0          0          1          0
##   [0.15,0.2)          0          0          0          0          0
##   [0.2,0.25)          0          0          0          0          0
##   [0.25,0.3)          0          1          0          0          0
##   [0.3,0.35)          0          0          0          0          0
##   [0.35,0.4)          0          1          0          0          0
##   [0.4,0.45)          0          1          0          0          0
##   [0.45,0.5)          0          0          0          0          0
##   [0.5,0.55)          0          0          1          0          0
##   [0.55,0.6)          0          0          0          0          0
##   [0.6,0.65)          0          0          0          0          0
##   [0.65,0.7)          0          0          0          0          0
##   [0.7,0.75)          0          0          0          0          0
##   [0.75,0.8)          0          0          0          0          0
##   [0.8,0.85)          0          1          0          0          0
##   [0.85,0.9)          0          0          0          0          0
##   [0.9,0.95)          0          0          0          0          0
##   [0.95,1]            0          0          0          0          0
##             y
## x            [0.2,0.25) [0.15,0.2) [0.1,0.15) [0.05,0.1) [0,0.05)
##   [0,0.05)            1          0          0          0        0
##   [0.05,0.1)          0          0          0          0        0
##   [0.1,0.15)          0          0          0          0        0
##   [0.15,0.2)          0          0          0          1        1
##   [0.2,0.25)          1          1          0          0        0
##   [0.25,0.3)          1          0          0          0        0
##   [0.3,0.35)          0          0          0          0        0
##   [0.35,0.4)          0          0          0          0        0
##   [0.4,0.45)          0          1          0          1        1
##   [0.45,0.5)          0          0          0          0        0
##   [0.5,0.55)          0          1          0          0        1
##   [0.55,0.6)          0          0          0          0        0
##   [0.6,0.65)          0          0          0          0        0
##   [0.65,0.7)          0          0          0          0        1
##   [0.7,0.75)          0          0          0          0        0
##   [0.75,0.8)          0          0          0          0        0
##   [0.8,0.85)          0          0          0          0        0
##   [0.85,0.9)          0          0          1          0        0
##   [0.9,0.95)          0          0          0          0        0
##   [0.95,1]            0          0          1          0        0
vc = data.frame(q)
colores = c(ifelse(vc$Freq==0,'darkred','darkblue'))
plot(t(q),col=colores,cex = 0.8,
     main = 'Discretizacion de conteo de cuadrantes')

plot(t(q2),col=colores,cex = 0.8,
     main = 'Discretizacion de conteo de cuadrantes')

###
set.seed(1964)
x6 = rnorm(50)
y6 = runif(50)
plot(x6,y6)

dxy2 = ppp(x6,y6)
## Warning: 30 points were rejected as lying outside the specified window
(q = t(quadratcount(dataxy, nx = 10, ny = 10)))
##            y
## x           [0.9,1] [0.8,0.9) [0.7,0.8) [0.6,0.7) [0.5,0.6) [0.4,0.5)
##   [0,0.1)         0         0         0         0         0         0
##   [0.1,0.2)       0         0         2         1         1         0
##   [0.2,0.3)       1         1         0         0         0         1
##   [0.3,0.4)       0         0         0         1         0         1
##   [0.4,0.5)       0         0         0         0         0         1
##   [0.5,0.6)       1         0         2         0         1         0
##   [0.6,0.7)       0         0         0         0         1         0
##   [0.7,0.8)       0         1         0         1         0         0
##   [0.8,0.9)       0         0         0         0         0         1
##   [0.9,1]         0         2         2         1         1         0
##            y
## x           [0.3,0.4) [0.2,0.3) [0.1,0.2) [0,0.1)
##   [0,0.1)           0         1         0       0
##   [0.1,0.2)         1         0         0       2
##   [0.2,0.3)         0         2         1       0
##   [0.3,0.4)         0         0         0       0
##   [0.4,0.5)         0         0         1       2
##   [0.5,0.6)         1         0         1       1
##   [0.6,0.7)         0         0         0       1
##   [0.7,0.8)         0         0         0       0
##   [0.8,0.9)         0         0         1       0
##   [0.9,1]           0         0         1       0
vc = data.frame(q)
colores = c(ifelse(vc$Freq==0,'darkred',ifelse(vc$Freq>1,'darkgreen','darkblue')))
plot(t(q),col=colores,cex = 1)

cua = 1
set.seed(1964)
x6 = rnorm(50)
y6 = runif(50)
dxy2 = ppp(x6,y6)
## Warning: 30 points were rejected as lying outside the specified window
(q = t(quadratcount(dxy2, nx = cua, ny = cua)))
##       A
## [1,] 20
vc = data.frame(q)

while (sum(vc$Freq>1)!=0) {
  q = t(quadratcount(dxy2, nx = cua, ny = cua))
  vc = data.frame(q)
  colores = c(ifelse(vc$Freq==0,'darkred',
                     ifelse(vc$Freq>1,'darkgreen','darkblue')))
  plot(t(q),col=colores,cex = 1, main = paste('tamano: ',cua-1))
  print(cua)
  cua = cua + 1
}

## [1] 1

## [1] 2

## [1] 3

## [1] 4

## [1] 5

## [1] 6

## [1] 7

## [1] 8

## [1] 9

## [1] 10

## [1] 11

## [1] 12

## [1] 13

## [1] 14

## [1] 15
cua-1
## [1] 15
####
m.x = clickppp(10)

plot(m.x)