Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
$geo1.on('add:path', function (p) {
const match = checkPathMatches(p, [
new Segment(model.b, model.d),
new Segment(model.a, model.c)
]);
if (match >= 0) {
p.$el.setAttr('target', 'diagonal');
if (match === 1) model.set('o', true);
$step.score('diagonal');
$geo1.setActiveTool('move');
} else {
$step.addHint('not-a-diagonal-1');
}
});
}
export function edgeToSegment(e: Edge) {
const p1 = new Point(e.vertices[0].posn.x, e.vertices[0].posn.y);
const p2 = new Point(e.vertices[1].posn.x, e.vertices[1].posn.y);
return new Segment(p1, p2);
}
checkForIntersects() {
if (!this.options.intersect || this.paths.length <= 1) return;
let path1 = last(this.paths);
let points1 = path1.points as Point[];
let line1 = new Segment(points1[points1.length - 2],
points1[points1.length - 1]);
for (let i = 0; i < this.paths.length - 1; ++i) {
let path2 = this.paths[i];
let points2 = path2.points as Point[];
for (let j = 1; j < points2.length - 2; ++j) {
let line2 = new Segment(points2[j], points2[j + 1]);
let t = Segment.intersect(line1, line2);
if (t) {
this.trigger('intersect', {point: t, paths: [path1, path2]});
return;
}
}
}
}
}
checkForIntersects() {
if (!this.options.intersect || this.paths.length <= 1) return;
let path1 = last(this.paths);
let points1 = path1.points as Point[];
let line1 = new Segment(points1[points1.length - 2],
points1[points1.length - 1]);
for (let i = 0; i < this.paths.length - 1; ++i) {
let path2 = this.paths[i];
let points2 = path2.points as Point[];
for (let j = 1; j < points2.length - 2; ++j) {
let line2 = new Segment(points2[j], points2[j + 1]);
let t = Segment.intersect(line1, line2);
if (t) {
this.trigger('intersect', {point: t, paths: [path1, path2]});
return;
}
}
}
}
}
draw($canvas: CanvasView, res = 2) {
const tail = this.points.map(t => new Point(t[0] * res, t[1] * res));
for (let i = 1; i < tail.length - 2; ++i) {
const line = new Segment(tail[i], tail[i + 1]);
const opacity = 0.5 * (1 - i / tail.length);
$canvas.draw(line,
{stroke: this.color, strokeWidth: this.width, opacity});
}
}
$geo1.on('add:path', function (p) {
const match = checkPathMatches(p, [
new Segment(model.b, model.d),
new Segment(model.a, model.c)
]);
if (match >= 0) {
p.$el.setAttr('target', 'diagonal');
if (match === 1) model.set('o', true);
$step.score('diagonal');
$geo1.setActiveTool('move');
} else {
$step.addHint('not-a-diagonal-1');
}
});
}
function addEdge(u: number, v: number) {
if (u === v) return;
let edge = new Segment(points[u], points[v]);
for (let i = 0; i < edges.length; ++i) {
const e2 = new Segment(points[edges[i][0]], points[edges[i][1]]);
if (edge.equals(e2) || Segment.intersect(edge, e2)) return;
}
edges.push([u, v]);
}