I would like to give two stars to Thomas @TfGuy44 … for taking time to recognize @solub efforts.
Numbers like 5, 10000 and 10001 are an example of magic numbers. This is why it is better to assign them constants so they can clarify the functionality in the code. Maybe at the end of this exercise you could suggest a name to those values.
There are two key points for this code:
- Check this statement
Unfortunately this is not clear as tangents from lines is really not defined in geometry based on my limited experience. Looking at the code during crack
creations, angles are changed by +90 or -90 degrees (plus some angle noise):
if (random(100)<50) {
a-=90+int(random(-2,2.1));
} else {
a+=90+int(random(-2,2.1));
}
So I believe the line creating happens perpendicular to existing lines. This makes more sense.
- The other point to consider is in this line of the code that you presented:
cgrid[cy*dimx+cx]=int(t);
So this is what happens. All the points in the grid are marked with an undefined value of 10001. Initially 3 cracks are created. Also, as you mentioned, 16 random coordinates in the grid are assigned an angle. When the cracks start moving, one point is anexed to the line follwing the same angle (plus/minus some noise). That point is also assinged an angle, which derives from the parent (pre-decesor) point. When a point hits the wall (I think) or a moving point finds a position with an angle, then something happens. The algorithm enters into crack creation mode (a while loop) and randomly searches in the grid for a position that is not marked as undefined, reads the angle at that position and then randomly assigns set a value of + or - 90 (plus some noise). And then the crack starts moving from there.
Notice that the while loop has a mechanism to dictate when to stop drawing cracks. When searching for a new undefined stop, if it cannot find one, then it will not generate the crack. The algorithm is permitted to sample the grid 1000 times before giving up creating a crack. You can imagine that when the grid becomes so pupulated, 1000 times will be enough to stop generating cracks.
Anyways, this is the story of this algorithm. If you want to explore another concept, instead of the algorithm advancing a single point, make the algorithm draw 10 points per step. Or make the algorithm generate cracks only at certain defined “regularly spaced” grid positions. What do you think the outcome would be?
Two more things to talk about:
Why 10001? Well, the algorithm adds and subtrracts 90 during every crack creation, right? A single point in the grid could experience this operation multiple times. Statistically speaking, any non undefined position could vary by 90 “jumps” but because you can have a positive or negative angle jumps, your angle will sort of stay closer to zero. However, if a point is lucky enough and based on the random nature, a point could be incremented by positive 90 multiple times before it is drven back by negative 90s. In other words, an angle stored in a grid is not restricted to values between 0 and 360 but it could have values between MIN_INT and MAX_INT. The likelihood this angle reaches thes boundaries of integer values is low. The changes that an angle reaches a value of 10001 or larger? They are low as well but still probable. In the case an angle reaches or surpases this value, then it is not big deal. The algorithm will consider this point as undefined again and it will participate in a crack creation, if it is randomly selected.
Finally, the other thing we need to talk about is about the value of 5:
However, I believe I already answer this question above.
Kf