给定两条共线线段 AB 和 CD,我如何找到它们是否重叠?如何定位重叠的起点和终点?
以下是我正在使用的方法。我首先要确保 A < B 和 C < D。
if(pa < pc){
if(pc < pb){
if(pd < pb){
// overlap exists; CD falls entirely within AB
}
else {
// overlap exists; CB is the overlapping segment
}
}
else {
// no overlap exists; AB lies before CD
}
}
else {
if(pa < pd){
if(pb < pd){
// overlap exists; AB lies entirely within CD
}
else {
// overlap exists; AD is the overlapping segment
}
}
else {
// no overlap exists; CD lies before AB
}
}
现在,没有更简单的解决方案来做到这一点吗?
更新:还有另一种方法......将两个段的长度之和与最外点之间的距离进行比较。如果后者较小,则存在重叠。