Fix divide by zero on triangle shapes

This commit is contained in:
Mitchell McCaffrey 2020-06-24 09:27:36 +10:00
parent 9a4d047cd5
commit 96a100c02a
2 changed files with 4 additions and 1 deletions

View File

@ -150,7 +150,7 @@ export function getUpdatedShapeData(type, data, brushPosition, gridSize) {
const length = Vector2.length(scaled); const length = Vector2.length(scaled);
const direction = Vector2.normalize(scaled); const direction = Vector2.normalize(scaled);
// Get the angle for a triangle who's width is the same as it's length // Get the angle for a triangle who's width is the same as it's length
const angle = Math.atan(length / 2 / length); const angle = Math.atan(length / 2 / (length === 0 ? 1 : length));
const sideLength = length / Math.cos(angle); const sideLength = length / Math.cos(angle);
const leftDir = Vector2.rotateDirection(direction, toDegrees(angle)); const leftDir = Vector2.rotateDirection(direction, toDegrees(angle));

View File

@ -10,6 +10,9 @@ export function length(p) {
export function normalize(p) { export function normalize(p) {
const l = length(p); const l = length(p);
if (l === 0) {
return { x: 0, y: 0 };
}
return divide(p, l); return divide(p, l);
} }