luc048.c (5960B)
1 /* Define a function to compute the distance between two points and 2 use it to develop another function that will compute the area of the 3 triangle whose vertices are A(x1, y1), B(x2, y2), and C(x3, y3). Use 4 these functions to develop a function which returns a value 1 if the 5 point (x, y) lines inside the triangle ABC, otherwise returns a value 6 0. Would you get any advantage if you develop these functions to 7 work on call by reference principle? 8 */ 9 /* Let Us C, Chap- 9 (Pointers), Page - 163, Qn No.: C(c) */ 10 11 #include <stdio.h> 12 #include <math.h> 13 #include <ctype.h> 14 #include <stdlib.h> 15 16 void distance(double, double, double, double, double *); 17 void area(double, double, double, double, double, double, double *); 18 int is_inside(double, double, double, double, double, double, double, double); 19 20 int main() 21 { 22 23 // Variables 24 double x, y, x1, x2, y1, y2, x3, y3, result_Distance, result_Area; 25 char choice; 26 27 /* ********** Requirement: 1 ********** */ 28 29 // Calculate the length of the line segment between two points, P1(x1, y1) and P2(x2, y2). 30 // Function Used: distance() 31 32 printf("--- Compute the distance between two points ---\n\n"); 33 printf("Enter the co-ordinates of Point 1 (Ex.: 5,6): "); 34 scanf("%lf,%lf", &x1, &y1); 35 printf("Enter the co-ordinates of Point 2 (Ex.: 3,4): "); 36 scanf("%lf,%lf", &x2, &y2); 37 38 distance(x1, y1, x2, y2, &result_Distance); 39 printf("\nDistance between P1(%g, %g) and P2(%g, %g) = %g", 40 x1, y1, x2, y2, result_Distance); 41 42 /* ********** Requirement: 2 ********** */ 43 44 // Calculate the area of a triangle ABC using its three vertices A(x1, y1), B(x2, y2), and C(x3, y3). 45 // Fuction Used: distance(), area() 46 47 printf("\n\n--- Compute the area of a triangle ABC ---\n\n"); 48 printf("Enter the co-ordinates of Point A (Ex.: 5,6): "); 49 scanf("%lf,%lf", &x1, &y1); 50 printf("Enter the co-ordinates of Point B (Ex.: 3,4): "); 51 scanf("%lf,%lf", &x2, &y2); 52 printf("Enter the co-ordinates of Point C (Ex.: 6,7): "); 53 scanf("%lf,%lf", &x3, &y3); 54 55 area(x1, y1, x2, y2, x3, y3, &result_Area); 56 printf("\nArea of the A(%g, %g), B(%g, %g), C(%g, %g) = %g", 57 x1, y1, x2, y2, x3, y3, result_Area); 58 59 /* ********** Requirement: 3 ********** */ 60 61 // Check if a test point P(x, y) lies strictly inside the triangle ABC. 62 // Function Used: distance(), area(), is_inside() 63 64 printf("\n\n--- Check if a test point P(x, y) lies strictly inside the triangle ABC ---\n\n"); 65 printf("Do you wish to use previous entered triangle?" 66 "\nIf Yes, type 'Y'; If No, type 'N': "); 67 scanf(" %c", &choice); 68 choice = toupper(choice); 69 switch (choice) 70 { 71 case 'Y': 72 break; 73 case 'N': 74 printf("Enter the co-ordinates of Point A (Ex.: 5,6): "); 75 scanf("%lf,%lf", &x1, &y1); 76 printf("Enter the co-ordinates of Point B (Ex.: 3,4): "); 77 scanf("%lf,%lf", &x2, &y2); 78 printf("Enter the co-ordinates of Point C (Ex.: 6,7): "); 79 scanf("%lf,%lf", &x3, &y3); 80 break; 81 default: 82 printf("\nYou entered invalid choice."); 83 break; 84 } 85 printf("Enter the co-ordinates of Test Point (Ex.: 3,5): "); 86 scanf("%lf,%lf", &x, &y); 87 88 if (is_inside(x, y, x1, y1, x2, y2, x3, y3)) 89 printf("\nTest Point (%g, %g) is inside ABC.", x, y); 90 else 91 printf("\nTest Point (%g, %g) is not inside ABC.", x, y); 92 93 /* ANSWER TO THE QUESTION: 94 Qn.: 95 Would you get any advantage if you develop these functions to 96 work on call by reference principle? 97 98 Ans: 99 NO, there is no advantage, and moreover, that would be a disadvantage. 100 It introduces unnecessary dereferencing overhead for every operation, 101 Which is less efficient than reading the simple value copy supplied by Call by Value. 102 */ 103 104 // End of Program 105 return 0; 106 } 107 108 void distance(double x1, double y1, double x2, double y2, double *result_Distance) 109 { 110 *result_Distance = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2)); 111 } 112 113 void area(double x1, double y1, double x2, double y2, double x3, double y3, double *result_Area) 114 { 115 double s, ab, bc, ac; 116 117 distance(x1, y1, x2, y2, &ab); 118 distance(x2, y2, x3, y3, &bc); 119 distance(x1, y1, x3, y3, &ac); 120 121 s = (ab + bc + ac) / 2.0; 122 double area_sq = s * (s - ab) * (s - bc) * (s - ac); 123 124 if (area_sq < 0) 125 // Handle cases where area_sq is slightly negative due to floating-point errors 126 *result_Area = 0.0; 127 else 128 *result_Area = sqrt(area_sq); 129 } 130 131 int is_inside(double x, double y, double x1, double y1, double x2, double y2, double x3, double y3) 132 { 133 double area_ABC, area_PAB, area_PBC, area_PAC; 134 135 const double EPSILON = 0.000001; 136 137 area(x1, y1, x2, y2, x3, y3, &area_ABC); 138 area(x, y, x1, y1, x2, y2, &area_PAB); 139 area(x, y, x2, y2, x3, y3, &area_PBC); 140 area(x, y, x1, y1, x3, y3, &area_PAC); 141 142 double sum_of_sub_areas = area_PAB + area_PBC + area_PAC; 143 144 if (fabs(area_ABC - sum_of_sub_areas) < EPSILON) 145 return 1; 146 else 147 return 0; 148 } 149 150 /* 151 *************** SAMPLE OUTPUT *************** 152 153 PS G:\bsc\letusc> ./*.exe 154 --- Compute the distance between two points --- 155 156 Enter the co-ordinates of Point 1 (Ex.: 5,6): 1234.56,7890.12 157 Enter the co-ordinates of Point 2 (Ex.: 3,4): 1234.56,7891.12 158 159 Distance between P1(1234.56, 7890.12) and P2(1234.56, 7891.12) = 1 160 161 --- Compute the area of a triangle ABC --- 162 163 Enter the co-ordinates of Point A (Ex.: 5,6): 100.10,200.20 164 Enter the co-ordinates of Point B (Ex.: 3,4): 105.10,200.20 165 Enter the co-ordinates of Point C (Ex.: 6,7): 100.10,205.20 166 167 Area of the A(100.1, 200.2), B(105.1, 200.2), C(100.1, 205.2) = 12.5 168 169 --- Check if a test point P(x, y) lies strictly inside the triangle ABC --- 170 171 Do you wish to use previous entered triangle? 172 If Yes, type 'Y'; If No, type 'N': y 173 Enter the co-ordinates of Test Point (Ex.: 3,5): 101.10,201.20 174 175 Test Point (101.1, 201.2) is inside ABC. 176 177 */