To Check Wether the 3 Points are Collinear or Not.

Updated ashine80 0 Tallied Votes 325 Views Share

To Check Wether the 3 Points are Collinear or Not.

#include<stdio.h>
    #include<ctype.h>
    #include<conio.h>
    #include<math.h>
    
    int main()
    {
    int x[]={0,0,0};
    int y[]={0,0,0};
    int z[]={1,1,1};
    int i=0;
    int Det=0;
    int Size=sizeof(x)/sizeof(int);
    clrscr();
    printf("Size of x: %d\n",Size);
    for (i=0;i<3;i++)
     {
      printf("Enter X[%d]:",i);
      scanf("%d",&x[i]);
      printf("Enter Y[%d]:",i);
      scanf("%d",&y[i]);
     /* printf("Enter Z[%d]:",i); */
     /* scanf("%d",&z[i]); */
     }
    
     for (i=0;i<3;i++)
    
         {
           printf("X[%d]  Y[%d] Z[%d] = [%d] [%d] [%d] \n",i,i,i,x[i],y[i],z[i]);
          }
    
       for(i=0;i<3;i++)
          {
           if(i==0)
           {   Det=Det+(x[i]*pow(-1,i))*(y[i+1]*z[i+2]-y[i+2]*z[i+1]);
    	       printf("The Value of DET:%d\n",Det);}
    	  else 	if(i==1)
    		 {  Det=Det+(x[i]*pow(-1,i))*(y[i-1]*z[i+1]-y[i+1]*z[i-1]);
    		    printf("%d\n",pow(-1,i));
    		    printf("The Value of DET:%d\n",Det);
    		    }
    		      printf("Value of DET : %d\n",Det);
    	     if(i==2)
    		    {
    		     printf("The Value of DET:%d\n",Det);
    		     Det=Det+(x[i]*pow(-1,i))*(y[i-2]*z[i-1]-y[i-1]*z[i-2]);
    				 printf("%d\n",pow(1,2));
    		       printf("The Value Of DET :%d\n",Det); }
    
    
           }
    	  printf("The Value of DET:%d\n",Det);
    
      if (Det==0)
           {      printf("\n The Three Points P(X0,Y0), Q(X1,Y1),R(X2,X3)  are Collinear\n");}
           else
    	     { printf(" \n These Three Points are not Collinear\n " );}
    getch();
    getch();
    return(0);
    
    }

Dani AI

Generated

A robust, simple check is to compute the signed area (equivalently the 2D cross product) of the triangle formed by the three points. For integer coordinates this is exact: if the value below equals zero the points are collinear. This addresses the thread goal directly and avoids the indexing, sign and floating-point pitfalls that appear in the posted code. Thanks to for the title correction; in reply to and , the following is a compact, portable approach.

For points (x0,y0), (x1,y1), (x2,y2) compute

cross = (x1 - x0)*(y2 - y0) - (y1 - y0)*(x2 - x0)

If cross == 0 (integer case) the points are collinear. For floating-point inputs test fabs(cross) < eps (eps ≈ 1e-9).

A minimal, portable C implementation:

#include <stdio.h>

int main(void) {
    long long x0,y0,x1,y1,x2,y2;
    if (scanf("%lld %lld %lld %lld %lld %lld", &x0,&y0,&x1,&y1,&x2,&y2) != 6) return 1;
    long long cross = (x1 - x0)*(y2 - y0) - (y1 - y0)*(x2 - x0);
    if (cross == 0) printf("Collinear\n");
    else printf("Not collinear\n");
    return 0;
}

Troubleshooting notes and cautions:

  • Avoid pow(-1,i) for sign flipping: pow returns double and is unnecessary.
  • conio.h, clrscr() and getch() are nonstandard; omit them for portable code.
  • Watch scanf return values and index bounds if using arrays.
  • Use long long (or __int128 for extreme ranges) to prevent overflow on large coordinates.
  • For floating coordinates, use double and compare against an epsilon.

This method is simple, fast and less error-prone than looping determinant code with manual index arithmetic.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

What is your problem?

mike_2000_17 2,669 21st Century Viking Team Colleague Featured Poster

Is this, and all your other threads, entries to the code snippet contest?

Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster

The title should be "Whether", not "wether"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.