create a circle with N points

I try to fill a grid with points and only keep the points inside a imaginary circle. What i like is that i can call the method and request an amount of points. However my calculations are way off. When i want to 1000 points for example i get 2289. It doesn't have to be really precise but more then 228% of is a bit much.

I know i could do it recursive but i prefer that it's just more accurate to start with.

Hope someone can help.

ArrayList<PVector> colorDetectionVecs = new ArrayList<PVector>();


void setup() {
  size(500, 500);
  createColorDetectionPoints(1000);
}

void draw() {
  noStroke();
  for(PVector v :   colorDetectionVecs) {
   ellipse(v.x*500, v.y*500, 1, 1); 
  }
}




void createColorDetectionPoints(int nTargetPoints) {

    colorDetectionVecs.clear();

    // xSteps and ySteps needs to be calculated
    // the ratio between a rect and ellipse is
    // 0.7853982

    int xSteps = (int)sqrt(nTargetPoints);
    xSteps *= 1.7853982; // make it bigger in proportion to the ratio
    int ySteps = xSteps;

    float centerX = (float)xSteps/2;
    float centerY = (float)ySteps/2;

    float fX, fY, d;

    float maxDistSquared = 0.5*0.5;

    for (int y = 0; y < ySteps; y++) {
        for (int x = 0; x < xSteps; x++) {
            fX = x;
            fY = y;
            // normalize
            fX /= xSteps-1;
            fY /= ySteps-1;

            d = distSquared(fX, fY, 0.5, 0.5);

            if(d <= maxDistSquared) {
                colorDetectionVecs.add(new PVector(fX, fY));
            }
        }
    }


    println("colorDetectionVecs: "+colorDetectionVecs.size());


}

float distSquared(float x1, float y1, float x2, float y2) {
    return sq(x2-x1) + sq(y2-y1);
  }

Answers

  • edited October 2013

    Maths problem change x and y steps to

    int xSteps = (int)sqrt(nTargetPoints * 1.2838);
    int ySteps = xSteps;
    
  • Answer ✓

    Hi! You multiplied by 1.78, but maybe you wanted to divide by 0.78? The value you get is much closer (1129). Then, if you switch to floats instead of ints, and use xSteps /= sqrt(0.7853982); instead of xSteps /= 0.7853982; you get even closer. You get 81 when requesting 100, which means it's doing 9x9 instead of 10x10.

    I came up with this simpler more precise approach, which gives you 999 when asking for 1000:

    void createColorDetectionPoints(int nTargetPoints) {
      colorDetectionVecs.clear();
    
      float step = 1 / sqrt(nTargetPoints);
      step *= sqrt(0.7853982);
    
      for (float y = 0; y < 1; y+=step) {
        for (float x = 0; x < 1; x+=step) {
          if (dist(x, y, 0.5, 0.5) <= 0.5) {
            colorDetectionVecs.add(new PVector(x, y));
          }
        }
      }
    
      println("colorDetectionVecs: "+colorDetectionVecs.size());
    }
    
  • THANKS! I get 999 when requesting 1000 so that's really good :)

  • quarks. I didn't see your post untill now. How did you come by the 1.2838?

Sign In or Register to comment.