Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add docs for Trace.addRandomIntersectingSegment
  • Loading branch information
seadowg committed Dec 2, 2025
commit 74a6464d7f69bdbcd62f1f981d1a059323fae7d0
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.odk.collect.shared.geometry.LineSegment
import org.odk.collect.shared.geometry.Point
import org.odk.collect.shared.geometry.Trace
import org.odk.collect.shared.geometry.segments
import org.odk.collect.shared.geometry.support.GeometryTestUtils.interpolate
import kotlin.random.Random

object GeometryTestUtils {
Expand Down Expand Up @@ -41,6 +42,13 @@ object GeometryTestUtils {
})
}

/**
* Choose random segment, a random (interpolated) point on that segment and then create a new
* trace with an additional point just beyond that to create an intersecting trace.
*
* Never chooses the last segment as a target for intersecting as that can only create a
* collinear intersection which is unlikely to be accurate due to inaccuracies in [interpolate].
*/
fun Trace.addRandomIntersectingSegment(): Trace {
val intersectionSegment = segments().dropLast(1).random()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to ignore the last segment? As I understand it, it avoids the scenario when the additional segment lies on the last segment but why? This still would be treated as an intersection.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I'd meant to add a comment when moving this to a method and then forgotten:

/**
* Choose random segment, a random (interpolated) point on that segment and then create a new
* trace with an additional point just beyond that to create an intersecting trace.
*
* Never chooses the last segment as a target for intersecting as that can only create a
* collinear intersection which is unlikely to be accurate due to inaccuracies in [interpolate].
*/

This is basically the "two consecutive segments intersect" special case, and we test this it specifically (Trace#intersects returns true when two segments and they intersect).

val intersectPosition = Random.nextDouble(0.1, 1.0)
Expand Down