# Class for line lenght, slope, area...

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <cmath>

class Coords
{
public:
void DisplayCoords();
void SetCoords(int X, int Y);
void GetCoords();
void SetLine(Line &AB, Coords A, Coords B);
void DisplayLine(Line AB);
float LineSlope(Line AB);
double LineLength(Line AB);

int x;
int y;
};

main()
{
Coords A, B;
Line AB;
cout<<"A: "<<endl;
A.GetCoords();
cout<<"B: "<<endl;
B.GetCoords();
SetLine(AB, A, B);
DisplayLine(AB);
cout<<"The length of the line is: "<<LineLength(AB)<<endl;
cout<<"The slope of the line is: "<<LineSlope(AB)<<endl;
getch();
}

void Coords::GetCoords()
{
int X, Y;
cout<<">>";
cin>>X;
cout<<">>";
cin>>Y;
SetCoords(X, Y);

}
void Coords::SetCoords(int X, int Y)
{
x = X;
y = Y;

}

void Coords::DisplayCoords()
{
cout<<"("<<x<<", "<<y<<")";
}

void Coords::SetLine(Line &AB, Coords A, Coords B)
{
AB.A = A;
AB.B = B;
}

void Coords::DisplayLine(Line AB)
{
cout<<endl<<"{";
DisplayCoords(AB.A);
cout<<", ";
DisplayCoords(AB.B);
cout<<"}"<<endl;
}

Can someone help me to find the area, linelength, lineslope, and circumstances of the triangle in the rectangular coordinate.
###### Who is Participating?

Commented:
I think that is a typo. you probably meant <ctype.h>
0

Commented:
hi cuong,

1. use pythagorean formula for length
2. slope is too easy. (y2-y1)/(x2-x2)
3. area: find the signed area under curve for all three sides in either clockwise or anticlockwise direction and add it up (this actually works for all shapes, concave or convex but without holes)
4. circumcircle(?) is a little bit complex to calculate. you will need:
i. equations for perpendicular bisectors for at least two sides
ii. find their intersection. this is the circumcenter
iii. distance from circumcenter to any vertex is the radius of circumcircle.

now try to translate this into C++. define the data structures you might need. this is not very complex but non-trivial problem. attempt one thing at a time. if the results don't match expected output, post ur code and we will be glad to help.

jaydutt
0

Commented:
correction: slope is too easy. (y2-y1)/(x2-x1)
0

Commented:
fact that can help you in 4.i: the product of slopes of two perpendicular lines is -1
0

Software ArchitectCommented:
line lenght could be calculated with pitagorical formula (the square root of the sum of the x and y delta to the 2th power):

double LineLength(Line AB) {
return sqrt( pow(AB.A.x-AB.B.x,2.0) + pow(AB.A.y-AB.B.y,2.0) );
}

for slope you can use the atan() function.

the formula for area of triangle is (Heron formula):
S = sqrt ( p.(p-a).(p-b).(p-c) )
where a,b,c are the lenght of each side and:
p = 0.5 (a+b+c)

0

Software ArchitectCommented:
0

Commented:
>> for slope you can use the atan() function
we want the slope. not the angle.

>> find the signed area under curve for all three sides in either clockwise or anticlockwise direction
correction: only clockwise direction gives correct answer. use abs() [or may be fabs() is needed] to get actual area.
0

Author Commented:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <conio.h>
#include <stype.h>
#include <cmath>

struct Coords
{
int x;
int y;
};

struct Line
{
Coords A;
Coords B;
};

void DisplayCoords (Coords Point);
void SetCoords (Coords &Point, int x, int y);
void GetCoords (Coords &Point);
void SetLine (Line &AB, Coords A, Coords B);
void DisplayLine (Line AB);
float LineSlope (Line AB);
double LineLength (Line AB);

main ()
{
Coords A, B;
Line AB;
cout<<"A: "<<endl;
GetCoords(A);
cout <<"B: "<<endl;
GetCoords(B);
SetLine (AB, A, B);
DisplayLine (AB);
cout <<"The length of the line is: "<<LineLength(AB)<<endl;
cout <<"The slope of the line is: "<<LineSlope(AB)<<endl;
getch();
}

void GetCoords (Coords &Point)
{
int x, y;
cout <<">>";
cin>>x;
cout <<">>";
cin>>y;
SetCoords (Point, x, y);
}

