Skip to content

randomLineString does not take max_rotation option into account #2893

@claustres

Description

@claustres

It seems to be related to the fact that the previous angle variable is actually a local variable of the inner loop over the vertices so that it is incorrectly reset at each vertex. Here is a suggested modification of the code, which also appears to be more simple:

for (var i = 0; i < count; i++) {
    var startingPoint = turf.randomPosition(bbox);
    var vertices = [startingPoint];
    var priorAngle = Math.random() * 2 * Math.PI;
    for (var j = 0; j < num_vertices - 1; j++) {
        var angle = priorAngle + (Math.random() - 0.5) * max_rotation * 2;
        priorAngle = angle;
        var distance = Math.random() * max_length;
        vertices.push([
            vertices[j][0] + distance * Math.cos(angle),
            vertices[j][1] + distance * Math.sin(angle),
        ]);
    }
    features.push(turf.lineString(vertices));
}

I would be pleased to try to create a PR if you think this fix is correct, althought it might take some time to me for a first contribution to study how to write a relevent test, update the build, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions