Triangular Grid 1

My first turtle!

Just a triangular grid with some segments removed.

Log in to post a comment.

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }

    inRectangle(minX, maxX, minY, maxY) {
        if (this.x < minX || this.x > maxX) { return false; }
        if (this.y < minY || this.y > maxY) { return false; }

        return true;
    }

    toArray() {
        return [this.x, this.y];
    }
}

class LineSegment {
    constructor(p1, p2) {
        this.p1 = p1;
        this.p2 = p2;
    }

    intersectsRectangle(minX, maxX, minY, maxY) {
        return this.p1.inRectangle(minX, maxX, minY, maxY) || this.p2.inRectangle(minX, maxX, minY, maxY);
    }

    containedInRectangle(minX, maxX, minY, maxY) {
        return this.p1.inRectangle(minX, maxX, minY, maxY) && this.p2.inRectangle(minX, maxX, minY, maxY);
    }

    draw() {
        turtle.goto(this.p1.toArray());
        turtle.pendown();
        turtle.goto(this.p2.toArray());
        turtle.penup();
    }
}

class Vector2d {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }

    scale(n) {
        return new Vector2d(this.x * n, this.y * n);
    }

    add(other) {
        return new Vector2d(this.x + other.x, this.y + other.y);
    }

    subtract(other) {
        return new Vector2d(this.x - other.x, this.y - other.y);
    }
}


function randomIndex(list) {
    return Math.floor(list.length * Math.random());
}


// Parameters
Canvas.setpenopacity(1);
const edgeLength = 3;
const fractionToRemove = 0.8;

const turtle = new Turtle();
turtle.penup();

const sqrt3 = Math.sqrt(3);
const canvasSize = 100;
const horizontalVector = new Vector2d(edgeLength, 0);
const diagonalVector = new Vector2d(edgeLength / 2, edgeLength * sqrt3 / 2);


let minIndex = Math.floor(-canvasSize * sqrt3 / edgeLength);
let maxIndex = Math.ceil(canvasSize * sqrt3 / edgeLength)

// Create all of the line segments
let lineSegments = [];
for (var i = minIndex; i < maxIndex; i++) {
    for (var j = minIndex; j < maxIndex; j++) {
        let start = horizontalVector.scale(i).add(diagonalVector.scale(j));
        let end1 = start.add(horizontalVector);
        let end2 = start.add(diagonalVector);
        let end3 = start.add(diagonalVector).subtract(horizontalVector);

        let segments = [
            new LineSegment(new Point(start.x, start.y), new Point(end1.x, end1.y)),
            new LineSegment(new Point(start.x, start.y), new Point(end2.x, end2.y)),
            new LineSegment(new Point(start.x, start.y), new Point(end3.x, end3.y))
        ];

        for (var k = 0; k < segments.length; k++) {
            let segment = segments[k];
            if (segment.intersectsRectangle(-canvasSize, canvasSize, -canvasSize, canvasSize)) {
                lineSegments.push(segment);
            }
        }
    }
}

// Remove some of the line segments
let numSegmentsToRemove = Math.floor(lineSegments.length * fractionToRemove);
for (var i = 0; i < numSegmentsToRemove; i++) {
    lineSegments.splice(randomIndex(lineSegments), 1);
}


// Draw the remaining segments
function walk(i) {
    for (var i = 0; i < lineSegments.length; i++) {
        lineSegments[i].draw();
    }

    return false;
}