Straight Line (not sure)


SUBMITTED BY: Guest

DATE: Feb. 20, 2014, 10:57 a.m.

FORMAT: C++

SIZE: 1.9 kB

HITS: 1091

  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4. class line
  5. {
  6. int m;
  7. int c;
  8. public:
  9. line(){m=0,c=0;};
  10. line(int x,int y);
  11. void perpendicular(int a, int b);
  12. float perpendicularDistance(line A);
  13. void intersectionPoint(int p,int q);
  14. };
  15. line::line(int x,int y)
  16. {
  17. m=x;
  18. c=y;
  19. cout<< "y= "<<m<<"x+"<<c<<endl;
  20. }
  21. void line::perpendicular(int a, int b)
  22. {
  23. float m1=-(1/a); //m1=slope of the perpendicular line
  24. float k=(m*b)+a; //k=constant of the perpendicular line
  25. cout<<"y= "<<m1<<"x+"<<k<<endl;
  26. }
  27. float line::perpendicularDistance(line A)
  28. {
  29. int a,b;
  30. cin>>a;
  31. cin>>b;
  32. float X=(m*a);
  33. float Y=b;
  34. float distance=(-X+Y-c)/(sqrt(m*m+1*1));
  35. return abs(distance);
  36. }
  37. void line::intersectionPoint(int p,int q)
  38. {
  39. int x1,y1;
  40. x1=(-q+c)/(-m+p);
  41. y1=((c*p)-(q*m))/(-c+p);
  42. cout<< "("<<x1<< ","<<y1<< ")\n";
  43. }
  44. int main()
  45. {
  46. int x,y;
  47. cout<< "enter value of m & c\n";
  48. cin>>x;
  49. cin>>y;
  50. cout<< "Equation: ";
  51. line A(x,y);
  52. cout << endl<< "For Perpendicular Distance enter (x,y)"<<endl;
  53. cout<<"Distance= "<<A.perpendicularDistance(A)<<" Units"<<endl;
  54. int a,b;
  55. cout<< endl<<"For Perpendicular Equation enter (a,b)"<<endl;
  56. cin>>a;
  57. cin>>b;
  58. cout<< "Perpendicular Equation: ";
  59. A.perpendicular(a,b);
  60. cout<< "\nFor Intersection Point enter another lines m & c"<<endl;
  61. int p,q;
  62. cin>>p;
  63. cin>>q;
  64. cout<< "Equation: ";
  65. line B(p,q);
  66. cout<< "Intersection Point is: ";
  67. A.intersectionPoint(p,q);
  68. return 0;
  69. }

comments powered by Disqus