bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc048.c (6151B)


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