fork download
  1. #include <stdio.h>
  2.  
  3. //Reference: https://m...content-available-to-author-only...m.com/Collinear.html
  4.  
  5. typedef struct Point {
  6. float x,//abscisa
  7. y;//ordonata
  8. } TPoint;
  9.  
  10. //method number#2
  11. /*
  12. Sarrus Rule:
  13.   x1 y1 1
  14. A = x2 y2 1
  15.   x3 y3 1
  16.   x1 y1 1
  17.   x2 y1 1
  18.   x1y2 + x2y3 + x3y1 - x3y2 - x1y3-x2y1 = x1(y2-y3) - y1(x2-x3) + (x2y3-x3y2)
  19. */
  20. int checkCollinearity(TPoint P1, TPoint P2, TPoint P3) {
  21.  
  22. return (P1.x*(P2.y-P3.y) - P1.y*(P2.x-P3.x) + P2.x*P3.y - P2.y*P3.x);
  23. }
  24.  
  25. int main(int argc, char const *argv[]) {
  26.  
  27. TPoint P[3];
  28.  
  29. for(int i = 0; i < 3; ++i) {
  30. printf("Point#%d\n",i+1);
  31. scanf("%f %f",&P[i].x, &P[i].y);
  32. }
  33. if(P[0].x == P[1].x || P[0].x == P[2].x) {
  34.  
  35. if(P[2].x == P[0].x) {
  36. printf("Collinear Points.\n");
  37. } else {
  38. printf("No Collinear Points.\n");
  39. }
  40. //compute the slope of the line AB
  41. //y2-y1/x2-x1
  42. } else {
  43. if( (P[1].y - P[0].y)/(P[1].x - P[0].x) == (P[2].y - P[1].y)/(P[2].x - P[1].x)) {
  44. printf("Collinear Points.\n");
  45. } else {
  46. printf("No Collinear Points.\n");
  47. }
  48. }
  49.  
  50. if(checkCollinearity(P[0],P[1],P[2]) == 0) {
  51. printf("Collinear Points.\n");
  52. } else {
  53. printf("No Collinear Points.\n");
  54. }
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0.01s 5424KB
stdin
3.45 6.78 
2.34 1.23
6.75 7.43
stdout
Point#1
Point#2
Point#3
No Collinear Points.
No Collinear Points.