Skip to content

Commit

Permalink
Merge pull request #754 from hackclub/revert-717-assertions
Browse files Browse the repository at this point in the history
Revert "Significantly improve error messages"
  • Loading branch information
maxwofford authored Jul 29, 2024
2 parents da61aa5 + 42a544f commit bf30801
Show file tree
Hide file tree
Showing 21 changed files with 23 additions and 348 deletions.
33 changes: 1 addition & 32 deletions src/drawingToolkit/Turtle.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { translatePt, rotatePt } from "./affineTransformations.js";
import { assertArgs } from './assert.js'

const copy = obj => JSON.parse(JSON.stringify(obj))

export class Turtle {

constructor() {
assertArgs(arguments, [], 'new bt.Turtle')

this.drawing = true
this.position = [ 0, 0 ]
this.angle = 0
Expand All @@ -18,24 +15,18 @@ export class Turtle {
}

up() {
assertArgs(arguments, [], 'turtle.up')

if (!this.drawing) return this
this.drawing = false
this.path.push([[...this.position]])
return this
}

down() {
assertArgs(arguments, [], 'turtle.down')

this.drawing = true
return this
}

goTo([x, y]) {
assertArgs(arguments, ['point'], 'turtle.goTo')

const lastPath = this.path.at(-1)
if (this.drawing) {
const [lastX, lastY] = this.position
Expand All @@ -51,9 +42,7 @@ export class Turtle {
return this
}

step([dx, dy]) {
assertArgs(arguments, ['point'], 'turtle.step')

step([dx, dy]) { // document this
const [x, y] = this.position;

this.goTo([
Expand All @@ -65,8 +54,6 @@ export class Turtle {
}

jump(pt) {
assertArgs(arguments, ['point'], 'turtle.jump')

const [x, y] = pt;
const lastPath = this.path.at(-1);
if (lastPath.length === 1) {
Expand All @@ -82,8 +69,6 @@ export class Turtle {
}

forward(distance) {
assertArgs(arguments, ['number'], 'turtle.forward')

const last = this.position
const a = (this.angle / 180) * Math.PI
const x = last[0] + distance * Math.cos(a)
Expand All @@ -95,8 +80,6 @@ export class Turtle {
}

arc(angle, radius) {
assertArgs(arguments, ['number', 'number'], 'turtle.arc')

if (angle === 0 || radius === 0) return this;

const n = 64;
Expand Down Expand Up @@ -135,8 +118,6 @@ export class Turtle {

// setHeading?
setAngle(theta) {
assertArgs(arguments, ['number'], 'turtle.setAngle')

this.angle = theta

return this
Expand All @@ -147,33 +128,25 @@ export class Turtle {
// }

right(theta) {
assertArgs(arguments, ['number'], 'turtle.right')

this.angle -= theta

return this
}

left(theta) {
assertArgs(arguments, ['number'], 'turtle.left')

this.angle += theta

return this
}

lines() { // could be called polylines
assertArgs(arguments, [], 'turtle.lines')

const pls = copy(this.path);

return pls.filter(pl => pl.length > 1);
}


copy() {
assertArgs(arguments, [], 'turtle.copy')

const t = new Turtle()

t.path = copy(this.path)
Expand All @@ -185,16 +158,12 @@ export class Turtle {
}

applyToPath(fn) {
assertArgs(arguments, ['function'], 'turtle.applyToPath')

fn(this.path);
return this;
}

// undoced
apply(fn) {
assertArgs(arguments, ['function'], 'turtle.apply')

fn(this);
return this;
}
Expand Down
13 changes: 3 additions & 10 deletions src/drawingToolkit/affineTransformations.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { assertArgs } from "./assert.js";
import { bounds } from "./bounds.js";

export function translate(polylines, to, origin = [0, 0]) {
assertArgs(arguments, ['polylines', 'point', 'point?'], 'bt.translate')

export const translate = (polylines, to, origin = [0, 0]) => {
polylines.flat().forEach(pt => {
const [x, y] = translatePt(pt, to, origin);
pt[0] = x;
Expand All @@ -13,9 +10,7 @@ export function translate(polylines, to, origin = [0, 0]) {
return polylines;
}

export function rotate(polylines, angle, origin) {
assertArgs(arguments, ['polylines', 'number', 'point?'], 'bt.rotate')

export const rotate = (polylines, angle, origin) => {
if (!origin) origin = bounds(polylines).cc

polylines.flat().forEach(pt => {
Expand All @@ -27,9 +22,7 @@ export function rotate(polylines, angle, origin) {
return polylines
}

export function scale(polylines, factor, origin) {
assertArgs(arguments, ['polylines', ['number', 'point'], 'point?'], 'bt.scale')

export const scale = (polylines, factor, origin) => {
if (!origin) origin = bounds(polylines).cc

polylines.flat().forEach(pt => {
Expand Down
113 changes: 0 additions & 113 deletions src/drawingToolkit/assert.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/drawingToolkit/bounds.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { assertArgs } from './assert'

export function bounds(polylines) {
assertArgs(arguments, ['polylines'], 'bt.bounds');

const { xMin, xMax, yMin, yMax } = extrema(polylines.flat());

const width = xMax - xMin;
Expand Down
4 changes: 0 additions & 4 deletions src/drawingToolkit/catmullRom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { assertArgs } from './assert'

export function catmullRom(controlPoints, segments = 100) {
assertArgs(arguments, ['polyline', 'number?'], 'bt.catmullRom');

const isClosed = (points) => {
const start = points[0];
const end = points[points.length - 1];
Expand Down
9 changes: 2 additions & 7 deletions src/drawingToolkit/cutCover.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { bounds } from "./bounds.js";
import { pointInPolylines } from "./pointInPolylines.js"
import { mergePolylines } from "./mergePolylines.js";
import { assertArgs } from "./assert.js";

export function cut(polylines0, polylines1, assumeClosed = true) {
assertArgs(arguments, ['polylines', 'polylines', 'boolean?'], 'bt.cut');

export const cut = (polylines0, polylines1, assumeClosed = true) => {
if (assumeClosed) polylines1.forEach(poly => {
const [x, y] = poly.at(0);
poly.push([x, y]);
Expand All @@ -28,9 +25,7 @@ export function cut(polylines0, polylines1, assumeClosed = true) {
return polylines0;
};

export function cover(polylines0, polylines1, assumeClosed = true) {
assertArgs(arguments, ['polylines', 'polylines', 'boolean?'], 'bt.cover');

export const cover = (polylines0, polylines1, assumeClosed = true) => {
if (assumeClosed) polylines1.forEach(poly => {
const [x, y] = poly.at(0);
poly.push([x, y]);
Expand Down
4 changes: 0 additions & 4 deletions src/drawingToolkit/getAngleAtT.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { assertArgs } from './assert'

export function getAngleAtT(polylines, t) {
assertArgs(arguments, ['polylines', 'number'], 'bt.getAngle')

// Calculate the total length of all polylines
let totalLength = 0
let lengths = polylines.map(polyline => {
Expand Down
4 changes: 0 additions & 4 deletions src/drawingToolkit/getNormalAtT.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { assertArgs } from './assert'

export function getNormalAtT(polylines, t) {
assertArgs(arguments, ['polylines', 'number'], 'bt.getNormal')

// Calculate the total length of all polylines
let totalLength = 0
let lengths = polylines.map(polyline => {
Expand Down
4 changes: 0 additions & 4 deletions src/drawingToolkit/getPointAtT.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { assertArgs } from './assert'

export function getPointAtT(polylines, t) {
assertArgs(arguments, ['polylines', 'number'], 'bt.getPoint')

t = Math.max(t, 0)
t = Math.min(t, 1)

Expand Down
4 changes: 0 additions & 4 deletions src/drawingToolkit/iteratePolylines.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { assertArgs } from './assert'

export function iteratePolylines(polylines, fn) {
assertArgs(arguments, ['polylines', 'function'], 'bt.iteratePoints')

const toRemove = new Set()
const toBreak = new Set()

Expand Down
4 changes: 0 additions & 4 deletions src/drawingToolkit/mergePolylines.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { assertArgs } from './assert'

export function mergePolylines(polylines) {
assertArgs(arguments, ['polylines'], 'bt.merge');

let merged = true;

while (merged) {
Expand Down
2 changes: 0 additions & 2 deletions src/drawingToolkit/noise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertArgs } from './assert.js'
import { rand } from './rand.js'

let PERLIN_YWRAPB = 4
Expand All @@ -14,7 +13,6 @@ let scaled_cosine = function (i) {
let perlin// will be initialized lazily by noise() or noiseSeed()

export function noise( vector, options = {}) {
assertArgs(arguments, ['point', 'any?'], 'bt.noise')
if (typeof vector === 'number') vector = [vector, 0, 0]
let [x, y, z] = vector
y = y || 0
Expand Down
3 changes: 0 additions & 3 deletions src/drawingToolkit/offset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertArgs } from './assert.js'
import { ClipperLib } from "./clipper_unminified.js";

const overlap = (p0, p1) => 0.00000001 > Math.abs(p0[0] - p1[0]) + Math.abs(p0[1] - p1[1]);
Expand All @@ -11,8 +10,6 @@ const isClosed = shape => {
}

export function offset(polylines, delta, ops = {}) {
assertArgs(arguments, ['polylines', 'number', 'any?'], 'bt.offset')

const ogPolylines = polylines;
if (typeof polylines.at(0)?.at(0) === "number") polylines = [polylines];

Expand Down
Loading

0 comments on commit bf30801

Please sign in to comment.