void SetCoords (Cooords &Point, int x, int y)
{
Point.x = x;
Point.y = y;
}

void SetLine (Line &AB, Coords A, Coords B)
{
AB.A = A;
AB.B = B;
}

void DisplayLine (Line AB)
{
cout<<endl<<"{";
DisplayCoords(AB.A);
cout<<", ";
DisplayCoords(AB.B);
cout<<"}"<<endl;
}

void DisplayCoords (Coords Point)
{
cout<<"("<<Point.x<<", "<<Point.y<<")";
}

double LineLength(Line AB)
{
return sqrt( pow(AB.A.x-AB.B.x,2.0) + pow(AB.A.y-AB.B.y,2.0) );
}

float LineSlope (Line AB)
{
if (AB.B.x!=AB.A.x)
return ((AB.B.y-AB.A.y)/(AB.B.x-AB.A.x));//Slope = (y2-y1)/(x2-x1)
else
return 3.14/2;
}

This show me a lot of problem:
37 H:\LIne, Slope.cpp `cout' undeclared (first use this function)
52 H:\LIne, Slope.cpp `cin' undeclared (first use this function)
How should i fix this
0

Commented:
add the following line after all #include's:
using namespace std;

or alternatively,
using std::cin;
using std::cout;
using std::err; // ditto
using std::endl; // this is not required in your code but most often you would need it

or you could add 'std::' before all your cin, cout, endl and err. like, std::cin >> some_stuff;
0

Commented:
float LineSlope (Line AB)
{
if (AB.B.x!=AB.A.x)
return ((AB.B.y-AB.A.y)/(AB.B.x-AB.A.x));//Slope = (y2-y1)/(x2-x1);
else
return 3.14/2; <<----- this is wrong. it is supposed to be +/- INF
}

I would define a global const float.
const float INF = 1.#INF; // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcstdlib/html/vcsampsamplemembersofthenumericlimitsclassstlsample.asp
and the new function would be float LineSlope (Line AB)
{
if (AB.B.x!=AB.A.x)
return ((AB.B.y-AB.A.y)/(AB.B.x-AB.A.x));//Slope = (y2-y1)/(x2-x1);
else
return INF;
}

or you could stick to returning angles. in that case, as jaime said you can use atan from <cmath>
if (AB.B.x!=AB.A.x)
return atan(((AB.B.y-AB.A.y)/(AB.B.x-AB.A.x));//Slope = (y2-y1)/(x2-x1));
0

Author Commented:
I fix all but the last problem is : \LIne, Slope.cpp H:\H stype.h: No such file or directory. It does not shoe me where is error either.
0

Author Commented:
i have fixed it. Thanks for your comment.
0

Commented:
1.#INF is for VC compilers only. to be 100% sure you can do the following but it makes INF non-const.
float INF;

int main(){
char * pINF = (char*)&INF;
pINF[0] = 0x3f; pINF[1] = 0x80; pINF[2] = pINF[3] = 0;
}
0

Author Commented:
can you help me with circumference and area:

double Circumference(Triangle ABC)
{
return LineLength(ABC.AB)+LineLength(ABC.BC)+LineLength(ABC.CA); //A+B+C
}

i dont know how to do with area

doulbe Area(Triangle ABC)
0

Commented:
linelength looks right to me:
area use jaime's solution (Heron's formula)

float a,b,c,s, area;
a = LineLength(ABC.BC);
b = LineLength(ABC.CA);
c = LineLength(ABC.AB);
s = (a+b+c)/2;
area = pow(s*(s-a)*(s-b)*(s-c), 0.5);

note that jaime used S for area and I used s for semi-perimeter. do not confuse yourself with two s's. also, he uses p for semi-perimeter.
0

Author Commented:
double Area( Triangle ABC)
{
return pow((Circumference(ABC)/2.0)*((Circumference(ABC)/2.0)-LineLength(ABC.AB))
*((Circumference(ABC)/2.0)-LineLength(ABC.BC))*((Circumference(ABC)/2.0)-LineLength(ABC.CA)),0.5);
}

I do on this way, just copy the way jhshukla do it, area = pow(s*(s-a)*(s-b)*(s-c), 0.5); But it doesnt shoe the right answer.  I can not you a, b, c,, s instead.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.