Thursday 10 January 2013

Let Us C Chapter 2 problem No: C-j


//Program to check 3 points lie in a straight line
/*concept: If three points are in straight line (i.e. collinear)
the area of triangle formed by these three points will be zero */

#include<iostream>
#include<math.h>
#include<windows.h>

using namespace std;

class points
{
public:
    void in()
    {

    }

    double distance(long u, long v, long w, long x)
    {
        double d;
        d=sqrt(((v-u)*(v-u))+((x-w)*(x-w)));
        return(d);
    }

    long area_tri(long p, long q, long r)
    {
        long peri, area;
        peri = (p+q+r)/2;
        area = (peri*(peri-p)*(peri-q)*(peri-r));
        return(area);
    }
};

int main(void)
{
    system("color 3F");
    long x1,x2,x3,y1,y2,y3,dist_ab,dist_bc,dist_ac;
    double ar;
    points p;

    cout<<"Enter x and y co-ordinates of three points:\n";
    cout<<"Point A:  \n";
    cout<<"x1:  ";
    cin>>x1;
    cout<<"y1:  ";
    cin>>y1;
    cout<<"Point B:  \n";
    cout<<"x2:  ";
    cin>>x2;
    cout<<"y2:  ";
    cin>>y2;
    cout<<"Point C:  \n";
    cout<<"x3:  ";
    cin>>x3;
    cout<<"y3:  ";
    cin>>y3;

    cout<<endl;

    dist_ab = p.distance(x1,x2,y1,y2);
    dist_ac = p.distance(x1,x3,y1,y3);
    dist_bc = p.distance(x2,x3,y2,y3);

    ar=p.area_tri(dist_ab,dist_ac,dist_bc);

    if(ar==0)
        cout<<"Points lie on a straight line\n";
    else
        cout<<"Points do not lie on a staight line\n";
}



No comments:

Post a Comment

Comments please: