Home > OS >  function that the user inputs coordinates x,y and find if the coordinates are inside the area of an
function that the user inputs coordinates x,y and find if the coordinates are inside the area of an

Time:10-12

#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;
    }

so basically, this function is about of checking if a point is inside of a rectangle with the inferior corner x,y coordinates being v1x and v1y, and the superior coordinates v2x and v2y, and in the main function the user is supposed to input x and y coordinates to verify if they are inside it, i'm kinda of a newbie in C, but i already lost 1 entire day trying to find out what i did wrong here and idk maybe i'm too stupid to not see what is going on wrong here

CodePudding user response:

You are trying too much in a single line... this one is too complex:

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

and it's also wrong. It should be:

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

But avoid such statements...

Keep things simple by writing more code lines.

But first notice that a function like this should return 0 (aka false) when the point is outside and 1 (aka true) when it's inside.

So to keep it simple do:

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
}

One simple check on each line makes your code more simple.

And in main call it like

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

CodePudding user response:

The posted code has some issues, but the main problem seems to arise from some basic misunderstandings.

// 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 ) { /* ... */ }

If the intent is to write a function that returns a char value of 'D' when a point is inside a rectangle or 'F' otherwise (maybe 'B' if it's on one of the edges), the following could be a possible implementation.

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';    
}

  • Related