给定两个共线线段AB和CD,我如何确定它们是否重叠?如何找到重叠的起点和终点?
以下是我使用的方法。我首先确保A
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
}
}
现在,没有更简单的解决方案吗?
更新:还有另一种方法...将两个线段的长度之和与最外面的点之间的距离进行比较。如果后者较小,则存在重叠。
最佳答案
确保A
if (pb - pc >= 0 and pd - pa >=0 ) { // overlap
OverlapInterval = [ max(pa, pc), min(pb, pd) ] // it could be a point [x, x]
} // else: not overlap
关于algorithm - 查找共线之间的重叠,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15726825/