# point in hexagon

Printable View

• 10-25-2012, 05:56 PM
DoyleBrunson
point in hexagon
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);
}
}
• 10-25-2012, 06:27 PM
KevinWorkman
Re: point in hexagon
Have you run through this with a debugger, or at least added some print statements, to figure out what's going on?
• 10-25-2012, 06:41 PM
DarrylBurke
Re: point in hexagon
• 10-25-2012, 06:51 PM
DoyleBrunson
Re: point in hexagon
Everything works correct. That's why I am confused. I am also sure about mathematical equations. Even if I run the code for the left-up corner it results in the same pattern. Problem is that some of points are not corresponding to the if conditions but they still do appear. The end result when I run the program is a very dense hexagon and some points outside. Why do the outside points remain?
• 10-25-2012, 07:00 PM
KevinWorkman
Re: point in hexagon
Quote:

Originally Posted by DoyleBrunson
Everything works correct. That's why I am confused. I am also sure about mathematical equations. Even if I run the code for the left-up corner it results in the same pattern. Problem is that some of points are not corresponding to the if conditions but they still do appear. The end result when I run the program is a very dense hexagon and some points outside. Why do the outside points remain?

Have you run through this with a debugger, or at least added some print statements, to figure out what's going on?

When does the execution of the program differ from what you expect? How does the first point drawn outside the hexagon get there? These are the questions you should be asking yourself, and the answer lies in debugging your code.
• 10-25-2012, 07:15 PM
DoyleBrunson
Re: point in hexagon
ok. Thank you.