【问题标题】:How to check if rectilinear polygons intersect如何检查直线多边形是否相交
【发布时间】:2021-07-13 03:02:30
【问题描述】:

我正在尝试用 C 创建一个程序来检查直线多边形的线是否在任何点相互交叉。
我只需要在任何点都不相交的简单直线多边形。它可以是逆时针或顺时针。

方向值将小于 10。NS 方向必须与 WE 方向交替,反之亦然。

正在传递的输入是来自输入文件的方向形式,例如;并在图片中显示:

S 2 E 4 S 2 E 4 N 2 W 4 N 2 W 4

我试图将点存储在一个二维数组中,每个点都被检查为真,但我无法弄清楚在逆时针和顺时针方向的情况下如何移动,因为这些点可能是 N4 E6 或 S4 W6。在这种情况下,如果我在值为 N-4 (x,y) = (0,4) 时添加并在 S-4 (x,y) = (0,-4) 时减去,则在使用它时会失败数组中的索引。

int arr[10][10];
int xPrime = 0, yPrime = 0;

bool checkContinuity(int y, const char * dir ){

    if(strcmp(dir, "S")==0){
        y = -y;
        cols = y;
        int j;
        for(j = cols; j >= 0; j--){
            if(arr[xPrime][j] == 1 && j != yPrime){
                return false;
            }
            arr[xPrime][j] = 1;             
            printf(" %d ", j);
        }
        yPrime -= y;
        if(yPrime < 0)
            yPrime = -yPrime;
    }
    else if(strcmp(dir, "W")==0){
        y = -y;
        cols = y;       
        int j;
        for(j = cols; j >= 0; j--){
            if(arr[j][yPrime] == 1 && j != xPrime && (j != 0 && yPrime != 0)){
                return false;
            }
            arr[j][yPrime] = 1;
            printf(" %d ", j);  
        }
        xPrime -= y;
        if(xPrime < 0)
            xPrime = -xPrime;
    }
    else if(strcmp(dir, "N")==0){
        cols = y;
        int j;
        for(j = 0; j <= cols; j++){
            if(arr[xPrime][j] == 1)
                return false;
            arr[xPrime][j] = 1; 
            printf(" %d ", j);
        }
        yPrime += y;
    }
    else if(strcmp(dir, "E")==0){
        cols = y;
        int j;
        for(j = 0; j <= cols; j++){
            if(arr[j][yPrime] == 1 && j != xPrime)
                return false;
            arr[j][yPrime] = 1;
            printf(" %d ", j);  
        }
        xPrime += y;
    }
    else
        return false;
            
    return true;
}

【问题讨论】:

  • 一种方法是选择数组的中心作为零点。您可以使用负索引,只要它们在分配的内存范围内。
  • 请提供您已经编写的代码。这不仅表明您已经为此工作,而且这将帮助我们以您的编码风格提供答案,使其更具可读性
  • 您可以遍历输入字符串并累积给定的坐标增量以确定每个顶点的坐标。然后在给定链中构建线段列表。最后应用线段的交叉点检测算法,如所述,例如在ics.uci.edu/~goodrich/teach/geom/notes/LineSweep.pdf
  • @Ahsan Ali - 首先你必须明确输入运动在任何方向上是否有任何限制,如果有,限制是什么。
  • 限制是方向WE应该和SN交替

标签: arrays c algorithm geometry polygon


【解决方案1】:

存储实际的多边形可能比存储所有可能的多边形更容易 平面的点。这样我们就不会受到选择的限制 点数组(代码中的 arr)。请参阅此工作示例:

  • 多边形存储在 int 数组中 P = {0, 0, x1, y1, x2, y2, ...}

  • 段是两点多边形。函数“intersection”检查是否 两个这样的段 Q 和 P 相交; 如果是,则返回交点坐标。

  • 它使用辅助函数“between”来检查一个数字是否介于另外两个之间。

  • 函数'next'计算多边形的下一个点,假设 输入作为字符字符串提供(例如:“S2E4S2E4N2W4N2W4”)

  • 在 main 函数中,我们现在遍历所有段,并检查它们是否与 任何以前的。

    当然,应该在某些时候检查输入的完整性等。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


void
next(           const char *s,
                int *v          )
{
        v[2] = v[0]; 
        v[3] = v[1];
        int step = s[1] - '0';
        switch(s[0]) {
        case 'S': v[3] -= step; break;
        case 'N': v[3] += step; break;
        case 'W': v[2] -= step; break;
        case 'E': v[2] += step; break;
        }
}


int
between(        int x,
                int a,
                int b           )
{
        return a < b ? x >= a && x <= b : x >= b && x <= a;
}

int
intersection(   int *P,
                int *Q,
                int *R          )
{
        if(P[0] == P[2] && Q[1] == Q[3]){  // P vertical, Q horizontal (w.l.o.g.)
                if(between(P[0], Q[0], Q[2]) && between(Q[1], P[1], P[3])){
                        R[0] = P[0]; 
                        R[1] = Q[1]; 
                        return 1;
                } else
                        return 0;
        }else if(Q[0] == Q[2] && P[1] == P[3])
                return intersection(Q, P, R);
        else return 0;
}

int
main() {
        char *s = "S2E4S2E4N2W4N2W4";
        
        int n = strlen(s) / 2,                                  // number of steps
                *P = calloc((n + 1) * 2, sizeof(int)),          // polygon
                R[2];                                           // intersection
        if(!P) exit(137);
        
        for(int k = 0; k < n; k++){
                next(s + 2 * k, P + 2 * k);
                for(int j = 0; j < k - 1; j++) {
                        if(intersection(P + k * 2, P + j * 2, R)) {
                                printf("Intersection at: %d, %d\n", R[0], R[1]);
                                exit(0);
                        }
                }
        }
        printf("No intersection\n");
}
        

【讨论】:

  • 它在某些情况下会检测到交叉点,但当多边形从南以外的地方开始时会失败。例如“N4E6S4W6”
  • 程序并没有失败,而是将多边形的最终闭合步骤报告为交集。如果你想避免这种情况,那么不要检查第一个和最后一个段。这可以通过将内部循环中的条件更改为if(!(k == n - 1 &amp;&amp; j == 0) &amp;&amp; intersection... 来完成
猜你喜欢
  • 2016-03-26
  • 2014-01-24
  • 2011-08-30
  • 2021-02-21
  • 2014-03-21
  • 2021-07-11
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
相关资源
最近更新 更多