计算xSteps和ySteps以获得圆圈中的N个点数

时间:2013-10-27 22:14:04

标签: c++ math openframeworks

我尝试用点填充网格,只将点保持在一个假想的圆圈内。

example

首先我这样做: createColorDetectionPoints(int xSteps, int ySteps)

但对我而言,设定目标时要容易得多:

void ofxDTangibleFinder::createColorDetectionPoints(int nTargetPoints)

目标不必太精确。但是当我想要1000分时,例如我得到2289分。

我认为我的逻辑错了,但我无法理解。 我们的想法是获得适量的xSteps和ySteps。

有人可以帮忙吗?

void ofxDTangibleFinder::createColorDetectionPoints(int nTargetPoints) {

    colorDetectionVecs.clear();

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

    int xSteps = 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 = ofDistSquared(fX, fY, 0.5, 0.5);

            if(d <= maxDistSquared) {
                colorDetectionVecs.push_back(ofVec2f(fX, fY));
            }
        }
    }

//    for(int i = 0; i < colorDetectionVecs.size(); i++) {
//        printf("ellipse(%f, %f, 1, 1);\n", colorDetectionVecs[i].x*100, colorDetectionVecs[i].y*100);
//    }


    printf("colorDetectionVecs: %lu\n", colorDetectionVecs.size());


}

0 个答案:

没有答案