1. Member Join Date
Oct 2012
Posts
3
Rep Power
0

## 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();
int angle=60;
for(int i=0;i<6;i++){
angle-=60;
}
hex.move(S, Math.sqrt(3)*S/2);

while(N>0){
N--;
}
}
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);
return p;
}
public void init(){
setSize(width,height);
}
}
Last edited by DoyleBrunson; 10-25-2012 at 07:02 PM.  Reply With Quote

2. ## 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?  Reply With Quote

3. ## Re: point in hexagon  Reply With Quote

4. Member Join Date
Oct 2012
Posts
3
Rep Power
0

## 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?  Reply With Quote

5. ## Re: point in hexagon 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.  Reply With Quote

6. Member Join Date
Oct 2012
Posts
3
Rep Power
0

## Re: point in hexagon

ok. Thank you.  Reply With Quote

bug, hexagon, loop, point 