Results 1 to 6 of 6
  1. #1
    DoyleBrunson is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default 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);
    }
    }
    Last edited by DoyleBrunson; 10-25-2012 at 06:02 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default 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?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: point in hexagon

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DoyleBrunson is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default 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?

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: point in hexagon

    Quote Originally Posted by DoyleBrunson View Post
    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.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    DoyleBrunson is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

Similar Threads

  1. What's the point of try {} ?
    By Josep_16 in forum Java Applets
    Replies: 2
    Last Post: 08-05-2011, 01:47 PM
  2. Access Point
    By ja107 in forum Networking
    Replies: 0
    Last Post: 03-05-2010, 05:59 PM
  3. Drawing a point
    By chappa in forum AWT / Swing
    Replies: 3
    Last Post: 12-13-2009, 08:24 PM
  4. Can someone point me to an example?
    By smhjr in forum New To Java
    Replies: 2
    Last Post: 08-27-2009, 11:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •