Commit da49dd7c authored by Christian Winter's avatar Christian Winter

distanceToPoint now part of line class not utilities any more

parent 13299f35
......@@ -50,17 +50,8 @@ Utilities = new class {
p.position = GridManager.grid.getNearestPointFor(p.position);
}
// TODO make part of Line class
distancePointToLine(position, line) {
let se = line.end.position.subtractVector(line.start.position);
let sp = position.subtractVector(line.start.position);
let ep = position.subtractVector(line.end.position);
if (Vector2.dot(se, sp) <= 0)
return sp.magnitude();
if (Vector2.dot(se.flipped(), ep) <= 0)
return ep.magnitude();
return Math.abs((se.x * sp.y - se.y * sp.x) / se.magnitude());
return line.distanceToPoint(position);
}
cutWithSelection() {
......
......@@ -55,7 +55,6 @@
} else {
// calculate cursor position and delta and stuff
Application.cursor.position = this.camera.screenSpaceToCanvasSpace(newPosScreenSpace.copy());
//this._updateCurrentPosition();
this.oldPos = Application.cursor.currentPosition;
......@@ -505,7 +504,6 @@
break;
case Key.Control:
//this._updateCurrentPosition();
Application.redrawAllViews();
break;
case Key.E:
......@@ -551,7 +549,6 @@
break;
case Key.Control:
//this._updateCurrentPosition();
Application.redrawAllViews();
break;
}
......@@ -667,7 +664,7 @@
else if (m) { // if one line ending and the middle of the line is within cursor range select whole line. otherwise weird selection happens when whole line is inside cursor range
pointsToChangeSelection.push(line.start);
pointsToChangeSelection.push(line.end);
} else if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
} else if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Application.cursor.withinTimesRange(line.start, 3))
pointsToChangeSelection.push(line.start);
else if (Application.cursor.withinTimesRange(line.end, 3))
......@@ -692,7 +689,7 @@
// */
} else if (Preferences.selectEdges) {
for (let i = 0; i < lines.length; i++) {
if (Utilities.distancePointToLine(Application.cursor.position, lines[i]) <= Application.cursor.range) {
if (lines[i].distanceToPoint(Application.cursor.position) <= Application.cursor.range) {
pointsToChangeSelection.push(lines[i].start);
pointsToChangeSelection.push(lines[i].end);
}
......
......@@ -14,7 +14,7 @@
selectionHit() {
for (let line of this.getAsLines())
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range)
if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range)
return true;
for (let instance of this.reference.data.instances)
......@@ -23,7 +23,7 @@
line.start.position = this.objectSpaceForPosition(line.start.position);
line.end.position = this.objectSpaceForPosition(line.end.position);
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range)
if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range)
return true;
}
}
......
......@@ -32,6 +32,18 @@
&& line1.end.position.equals(line2.start.position);
}
distanceToPoint(point) {
let se = this.end.position.subtractVector(this.start.position);
let sp = point.subtractVector(this.start.position);
let ep = point.subtractVector(this.end.position);
if (Vector2.dot(se, sp) <= 0)
return sp.magnitude();
if (Vector2.dot(se.flipped(), ep) <= 0)
return ep.magnitude();
return Math.abs((se.x * sp.y - se.y * sp.x) / se.magnitude());
}
equals(other)
{
return Line.overlapping(this, other) && this.color.equals(other.color) && this._thickness == other._thickness;
......
......@@ -150,7 +150,7 @@
selectionHit() {
for (let line of this.data.lines)
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range)
if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range)
return true;
for (let p of this.data.points)
......@@ -159,7 +159,7 @@
for (let instance of this.data.instances)
for (let line of instance.getAsLines())
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range)
if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range)
return true;
return false;
......
......@@ -28,7 +28,7 @@
else if (m) { // if one line ending and the middle of the line is within cursor range select whole line. otherwise weird selection happens when whole line is inside cursor range
newSelPoints.push(line.start);
newSelPoints.push(line.end);
} else if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
} else if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Application.cursor.withinTimesRange(line.start, 3))
newSelPoints.push(line.start);
else if (Application.cursor.withinTimesRange(line.end, 3))
......@@ -41,7 +41,7 @@
}
} else if (Preferences.selectEdges) {
for (let line of Application.currentView.data.lines.concat(Application.currentView.selection.data.partialLines)) {
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range + (line.thickness * 0.5)) {
newSelPoints.push(line.start);
newSelPoints.push(line.end);
}
......@@ -93,7 +93,7 @@
else if (m) { // if one line ending and the middle of the line is within cursor range select whole line. otherwise weird selection happens when whole line is inside cursor range
newUnselPoints.push(line.start);
newUnselPoints.push(line.end);
} else if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
} else if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range + (line.thickness * 0.5)) {
if (Application.cursor.withinTimesRange(line.start, 3))
newUnselPoints.push(line.start);
else if (Application.cursor.withinTimesRange(line.end, 3))
......@@ -106,7 +106,7 @@
}
} else if (Preferences.selectEdges) {
for (let line of Application.currentView.selection.data.lines.concat(Application.currentView.selection.data.partialLines)) {
if (Utilities.distancePointToLine(Application.cursor.position, line) <= Application.cursor.range + (line.thickness * 0.5)) {
if (line.distanceToPoint(Application.cursor.position) <= Application.cursor.range + (line.thickness * 0.5)) {
newUnselPoints.push(line.start);
newUnselPoints.push(line.end);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment