Skip to content

Commit

Permalink
Make changes for existing graph tools that were made with the sine wa…
Browse files Browse the repository at this point in the history
…ve tool.

For all of the graph tools switch to using the highlight point
coordinates when a click occurs at an invalid location for the next
phase.

Don't use `==` for comparison of floating point numbers.
  • Loading branch information
drgrice1 committed Dec 12, 2024
1 parent 493dd18 commit 5f0bf97
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 73 deletions.
36 changes: 18 additions & 18 deletions htdocs/js/GraphTool/cubictool.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@
this.supportsSolidDash = true;

this.phase1 = (coords) => {
// Don't allow the point to be created off the board.
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are off the board, then use the highlight point coordinates instead.
if (!gt.boardHasPoint(coords[1], coords[2])) coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand All @@ -240,13 +240,13 @@
};

this.phase2 = (coords) => {
// Don't allow the second point to be created on the same
// vertical line as the first point or off the board.
// If the current coordinates are on the same vertical line as the first point or off the board,
// then use the highlight point coordinates instead.
if (
this.point1.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand All @@ -272,14 +272,14 @@
};

this.phase3 = (coords) => {
// Don't allow the third point to be created on the same vertical line as the
// first point, on the same vertical line as the second point, or off the board.
// If the current coordinates are on the same vertical line as the first point, on the same vertical
// line as the second point, or off the board, then use the highlight point coordinates instead.
if (
this.point1.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
this.point2.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
Math.abs(this.point2.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');
this.point3 = gt.graphObjectTypes.cubic.createPoint(coords[1], coords[2], [
Expand Down Expand Up @@ -311,16 +311,16 @@
};

this.phase4 = (coords) => {
// Don't allow the fourth point to be created on the same vertical line as the first
// point, on the same vertical line as the second point, on the same vertical line as
// the third point, or off the board.
// If the current coordinates are on the same vertical line as the first point, on the same vertical
// line as the second point, on the same vertical line as the third point, or off the board, then
// use the highlight point coordinates instead.
if (
this.point1.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
this.point2.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
this.point3.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
Math.abs(this.point2.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
Math.abs(this.point3.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down
82 changes: 46 additions & 36 deletions htdocs/js/GraphTool/graphtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,8 @@ window.graphTool = (containerId, options) => {
// degenerate.
gt.adjustDragPosition = (e, point, pairedPoint) => {
if (
(point.X() == pairedPoint?.X() && point.Y() == pairedPoint?.Y()) ||
(Math.abs(point.X() - pairedPoint?.X()) < JXG.Math.eps &&
Math.abs(point.Y() - pairedPoint?.Y()) < JXG.Math.eps) ||
!gt.boardHasPoint(point.X(), point.Y())
) {
const bbox = gt.board.getBoundingBox();
Expand All @@ -696,7 +697,11 @@ window.graphTool = (containerId, options) => {
let y = point.Y() < bbox[3] ? bbox[3] : point.Y() > bbox[1] ? bbox[1] : point.Y();

// Adjust position of the point if it has the same coordinates as its paired point.
if (pairedPoint && x === pairedPoint.X() && y === pairedPoint.Y()) {
if (
pairedPoint &&
Math.abs(x - pairedPoint.X()) < JXG.Math.eps &&
Math.abs(y - pairedPoint.Y()) < JXG.Math.eps
) {
let xDir, yDir;

if (e.type === 'pointermove') {
Expand Down Expand Up @@ -735,7 +740,11 @@ window.graphTool = (containerId, options) => {
// horizontal or vertical line as its paired point by a drag. Note that when this method is called, the point has
// already been moved by JSXGraph. This prevents parabolas from being made degenerate.
gt.adjustDragPositionRestricted = (e, point, pairedPoint) => {
if (point.X() == pairedPoint?.X() || point.Y() == pairedPoint?.Y() || !gt.boardHasPoint(point.X(), point.Y())) {
if (
Math.abs(point.X() - pairedPoint?.X()) < JXG.Math.eps ||
Math.abs(point.Y() - pairedPoint?.Y()) < JXG.Math.eps ||
!gt.boardHasPoint(point.X(), point.Y())
) {
const bbox = gt.board.getBoundingBox();

// Clamp the coordinates to the board.
Expand All @@ -756,8 +765,8 @@ window.graphTool = (containerId, options) => {
yDir = e.key === 'ArrowUp' ? 1 : e.key === 'ArrowDown' ? -1 : 0;
}

if (x == pairedPoint.X()) x += xDir * gt.snapSizeX;
if (y == pairedPoint.Y()) y += yDir * gt.snapSizeY;
if (Math.abs(x - pairedPoint.X()) < JXG.Math.eps) x += xDir * gt.snapSizeX;
if (Math.abs(y - pairedPoint.Y()) < JXG.Math.eps) y += yDir * gt.snapSizeY;

// If the computed new coordinates are off the board,
// then move the coordinates the other direction instead.
Expand Down Expand Up @@ -1524,8 +1533,9 @@ window.graphTool = (containerId, options) => {
// object's defining points.
for (const point of gt.selectedObj.definingPts) {
if (
point.X() == gt.snapRound(coords.usrCoords[1], gt.snapSizeX) &&
point.Y() == gt.snapRound(coords.usrCoords[2], gt.snapSizeY)
Math.abs(point.X() - gt.snapRound(coords.usrCoords[1], gt.snapSizeX)) <
JXG.Math.eps &&
Math.abs(point.Y() - gt.snapRound(coords.usrCoords[2], gt.snapSizeY)) < JXG.Math.eps
)
return;
}
Expand Down Expand Up @@ -1702,11 +1712,10 @@ window.graphTool = (containerId, options) => {
gt.board.on('up', (e) => this.phase1(gt.getMouseCoords(e).usrCoords));
}

// In phase1 the user has selected a point. If that point is on the board, then make
// that the first point for the line, and set up phase2.
// In phase1 the user has selected a point. Create the first point for the line, and set up phase2.
phase1(coords) {
// Don't allow the point to be created off the board.
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are off the board, then use the highlight point coordinates instead.
if (!gt.boardHasPoint(coords[1], coords[2])) coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -1735,16 +1744,16 @@ window.graphTool = (containerId, options) => {
gt.board.update();
}

// In phase2 the user has selected a second point. If that point is on the board
// and is not the same as the first point, then finalize the line.
// In phase2 the user has selected a second point, so finalize the line.
phase2(coords) {
// Don't allow the second point to be created on top of the first or off the board
// If the current coordinates are the same those of the first point or off the board,
// then use the highlight point coordinates instead.
if (
(this.point1.X() == gt.snapRound(coords[1], gt.snapSizeX) &&
this.point1.Y() == gt.snapRound(coords[2], gt.snapSizeY)) ||
(Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps &&
Math.abs(this.point1.Y() - gt.snapRound(coords[2], gt.snapSizeY)) < JXG.Math.eps) ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -1848,11 +1857,11 @@ window.graphTool = (containerId, options) => {
gt.board.on('up', (e) => this.phase1(gt.getMouseCoords(e).usrCoords));
}

// In phase1 the user has selected a point. If that point is on the board, then make
// that the center of the circle, and set up phase2.
// In phase1 the user has selected a point. Create the center of the circle, and set up phase2.
phase1(coords) {
// Don't allow the point to be created off the board.
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are off the board, then use the highlight point coordinates instead.
if (!gt.boardHasPoint(coords[1], coords[2])) coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

this.center = gt.board.create('point', [coords[1], coords[2]], {
Expand Down Expand Up @@ -1880,16 +1889,16 @@ window.graphTool = (containerId, options) => {
gt.board.update();
}

// In phase2 the user has selected a second point. If that point is on the board
// and is not the same as the center, then finalize the circle.
// In phase2 the user has selected a second point, so finalize the circle.
phase2(coords) {
// Don't allow the second point to be created on top of the center or off the board
// If the current coordinates are the same those of the first point or off the board,
// then use the highlight point coordinates instead.
if (
(this.center.X() == gt.snapRound(coords[1], gt.snapSizeX) &&
this.center.Y() == gt.snapRound(coords[2], gt.snapSizeY)) ||
(Math.abs(this.center.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps &&
Math.abs(this.center.Y() - gt.snapRound(coords[2], gt.snapSizeY)) < JXG.Math.eps) ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -2005,8 +2014,8 @@ window.graphTool = (containerId, options) => {
}

phase1(coords) {
// Don't allow the point to be created off the board.
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are off the board, then use the highlight point coordinates instead.
if (!gt.boardHasPoint(coords[1], coords[2])) coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -2041,14 +2050,14 @@ window.graphTool = (containerId, options) => {
}

phase2(coords) {
// Don't allow the second point to be created on the same
// horizontal or vertical line as the vertex or off the board.
// If the current coordinates are on the same horizontal or vertical line as the vertex or off the board,
// then use the highlight point coordinates instead.
if (
this.vertex.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
this.vertex.Y() == gt.snapRound(coords[2], gt.snapSizeY) ||
Math.abs(this.vertex.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
Math.abs(this.vertex.Y() - gt.snapRound(coords[2], gt.snapSizeY)) < JXG.Math.eps ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -2173,8 +2182,9 @@ window.graphTool = (containerId, options) => {
}

phase1(coords) {
// Don't allow the fill to be created off the board
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are off the board, then use the highlight point coordinates instead.
if (!gt.boardHasPoint(coords[1], coords[2])) coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

gt.selectedObj = new gt.graphObjectTypes.fill(gt.createPoint(coords[1], coords[2]));
Expand Down
11 changes: 6 additions & 5 deletions htdocs/js/GraphTool/intervaltools.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@
}

this.phase1 = (coords) => {
// Don't allow the point to be created off the board.
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are off the board, then use the highlight point coordinates instead.
if (!gt.boardHasPoint(coords[1], coords[2])) coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -559,12 +559,13 @@
};

this.phase2 = (coords) => {
// Don't allow the second point to be created on the first point or off the board.
// If the current coordinates are the same as those of the first point or off the board, then use
// the highlight point coordinates instead.
if (
this.point1.X() === gt.snapRound(coords[1], gt.snapSizeX) ||
Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down
4 changes: 2 additions & 2 deletions htdocs/js/GraphTool/pointtool.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@

initialize(gt) {
this.phase1 = (coords) => {
// Don't allow the point to be created off the board
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are off the board, then use the highlight point coordinates instead.
if (!gt.boardHasPoint(coords[1], coords[2])) coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down
22 changes: 11 additions & 11 deletions htdocs/js/GraphTool/quadratictool.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@
this.supportsSolidDash = true;

this.phase1 = (coords) => {
// Don't allow the point to be created off the board.
if (!gt.boardHasPoint(coords[1], coords[2])) return;
// If the current coordinates are off the board, then use the highlight point coordinates instead.
if (!gt.boardHasPoint(coords[1], coords[2])) coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand All @@ -199,13 +199,13 @@
};

this.phase2 = (coords) => {
// Don't allow the second point to be created on the same
// vertical line as the first point or off the board.
// If the current coordinates are on the same vertical line as the first point or off the board,
// then use the highlight point coordinates instead.
if (
this.point1.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand All @@ -231,14 +231,14 @@
};

this.phase3 = (coords) => {
// Don't allow the third point to be created on the same vertical line as the
// first point, on the same vertical line as the second point, or off the board.
// If the current coordinates are on the same vertical line as the first point, on the same vertical
// line as the second point, or off the board, then use the highlight point coordinates instead.
if (
this.point1.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
this.point2.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
Math.abs(this.point2.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down
2 changes: 1 addition & 1 deletion htdocs/js/GraphTool/sinewavetool.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@

this.phase2 = (coords) => {
// If the current coordinates are on the same vertical line as the first point or off the board,
// then use the coordinates of the highlight point instead.
// then use the highlight point coordinates instead.
if (
!gt.boardHasPoint(coords[1], coords[2]) ||
Math.abs(this.shiftPoint.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps
Expand Down

0 comments on commit 5f0bf97

Please # to comment.