1

Real numbers x and y are given. We need to determine whether or not a point with coordinates (x; y) belongs to the shaded area.(Using only C programming language).

enter image description here

I'm a beginner in C programming. I have no idea how to solve this problem. But want so much learn this. Please explain to me.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Hafiz
  • 21
  • 4
  • Hi Hafiz, There is not really a function in c to do this. And since this question seems to be more about the algorithm, i suggest you add the `algorithm` tag. If you know what algorithm to use, and just want help with the language, please specify what algorithm you want to use. – David van rijn Dec 02 '20 at 12:59
  • 1
    This is a math question rather than a programming question. Try here: https://math.stackexchange.com/questions/tagged/geometry – Jabberwocky Dec 02 '20 at 13:09
  • 1
    Note that the diagram is symmetrical, so you can simplify the problem by taking `fabs(x)` and considering only the right-side of the diagram. So the problem reduces to finding if a point is within the area of a triangle. – Weather Vane Dec 02 '20 at 13:13
  • Suppose you label the three triangle vertices A, B, C and the point P. If the sum of the areas of the three triangles ABP, BCP, CAP > area ABC then the point lies outside ABC. – Weather Vane Dec 02 '20 at 13:21
  • Yes.İt's maybe conclusive.I will try it. @WeatherVane – Hafiz Dec 02 '20 at 13:23
  • Another way is to consider three vectors AB, BC, CA. If the point P lies on the right-hand side of each vector, then it is inside the triangle ABC. Use type `double`. – Weather Vane Dec 02 '20 at 13:23
  • A third way is finding the equations of the three lines that make up the triangle. Each line cuts the plane in half; these half-planes are defined by the inequations obtained by replacing `==` with `<=` or `>=` in the equations. The area inside the triangle is exactly the intersection of three half-planes; a point lies inside the triangle if and only if it satisfies those three inequations. Hint: one of the three inequations is `x >= 1` and the other two can be written as `y <= a * x + b` for some `a` and `b`. – Stef Dec 02 '20 at 15:03
  • Yet another way is to express point `(x,y) = a (0,1) + b (5,1) + c (4,3)` where `a+b+c = 1` (a.k.a. [barycentric coordinates](https://en.wikipedia.org/wiki/Barycentric_coordinate_system#Barycentric_coordinates_on_triangles)). The point is inside the triangle on the right iff `a, b, c > 0`. – dxiv Dec 02 '20 at 17:24

1 Answers1

0

well lets see if my math doesn't fail me to define such a thing the equation would be something along the lines of:

  • 1<Y<3, -5<=X<=5
  • Y<3/4X+1, 0<=X<=4
  • Y<4/3X+1, -4<=X<=0
  • Y<3X+1, -5<=X<=-4
  • Y<1/3X+1, 4<=X<=5

This are a bunch of equations that basically set the area with this you can make a really ineffective if and else() we could use a bit of math and if you had asked me this like a year ago i would still have in mind the analitics class unfortunetely thats long gone, but i have one last card up my sleeve and thats wolfram alpha i putted up the equations it pasted the following(except the 1<Y<3 as that caused a small error)

(Y<3/4X+1, 0<=X<=4) and(Y<4/3X+1, -4<=X<=0) and (Y<3X+1, -5<=X<=-4) and (Y<1/3X+1, 4<=X<=5)

Welp unfortunately it outputted the same so were gonna go into the code now we just need to create a function i will try to find a better "formula compression" tomorrow Also since you are new i will have to explain some things || is a logical or while && is a logical and this has to do with math i chose double instead of float because the problem is mostly a math problem so i chose some precision although the input is an int because im lazy ive already answered on how to protect the scanf from wrong input requests at Is there a way of limiting scanf in C? so if you need this code to be well rounded and user proof read above in C true is any number that is not zero meaning while(1) or while(133) make infinite loops since they will always be true %d in the scanf are the format modifier to basically tell C "im waiting for an integer"

int IsItInside(double x, double y){
    if (y>3 || y<1 ){// i dont need to "range" this since the its always like this
        return 0; 
    }
    
    if (y<(((3*x)/4)+1)&& x>=0 && x<=4){
        return 1; 
    }
    if (y<((4/(3*x))+1) && x>=-4 && x<=0){
        return 1; 
    }
    if (y<((3*x)+1) && x>=-5 && x<=-4){
        return 1;
    }
    if (y<((1/3*x)+1) && x>=4 &&  x<=5){
        return 1;
    }
    return 0;
}
int main(int argc, char const *argv[]){
    int x=0, y=0;
    
    printf("Enter x: ");
    scanf ("%d", &x); 
    
    printf("Enter y: ");
    scanf ("%d", &y);
    if(IsItInside(x,y)){
        printf("It is inside");
    }else{
        printf("It is outside");
    }
    
    return 0;
}
Imeguras
  • 436
  • 6
  • 18