2

私はある種の「ランダムジェネレーター」を実行しようとしました。隣接するポイントがいくつかあり、それらの明るさに応じて、多かれ少なかれ選択される可能性があります。

ポイントは、座標と値を持つオブジェクトでxあり、その明るさを保存します。yb

私のアプローチは、aと値(両方とも0と1の間の何か)を持つpオブジェクトを(p可能性のために)設定することでした-との値の違いはそれらの明るさに依存します。startendstartend

次に、乱数を生成し、すべてのポイントをループして、乱数がプロパティの間にあるかどうかを確認しstartますend。1ポイントstartの値は前のポイントのend値であるため、1つのポイントのみを選択する必要がありますが、代わりにランダムな量のポイントが選択されます。

したがって、ランダムな値とポイントstartおよびendプロパティをログに記録しました。次のコードを除いて、すべてが機能したように見えます

for (i = 0; i < numAdjacentPoints; i++) {
    // set some shorthands
    curr_point = adjacentPoints[i];
    // if there is no previous point, we start with 0
    prev_point = ((i === 0) ? {p: {end: 0}} : adjacentPoints[i-1]);
    // initiate a probability object
    curr_point.p = {};
    // set a start value (the start value is the previous point's end value)
    curr_point.p.start = prev_point.p.end;
    // set an end value (the start value + the point's brightness' share of totalBrightness)
    // -> points with higher darkness (255-b) are more have a higher share -> higher probability to get grown on
    curr_point.p.end   = curr_point.p.start + (255 - curr_point.b) / totalBrightness;
    // if the random value is between the current point's p values, it gets grown on
    if (curr_point.p.start < rand < curr_point.p.end) {
        // add the new point to the path array
        path[path.length] = curr_point;
        // set the point's brightness to white -> it won't come into range any more
        curr_point.b = 255;
        console.log("  we've got a winner! new point is at "+curr_point.x+":"+curr_point.y);
        console.log("  "+curr_point.p.start.toFixed(2)+" < "+rand.toFixed(2)+" < "+curr_point.p.end.toFixed(2));
    }
};

これを出力します:

we've got a winner! new point is at 300:132 mycelium.php:269
0.56 < 0.53 < 0.67 mycelium.php:270
we've got a winner! new point is at 301:130 mycelium.php:269
0.67 < 0.53 < 0.78 mycelium.php:270
we've got a winner! new point is at 301:131 mycelium.php:269
0.78 < 0.53 < 0.89 mycelium.php:270
we've got a winner! new point is at 301:132 mycelium.php:269
0.89 < 0.53 < 1.00

-> WTF?0.56 < 0.53 < 0.67??

4

2 に答える 2

5

あなたがしたい

if (curr_point.p.start < rand  && rand < curr_point.p.end) {

それ以外の

if (curr_point.p.start < rand < curr_point.p.end) {

数値をブール値である比較の結果と比較していました。あなたのコードは同等でした

if ((curr_point.p.start < rand) < curr_point.p.end) {

このような操作で使用すると、ブール値は 1 または 0 に変換されるため、テストしていました

if (0 < 0.67) {

JavaScript での演算子の優先順位

于 2012-09-24T13:48:08.250 に答える
1

これを行うことはできません:

if (curr_point.p.start < rand < curr_point.p.end)

curr_point.p.start < randは andとして評価されるためboolean、不要なものが得られます。 if (boolean < curr_point.p.end)

正しい条件は次のようになります。

if (curr_point.p.start < rand  && rand < curr_point.p.end)

この場合、2 つのブール値 if (boolean1 && boolean2) があるため、正しく比較できます。

于 2012-09-24T14:01:22.553 に答える