0

I came across this and this questions, all about detection intersections in Android. Well, I couldn't manage to make them work with the final code, so I made an example where 2 lines definitely intersect. Not even lucky in that case. I've made an example code with two straight paths, regions that fit them, and a point that definitely crosses it. Completely unlucky.

var theyCross = false
val intersectionPath = Path()

val clipArea = Region(0, 0, 100, 100)
val path1 = Path()
path1.moveTo(50f, 0f)
path1.lineTo(50f, 100f)

val path2 = Path()
path2.moveTo(0f, 50f)
path2.lineTo(100f, 50f)

val newRegion1 = Region()
newRegion1.setPath(path1, clipArea)

val newRegion2 = Region()
newRegion2.setPath(path2, clipArea)

if(
    !newRegion1.quickReject(newRegion2) && 
    newRegion1.op(newRegion2, Region.Op.INTERSECT)
) {
    // lines should cross!
    theyCross = true
}

if (intersectionPath.op(path1, path2, Path.Op.INTERSECT)) {
    if (!intersectionPath.isEmpty) {
        // lines should cross!
        theyCross = true
    }
}

if (newRegion1.contains(50, 50)) {
    // lines should cross!
    theyCross = true
}

if (newRegion1.quickContains(49, 49, 51, 51)) {
    // lines should cross!
    theyCross = true
}

In this example I'm not using a Canvas, but in my original code, I am, and each path is made of a Paint with strokeWidth. No luck. Has any of you faced this before?

Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93

1 Answers1

1

It only works if the paths are surfaces, not lines, e.g. :

val clipArea = Region(0, 0, 100, 100)
val path1 = Path()
path1.moveTo(50f, 0f)
path1.lineTo(50f, 100f)
path1.lineTo(51f, 100f)
path1.lineTo(51f, 0f)
path1.close()

val path2 = Path()
path2.moveTo(0f, 50f)
path2.lineTo(100f, 50f)
path2.lineTo(100f, 51f)
path2.lineTo(0f, 51f)
path2.close()

By the way the (ignored) return value of newRegion1.setPath(path1, clipArea) is now true (non-empty) instead of false

bwt
  • 17,292
  • 1
  • 42
  • 60
  • That's awesome, thanks!. Now, I'll have to find a way to convert a complex, user-made path into a closed one! _I'm still wondering why one would need a region, if it is virtually useless (I honestly thought using a region was to **actually** convert a path into a surface!)_ – Sergi Juanola Dec 06 '18 at 18:33
  • Just as a heads up, after I tried it with complex `Path`s and a 3x3 pixels rectangle (made out of a closed `Path`), it turns out it intersects with paths as if they were closed (so, it will intersect the interior of a curve, but not the exterior!). After this, it is clear that my previous comment and your answer are **the** answer. – Sergi Juanola Dec 07 '18 at 08:35
  • @Korcholis can you explain how it worked? you have example of code with complex path? – Vadim Eksler Dec 22 '19 at 07:47
  • 1
    Hey @VadimEksler, I don't have the code anymore, but from what I remember, after getting the point list, I just created a copy of the list with 1 added pixel in one of the dimensions and added it in reverse to the drawing list. Something like **A, B, C, D, D(-1), C(-1), B(-1), A(-1)** . This way, you're creating a really thin closed path (of just one pixel tall or wide) which will be hard to perceive by the user (especially if the strokeWidth is more than 1 pixel). Then it's just a matter of `close()`ing the path. – Sergi Juanola Dec 23 '19 at 08:33
  • @Korcholis tnx but wich method exactly works to detect intersect – Vadim Eksler Dec 23 '19 at 15:37