Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Error when id equals 0 in getEdgeAt function #2994

Closed
steven-network opened this issue Apr 18, 2017 · 4 comments
Closed

Error when id equals 0 in getEdgeAt function #2994

steven-network opened this issue Apr 18, 2017 · 4 comments

Comments

@steven-network
Copy link

steven-network commented Apr 18, 2017

Hi all,

I discovered a bug in your library when getEdgeAt function is called when edge id equals 0.

key: 'getEdgeAt',
      value: function getEdgeAt(pointer) {
        var returnEdge = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

        // Iterate over edges, pick closest within 10
        var canvasPos = this.canvas.DOMtoCanvas(pointer);
        var mindist = 10;
        var overlappingEdge = null;
        var edges = this.body.edges;
        for (var i = 0; i < this.body.edgeIndices.length; i++) {
          var edgeId = this.body.edgeIndices[i];
          var edge = edges[edgeId];
          if (edge.connected) {
            var xFrom = edge.from.x;
            var yFrom = edge.from.y;
            var xTo = edge.to.x;
            var yTo = edge.to.y;
            var dist = edge.edgeType.getDistanceToEdge(xFrom, yFrom, xTo, yTo, canvasPos.x, canvasPos.y);
            if (dist < mindist) {
              overlappingEdge = edgeId;
              mindist = dist;
            }
          }
        }
        if (overlappingEdge !== null) { // Here if ID = 0, return undefined
          if (returnEdge === true) {
            return this.body.edges[overlappingEdge];
          } else {
            return overlappingEdge;
          }
        } else {
          return undefined;
        }
      }

I added "overlappingEdge !== null" to return id even if it is 0.

@steven-network steven-network changed the title Error when id equals 0 in getEdgeAt function Error when id equals 0 in getEdgeAt functions Apr 18, 2017
@steven-network steven-network changed the title Error when id equals 0 in getEdgeAt functions Error when id equals 0 in getEdgeAt function Apr 18, 2017
@mojoaxel mojoaxel added this to the Minor Release v4.20 milestone Apr 18, 2017
@bradh
Copy link
Contributor

bradh commented Apr 19, 2017

@wimrijnders Is this the same as #2936 (fixed pending release)?

@wimrijnders
Copy link
Contributor

@bradh No, not the same.

The solution is valid. I'll make a PR for this. We really need a way to detect this kind of errors. I hope we can agree on the usage of jslint RSN.

@wimrijnders
Copy link
Contributor

@steven-network Thanks for reporting!

@steven-network
Copy link
Author

steven-network commented Apr 19, 2017

@wimrijnders you're welcome!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants