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 on the board for
the next phase.  Still refuse to create a point if a click or touch
occurs off the board.  This doesn't really happen for a click, but does
for a touch on a touch screen device.  In that case, the touch off the
board should be ignored.

Don't use `==` for comparison of floating point numbers.
  • Loading branch information
drgrice1 committed Dec 13, 2024
1 parent 493dd18 commit 15a7446
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 84 deletions.
43 changes: 22 additions & 21 deletions htdocs/js/GraphTool/cubictool.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,12 @@
};

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 (
this.point1.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
if (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are on the same vertical line as the first point,
// then use the highlight point coordinates instead.
if (Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps)
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

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

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 (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are on the same vertical line as the first point, or on the same
// vertical line as the second point, 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) ||
!gt.boardHasPoint(coords[1], coords[2])
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
)
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,17 @@
};

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 (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are on the same vertical line as the first point, on the same vertical
// line as the second point, or on the same vertical line as the third point, 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) ||
!gt.boardHasPoint(coords[1], coords[2])
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
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down
75 changes: 44 additions & 31 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 @@ -1705,7 +1715,6 @@ window.graphTool = (containerId, options) => {
// 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.
phase1(coords) {
// Don't allow the point to be created off the board.
if (!gt.boardHasPoint(coords[1], coords[2])) return;

gt.board.off('up');
Expand Down Expand Up @@ -1735,16 +1744,17 @@ 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. If that point is on the board , then finalize the line.
phase2(coords) {
// Don't allow the second point to be created on top of the first or off the board
if (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are the same those of the first point,
// 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)) ||
!gt.boardHasPoint(coords[1], coords[2])
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
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -1848,11 +1858,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. If the point is on the board, then 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;

gt.board.off('up');

this.center = gt.board.create('point', [coords[1], coords[2]], {
Expand Down Expand Up @@ -1880,16 +1890,17 @@ 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. If that point is on the board, then finalize the circle.
phase2(coords) {
// Don't allow the second point to be created on top of the center or off the board
if (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are the same those of the first point,
// 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)) ||
!gt.boardHasPoint(coords[1], coords[2])
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
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -2041,14 +2052,15 @@ 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 (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are on the same horizontal or vertical line as the vertex,
// 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) ||
!gt.boardHasPoint(coords[1], coords[2])
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
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down Expand Up @@ -2175,6 +2187,7 @@ 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;

gt.board.off('up');

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

this.phase2 = (coords) => {
// Don't allow the second point to be created on the first point or off the board.
if (
this.point1.X() === gt.snapRound(coords[1], gt.snapSizeX) ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
if (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are the same as those of the first point,
// then use the highlight point coordinates instead.
if (Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps)
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

Expand Down
26 changes: 13 additions & 13 deletions htdocs/js/GraphTool/quadratictool.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,12 @@
};

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 (
this.point1.X() == gt.snapRound(coords[1], gt.snapSizeX) ||
!gt.boardHasPoint(coords[1], coords[2])
)
return;
if (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are on the same vertical line as the first point,
// then use the highlight point coordinates instead.
if (Math.abs(this.point1.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps)
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

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

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 (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are on the same vertical line as the first point, or on the same
// vertical line as the second point, 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) ||
!gt.boardHasPoint(coords[1], coords[2])
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
)
return;
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');

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

this.phase1 = (coords) => {
// 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;
// Don't allow the point to be created off the board
if (!gt.boardHasPoint(coords[1], coords[2])) return;

gt.board.off('up');

Expand All @@ -252,12 +252,11 @@
};

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.
if (
!gt.boardHasPoint(coords[1], coords[2]) ||
Math.abs(this.shiftPoint.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps
)
if (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are on the same vertical line as the first point,
// then use the highlight point coordinates instead.
if (Math.abs(this.shiftPoint.X() - gt.snapRound(coords[1], gt.snapSizeX)) < JXG.Math.eps)
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');
Expand Down Expand Up @@ -291,12 +290,11 @@
};

this.phase3 = (coords) => {
// If the current coordinates are on the same horizontal line as the first point or off the board,
if (!gt.boardHasPoint(coords[1], coords[2])) return;

// If the current coordinates are on the same horizontal line as the first point,
// then use the highlight point coordinates instead.
if (
Math.abs(this.shiftPoint.Y() - gt.snapRound(coords[2], gt.snapSizeY)) < JXG.Math.eps ||
!gt.boardHasPoint(coords[1], coords[2])
)
if (Math.abs(this.shiftPoint.Y() - gt.snapRound(coords[2], gt.snapSizeY)) < JXG.Math.eps)
coords = this.hlObjs.hl_point.coords.usrCoords;

gt.board.off('up');
Expand Down

0 comments on commit 15a7446

Please sign in to comment.