【问题标题】:function that the user inputs coordinates x,y and find if the coordinates are inside the area of an given rectangle c用户输入坐标 x,y 并查找坐标是否在给定矩形 c 的区域内的函数
【发布时间】:2021-11-30 11:47:50
【问题描述】:
#include <stdio.h>
char dentroRetangulo(int v1x, int v1y, int v2x, int v2y, int x, int y);
int main()
{
    int a, b, c, f,d,g,x,y;
    char D, h;
    
    printf("== Coordinates of a rectangle ==\n");
    printf("Inform the coordinates of the left inferior corner : \n");
    scanf("%d %d", &a, &b);
    printf("Inform the coordinates of the superior right corner: \n");
    scanf("%d %d", &c, &d);
    printf("=== points ===\n");
    printf("inform the coordinates of the point (x,y)\n");
    scanf("%d %d", &f, &g);
    h = dentroRetangulo(a,b,c,d,f,g);
    
    if(h == D)
    {
        printf("O ponto(%d, %d) encontra-se dentro  do retangulo", x, y);
    }
    
        return 0;
}
    
char dentroRetangulo(int v1x, int v1y, int v2x, int v2y, int x, int y)
{
    char D;
    char B;
    char F;
    if(x>v1x && x<v2x || y> v1y && y<v2y)
    {
        return D;
    }

所以基本上,这个函数是关于检查一个点是否在一个矩形内,下角x,y坐标是v1x和v1y,上坐标是v2x和v2y,并且在主函数中假设用户输入 x 和 y 坐标以验证它们是否在其中,我在 C 语言中有点像新手,但我已经花了一整天的时间试图找出我在这里做错了什么,我可能太愚蠢了看看这里出了什么问题

【问题讨论】:

  • 所以函数检查语句是否为真并返回一个 D 值,给主函数,如果 h == D 从函数返回的字符,if 语句检查它并打印点在矩形内,对不起,我是巴西人,我忘了翻译最后一个 printf
  • 是什么让你觉得你做错了什么?有什么症状?你观察什么?你想要什么行为?
  • 另外,if(x&gt;v1x &amp;&amp; x&lt;v2x || y&gt; v1y &amp;&amp; y&lt;v2y) 应该有 &amp;&amp;,而你有 ||,因为你希望 all 条件为真。
  • 好像你没有发布所有代码
  • @FaustinoDaSilva 不要使用“固定”代码更新问题...如果您愿意,您可以发布固定代码作为答案,但不要更改问题

标签: c function geometry


【解决方案1】:

你在一行中尝试了太多......这太复杂了:

if(x>v1x && x<v2x || y> v1y && y<v2y)

这也是错误的。应该是:

if(x>v1x && x<v2x && y> v1y && y<v2y)

但要避免这样的陈述......

通过编写更多代码行来保持简单。

但首先要注意,当点在外面时,像这样的函数应该返回 0(又名 false),在里面时应该返回 1(又名 true)。

所以为了简单起见:

int dentroRetangulo(int v1x, int v1y, int v2x, int v2y, int x, int y)
{
    if (x < v1x) return 0;  // If the point is to the left, return 0
    if (x > v2x) return 0;  // If the point is to the rigth, return 0
    if (y < v1y) return 0;  // If the point is below, return 0
    if (y > v2y) return 0;  // If the point is above, return 0

    return 1; // The point is inside, return 1
}

对每一行进行一次简单的检查使您的代码更简单。

main 中这样称呼它

if(dentroRetangulo(a,b,c,d,f,g))
{
    printf("O ponto(%d, %d) encontra-se dentro  do retangulo", f, g);
}

【讨论】:

  • 是的,但它反对我的第一个问题,如果你重复多次,我的问题是返回字符 D 的一种方式,我在我的主代码中找到了,第二行是错误的,因为我在不知情的情况下编辑了代码
  • 我说它反对我的第一个问题,因为我不想返回整数 1,我想返回一个字符 D
  • @FaustinoDaSilva 所以当点在里面时你想返回字符D,对吗?当它在外面时,你想要哪个角色?
  • 我要退F
【解决方案2】:

发布的代码有一些问题,但主要问题似乎源于一些基本的误解。

// The following will declare, but WON'T initialize two variables
// of type 'char'. Here, D, is the NAME of the variable, not its content,
// which is indeterminated.
char D, h; 

// Here h is assigned the value returned by OP's function, which has the 
// exact same issue noted before. Its value will still remain indeterminated.
h = /* ... */;

// Comparing two variables with indeterminated values has undefined behavior.
if ( h == D ) { /* ... */ }

如果打算编写一个函数,当点在矩形内时返回 char'D''F' 否则(如果它位于边之一上,则可能是 'B'),以下可能是一种可能的实现方式。

typedef struct point_s
{
    int x, y;
} Point;

char point_rectangle_intersection(Point bottom_left, Point top_right, Point p)
{
    // Check if the point is outside.
    if ( p.x < bottom_left.x  ||  p.x > top_right.x  ||
         p.y < bottom_left.y  ||  p.y > top_right.y )
        return 'F';
    
    // Check if the point is on one of the edges.
    if ( p.x == bottom_left.x  ||  p.x == top_right.x  ||
         p.y == bottom_left.y  ||  p.y == top_right.y )
        return 'B';

    // The point is inside.
    return 'D';    
}

【讨论】:

  • 哇最后一部分比我的还要好,你实际上减少了一行代码,只返回里面的内容
  • 对不起,因为我是新来的,所以我有点困惑,我不知道你可以回答你自己的问题
  • if ( h == D ) 这是我的误解,实际上代码必须返回您在其中输入的内容,例如如果代码 if h == 'D' 它会说坐标位于矩形内,依此类推,我在想它应该是另一回事
  • 我将您标记为答案,因为您完成了这项工作以了解我为什么会挣扎并从中获得一些意义,我已经解决了自己的问题,顺便说一句,但您才是真正得到答案的人最接近解决 thx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 2018-06-03
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 2013-02-05
  • 2016-09-11
相关资源
最近更新 更多