monarchy = function(kings) {
  P = kings$P
  Q = kings$Q
  C = (P+Q)/2
  D = Q-P;
  a = C[2] + D[1]/D[2]*C[1]
  b = -D[1]/D[2]
  y = x%*%c(-b,1)
  cls = ifelse(y<a,"red","blue")
  plot(x,pty="s",main="Monarchy",pch='.',col=cls,xlab="",ylab="",xlim=range(x),ylim=range(x),cex=2)
  abline(a=a,b=b)
  points(P[1],P[2],pch=19)
  points(Q[1],Q[2],pch=19)
  return(y<a)
}

democracy = function(whoAreRed) {
  newP = apply(x[whoAreRed,],2,mean)
  newQ = apply(x[!whoAreRed,],2,mean)
  cls = ifelse(whoAreRed,"red","blue")  
  plot(x,pty="s",main="Democracy",pch='.',col=cls,xlab="",ylab="",xlim=range(x),ylim=range(x),cex=2)
  #abline(a=a,b=b)
  points(newP[1],newP[2],pch=19)
  points(newQ[1],newQ[2],pch=19)
  return(list(P=newP,Q=newQ))
}

n= 100
x1 = rnorm(n)
y1 = rnorm(n)

x2 = rnorm(n,mean=4)
y2 = rnorm(n,mean=4)

x = rbind(cbind(x1,y1),cbind(x2,y2))



startGame = function(centre) {
  P=x[centre[1],]
  Q=x[centre[2],]
  plot(x,pty="s",main="Start",pch='.',xlab="",ylab="",xlim=range(x),ylim=range(x),cex=2)
  points(P[1],P[2],pch=19)
  points(Q[1],Q[2],pch=19)
  return(list(P=P,Q=Q))
}

#val = startGame(sample(100,2))
#val = monarchy(val)
#val = democracy(val)

