Thursday, 15 August 2013

Chapter 5 Solutions problem [J][j]

Question-Write a function to compute the distance betweentwo points 
and use it to develop another function that will compute the
area of the triangle whose vertices are A(x1, y1), B(x2, y2), 
and C(x3, y3). Use these functions to develop a function 
which returns a value 1 if the point (x, y)lines inside the 
triangle ABC, otherwise a value 0. 

Solution-
This is going to be the last problem of this chapter. I have escaped the problem [i] because i feel it's just a cakewalk for a person who has completed or at least have a basic knowledge of programming. still if you people facing trouble with that. no worries, please let me know.so let's look the solution.one more thing i used call by value method to write this program, just to show you how much hectic is  to write up a program using call by value. so, I would suggest you guys should get used to of call by reference. it really helps.  

#include <stdio.h>
#include <math.h>

int main()

  float area(float,float,float,float,float,float); 
  float distance(float,float,float,float);
  float inside(float,float,float,float,float,float,float,float);
    int m;
    float x1,y1,x2,y2,x3,y3,c,x,y;
    printf("Enter the points(x1,y1)(x2,y2)(x3,y3)\n");
    scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
    c=area(x1,y1,x2,y2,x3,y3);
    printf("Area of the Traingle=%f\n",c);
    printf("Enter a point (x,y) to check\n" );
    scanf("%f%f",&x,&y);
    m=inside(x,y,x1,y1,x2,y2,x3,y3);
    printf("%d",m);
    return 0;
}

float distance(float a,float b,float c,float d)
{
    float dis;
    dis=pow(pow((c-a),2)+pow((d-b),2),0.5);
    return(dis);
}
float area(float x1,float y1,float x2,float y2,float x3,float y3 )
{
    float ar;
    ar=0.5*distance(x3,y3,x1+x2/2,y1+y2/2)*distance(x1,y1,x2,y2);
    return(ar);
}
float inside(float x,float y,float x1,float y1,float x2,float y2,float x3,float y3)
{
float s,m;
s=distance(x1+x2+x3/3,y1+y2+y3,x,y);
m=distance(x1+x2+x3/3,y1+y2+y3,x3,y3);
if(s>m)
return(0);
else if(s<m)
return(1);
else
{
printf("point is on the periphery\n");
return(0);
}
}

No comments:

Post a Comment