?
Solved

Class for line lenght, slope, area...

Posted on 2005-05-12
16
Medium Priority
?
333 Views
Last Modified: 2008-02-01
#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.
0
Comment
Question by:cuong5985
  • 9
  • 5
  • 2
16 Comments
 
LVL 9

Expert Comment

by:jhshukla
ID: 13988402
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
 
LVL 9

Expert Comment

by:jhshukla
ID: 13988409
correction: slope is too easy. (y2-y1)/(x2-x1)
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 13988430
fact that can help you in 4.i: the product of slopes of two perpendicular lines is -1
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13988454
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13988545
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 13988828
>> 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 Comment

by:cuong5985
ID: 14011300
#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
 
LVL 9

Expert Comment

by:jhshukla
ID: 14011433
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
 
LVL 9

Expert Comment

by:jhshukla
ID: 14011497
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 Comment

by:cuong5985
ID: 14011529
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 Comment

by:cuong5985
ID: 14011543
i have fixed it. Thanks for your comment.
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 14011573
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;
  ... rest of your code...
}
0
 

Author Comment

by:cuong5985
ID: 14011585
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
 
LVL 9

Accepted Solution

by:
jhshukla earned 500 total points
ID: 14011586
I think that is a typo. you probably meant <ctype.h>
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 14011643
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 Comment

by:cuong5985
ID: 14011926
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question