2

I have nodes with the following properties

(n:User {valUpper:100, valLower:-100}) 
(m:User {valUpper:200, valLower: 0})

Using cypher is it possible to find if (m)'s range overlaps with (n)?

This is what I would have done in js..

if max(x2, y2) - min(x1, y1) < (x2 - x1) + (y2 - y1) {
     //Ranges Overlap
}
gk103
  • 377
  • 5
  • 15

2 Answers2

2

Apoc is very worthy option. Just want to mention non apoc way for people who dont want to enable apoc.

WITH n.valUpper as x1, n.valLower as y1, m.valUpper as x2, m.valLower as y2
RETURN 
  CASE WHEN x2 > y2 THEN x2 ELSE y2 END - 
  CASE WHEN x1 < y1 THEN x1 ELSE y1 END 
  < (x2 - x1) + (y2 - y1)
Tom Geudens
  • 2,638
  • 9
  • 15
Swapnil
  • 121
  • 1
  • 4
1

Using the formula from your javascript example you could do it like this

... here you match for n and m ...
WITH n.valUpper as x1, n.valLower as y1, m.valUpper as x2, m.valLower as y2
RETURN apoc.coll.max([x2, y2]) - apoc.coll.min([x1, y1]) < (x2 - x1) + (y2 - y1)

Hope this helps.

Regards, Tom

Tom Geudens
  • 2,638
  • 9
  • 15