OpenLearning
  1. /*
  2. * Test if a point is inside a triangle.
  3. * Julian Saknussemm
  4. *
  5. * Given three points of a triangle, and another arbitrary
  6. * point this program determines if that point lies
  7. * inside the triangle.
  8. *
  9. * This is determined by satisfying the following rule:
  10. * A point P(x,y) is inside triangle A(x0,y0), B(x1,y1), C(x2,y2)
  11. * iff
  12. * P is on the same side of the line AB as C
  13. * P is on the same side of the line BC as A
  14. * and
  15. * P is on the same side of the line AC as B
  16. *
  17. * A special case exits for a vertical line (inf gradient)
  18. * when testing the side of the line
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24. #define TRUE 1
  25. #define FALSE 0
  26.  
  27. #define OVER_LINE 1
  28. #define ON_LINE 0
  29. #define UNDER_LINE -1
  30.  
  31. #define NECESSARY_INPUTS 8
  32.  
  33. int positionCheck (double px, double py,
  34. double m, double b);
  35. int sameSideCheck (double px, double py,
  36. double m, double b,
  37. double lx,double ly);
  38. int triangleTest (double x0, double y0,
  39. double x1, double y1,
  40. double x2, double y2,
  41. double px, double py);
  42.  
  43.  
  44.  
  45.  
  46. int main(int argc, char* argv[]) {
  47. //x0, y0, x1, y1, x2, y2: respective points of triangle
  48. //px, py: point being tested
  49. double x0;
  50. double y0;
  51. double x1;
  52. double y1;
  53. double x2;
  54. double y2;
  55.  
  56. double px;
  57. double py;
  58.  
  59. int inputAmount = 0;
  60.  
  61. //get input
  62. printf("Triangle Vertex A (x,y): ");
  63. inputAmount += scanf("%lf,%lf", &x0,&y0);
  64. printf("Triangle Vertex B (x,y): ");
  65. inputAmount += scanf("%lf,%lf", &x1,&y1);
  66. printf("Triangle Vertex C (x,y): ");
  67. inputAmount += scanf("%lf,%lf", &x2,&y2);
  68. printf("Test Point (x,y): ");
  69. inputAmount += scanf("%lf,%lf", &px,&py);
  70.  
  71. if(inputAmount != NECESSARY_INPUTS) {
  72. //print error
  73. printf("Incorrect input\n");
  74. } else {
  75. // print answer
  76. if(triangleTest(x0, y0, x1, y1, x2, y2, px, py)) {
  77. printf("Point (%.2lf,%.2lf) is inside the triangle.\n", px, py);
  78. } else {
  79. printf("Point (%.2lf,%.2lf) is outside the triangle.\n", px, py);
  80. }
  81. }
  82.  
  83. return EXIT_SUCCESS;
  84. }
  85.  
  86. int triangleTest(double x0, double y0,
  87. double x1, double y1,
  88. double x2, double y2,
  89. double px, double py) {
  90.  
  91. //result after testing lines
  92. int line1Result;
  93. int line2Result;
  94. int line3Result;
  95.  
  96. // mab = gradient from point (xa, xb) to (xb, yb)
  97. double m01;
  98. double m02;
  99. double m12;
  100.  
  101. // Equation used:
  102. // b: y - y1 = m(x - x1) where x = 0
  103. double b01;
  104. double b02;
  105. double b12;
  106.  
  107. // Gradient Formula
  108. m01 = (y1 - y0) / (x1 - x0);
  109. m02 = (y2 - y0) / (x2 - x0);
  110. m12 = (y2 - y1) / (x2 - x1);
  111.  
  112. b01 = m01 * -x1 + y1;
  113. b02 = m02 * -x2 + y2;
  114. b12 = m12 * -x2 + y2;
  115.  
  116. // vertical line checks
  117. if (x1 == x0) {
  118. line1Result = ((px <= x0) == (x2 <= x0));
  119. } else {
  120. line1Result = sameSideCheck(px, py, m01, b01, x2, y2);
  121. }
  122.  
  123. if (x1 == x2) {
  124. line2Result = ((px <= x2) == (x0 <= x2));
  125. } else {
  126. line2Result = sameSideCheck(px, py, m12, b12, x0, y0);
  127. }
  128.  
  129. if (x2 == x0) {
  130. line3Result = ((px <= x0) == (x1 <= x0));
  131. } else {
  132. line3Result = sameSideCheck(px, py, m02, b02, x1, y1);
  133. }
  134.  
  135. return (line1Result && line2Result && line3Result);
  136. }
  137.  
  138. // test if two points lie on the same side of a line
  139. int sameSideCheck(double px, double py,
  140. double m, double b,
  141. double tx, double ty) {
  142.  
  143. //ty, tx: point of triangle
  144. int result = FALSE;
  145. if (positionCheck(px, py, m, b) == positionCheck(tx, ty, m, b)) {
  146. result = TRUE;
  147. }
  148. return result;
  149. }
  150.  
  151. int positionCheck(double px, double py, double m, double b) {
  152. int result;
  153. if(py < m * px + b) {
  154. result = UNDER_LINE;
  155. } else if (py == m * px + b) {
  156. result = ON_LINE;
  157. } else {
  158. result = OVER_LINE;
  159. }
  160. return result;
  161. }

Download file: inside.c (3.9 KB)

Comments

Loading
Error: Loading CSS chunk vendors-node_modules_fortawesome_pro-regular-svg-icons_faGlobe_js-node_modules_fortawesome_pr-4f8267 failed. (error: https://assets.openlearning.com/chunks_live/vendors-node_modules_fortawesome_pro-regular-svg-icons_faGlobe_js-node_modules_fortawesome_pr-4f8267.8fd07ef9993d3220.css)

Chat