Hello. In the code below random points should be placed within hexagon. I think there is a bug related to the while loop so that some of the points are not checked. When I run the code, density of points in the hexagon are nearly 10 times more than the points outdise. Where is the bug? Here is the code:

import acm.program.*;

import acm.graphics.*;

import acm.util.*;

import jаvа.awt.*;

public class RHex extends GraphicsProgram{

private RandomGenerator rgen=RandomGenerator.getInstance();

private static int N=20000;

private static int S=200;

private int height=(int)( Math.sqrt(3)*S);

private int width=(int) 2*S;

private double C=Math.sqrt(3);

private double x;

private double y;

private static double r=0.1;

public void run(){

GPolygon hex=new GPolygon();

hex.addVertex(-S, 0);

int angle=60;

for(int i=0;i<6;i++){

hex.addPolarEdge(S,angle);

angle-=60;

}

hex.move(S, Math.sqrt(3)*S/2);

add(hex);

while(N>0){

addRandomPoint();

N--;

}

}

public GOval addRandomPoint(){

x=rgen.nextDouble(0,2*S);

y=rgen.nextDouble(0,Math.sqrt(3)*S);

double y1=-C*x+S*C/2;

double y2=C*x+S*C/2;

double y3=C*x-S*C-S*C/2;

double y4=-C*x+5*S*C/2;

while(true){

if( (x>S/2&&x<3*S/2&&y>0&&y<C*S)){

break;

}

else if(x>0&&x<S/2&&y>0&&y<C*S/2&&y1<=y){

break;

}

else if(x>0&&x<S/2&&y>S*C/2&&y<C*S&&y2>=y){

break;

}

else if(x>3*S/2&&x<2*S&&y>0&&y<C*S/2&&y3<=y){

break;

}

else if(x>3*S/2&&x<2*S&&y>S*C/2&&y<C*S&&y4>=y){

break;

}

x=rgen.nextDouble(0,2*S);

y=rgen.nextDouble(0,Math.sqrt(3)*S);

}

GOval p=new GOval(x,y,r,r);

add(p);

return p;

}

public void init(){

setSize(width,height);

}

}