Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Line#getSignedDistance and add tests to avoid regression #1880

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/basic/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ var Line = Base.extend(/** @lends Line# */{
return vx === 0 ? (vy > 0 ? x - px : px - x)
: vy === 0 ? (vx < 0 ? y - py : py - y)
: ((x - px) * vy - (y - py) * vx) / (
vy > vx
? vy * Math.sqrt(1 + (vx * vx) / (vy * vy))
: vx * Math.sqrt(1 + (vy * vy) / (vx * vx))
Math.abs(vy) > Math.abs(vx)
? Math.abs(vy) * Math.sqrt(1 + (vx * vx) / (vy * vy))
: Math.abs(vx) * Math.sqrt(1 + (vy * vy) / (vx * vx))
);
},

Expand Down
64 changes: 64 additions & 0 deletions test/tests/Line.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/

QUnit.module('Line');

test('new Line(10, 20, 30, 40)', function() {
var line = new Line(10, 20, 30, 40);
equals(line._px, 10, 'point._px');
equals(line._py, 20, 'point._py');
// _vx = p2.x - p1.x
equals(line._vx, 20, 'point._vx');
// _vy = p2.y - p1.y
equals(line._vy, 20, 'point._vy');
});

test('new Line(10, 20, 30, 40, true)', function() {
var line = new Line(10, 20, 30, 40, true);
equals(line._px, 10, 'point._px');
equals(line._py, 20, 'point._py');
equals(line._vx, 30, 'point._vx');
equals(line._vy, 40, 'point._vy');
});

test('getDistance()', function() {
equals(function() {
return new Line(10, 20, 20, 10, true).getDistance(new Point(20, 10));
}, 13.416407864998737);
equals(function() {
return new Line(10, 20, -20, -10, true).getDistance(new Point(20, 10));
}, 13.416407864998737);
equals(function() {
return new Line(10, 20, 20, 10, true).getDistance(new Point(0, 30));
}, 13.416407864998737);
equals(function() {
return new Line(10, 20, -20, -10, true).getDistance(new Point(0, 30));
}, 13.416407864998737);
});

test('getSignedDistance()', function() {
equals(function() {
return new Line(10, 20, 20, 10, true).getSignedDistance(new Point(20, 10));
}, 13.416407864998737);
equals(function() {
// Change the line direction
return new Line(10, 20, -20, -10, true).getSignedDistance(new Point(20, 10));
}, -13.416407864998737);
equals(function() {
// Change the point side
return new Line(10, 20, 20, 10, true).getSignedDistance(new Point(0, 30));
}, -13.416407864998737);
equals(function() {
// Change the line direction and the point side
return new Line(10, 20, -20, -10, true).getSignedDistance(new Point(0, 30));
}, 13.416407864998737);
});
1 change: 1 addition & 0 deletions test/tests/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

/*#*/ include('Point.js');
/*#*/ include('Line.js');
/*#*/ include('Size.js');
/*#*/ include('Rectangle.js');
/*#*/ include('Matrix.js');
Expand Down