您的位置:首页 > 编程语言 > C语言/C++

(15C++ for homework)Point&Line

2016-09-05 11:04 281 查看

(15C++ for homework)Point&Line

标签(空格分隔): 程序设计实验 c++

本人学院

15C for homeworkPointLine
标签空格分隔 程序设计实验 c

本人学院

description

file provided

understanding

my answer

the standard answer

rethinking

description

Please complete two classs Point and Line.

Point-format: (X, Y)

Line-format: A*x + B*y + C = 0

function details:

# Point::display() return a string like this “(1, 1)”;

# Line::display() return a string like this

“1x+2y+1=0” / “-1x+1y=0” / “1y-40=0”;

# dist_PP(…) distance between two points;

# dist_LP(…) distance between a Line and a point;

# dist_LL(…) distance between a Line and a Line;

if two line is not parallel, return ‘-1’.

Hint:

1、熟悉‘友元函数’的用法;

2、复习’点线距离‘求解的公式;

file provided

main.cpp

#include "Point.h"
#include "Line.h"
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
double x, y, a, b, c, _LL;
Point p0, p1;
Line l0, l1;
p0 = Point(10, 10);
p1 = Point(1, 1);
l0 = Line(2, 4, -3);
l1 = Line(1, 3, 1);
cin >> x >> y >> a >> b >> c;
p1.reset(x, y);
l0.reset(a, b, c);

cout << setprecision(2) << fixed << "distance between " << p0.display()
<< " and " << p1.display() << " is " << dist_PP(p0, p1) << endl;
cout << setprecision(2) << fixed << "distance between " << l0.display()
<< " and " << p0.display() << " is " << dist_LP(l0, p0) << endl;

_LL = dist_LL(l0, l1);
if (_LL == -1) {
cout << l0.display() << " and " << l1.display()
<< " is not parallel.\n";
} else {
cout << setprecision(2) << fixed << "distance between " << l0.display()
<< " and " << l1.display() << " is " << _LL << endl;
}

return 0;
}


Point.hpp

#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <string.h>
using namespace std;

class Point {
public:
double x, y;
Point();
Point(double, double);
~Point() {}
void reset(const double &, const double &);
string display();
friend double dist_
cfdb
PP(const Point &, const Point &);
};

#endif


Line.hpp

#ifndef LINE_H
#define LINE_H
#include "Point.h"
#include <iostream>
#include <string.h>
using namespace std;

class Line {
public:
double a, b, c;
Line();
Line(double, double, double);
~Line() {}
void reset(const double &, const double &, const double &);
string display();
friend double dist_LP(const Line &, const Point &);
friend double dist_LL(const Line &, const Line &);
};

#endif


understanding

my answer

Point.cpp

#include<iostream>
#include<sstream>
#include<cmath>
#include"Point.h"

using namespace std;

Point::Point() {
x= 0.0;
y = 0.0;
}
Point::Point(double NewX, double NewY) {
x = NewX;
y = NewY;
}
void Point::reset(const double &NewX, const double &NewY) {
x = NewX;
y = NewY;
}
string Point::display() {
stringstream st;
st << "(";
st << x;
st << ", ";
st << y;
st << ")";
return st.str();
}
double dist_PP(const Point & a, const Point &b) {
return sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2));
}


Line.cpp

#include<iostream>
#include<sstream>
#include<cmath>
#include"Line.h"

using namespace std;

Line::Line() {
a = b = c = 0.0;
}
Line::Line(double Newa, double Newb, double Newc) {
a = Newa;
b = Newb;
c = Newc;
}
void Line::reset(const double &Newa, const double &Newb, const double &Newc) {
a = Newa;
b = Newb;
c = Newc;
}
string Line::display() {
stringstream st;
if (a == 0 && b == 0 & c == 0) return "";
if (a != 0) {
st << a;
st << "x";
}
if (b != 0) {
if (b > 0 && a != 0) st << "+";
st << b;
st << "y";
}
if (c != 0) {
if (c > 0) st << "+";
st << c;
}
st << "=0";
return st.str();
}
double dist_LP(const Line & l, const Point & p) {
double result;
result = (l.a * p.x + l.b * p.y + l.c) / sqrt(l.a * l.a + l.b * l.b);
result = fabs(result);
return result;
}
double dist_LL(const Line & l1, const Line & l2) {
if (fabs(l1.a / l2.a) == fabs(l1.b / l2.b)) {
double result;
double rate = l1.a / l2.a;
double realc = l2.c * rate;
result = fabs(l1.c - realc);
result = result / sqrt(l1.a * l1.a + l1.b * l1.b);
return result;
} else {
return -1;
}
}


the standard answer

Point.cpp

#include "Point.h"
#include <stdlib.h>
#include <sstream>
#include <cmath>

Point::Point() {
x = y = 0;
}

Point::Point(double _X, double _Y) {
x = _X;
y = _Y;
}

void Point::reset(const double &_X, const double &_Y) {
x = _X;
y = _Y;
}

string Point::display() {
stringstream x_s, y_s;
x_s << x;
y_s << y;
string str = "("+x_s.str()+", "+y_s.str()+")";
return str;
}

double dist_PP(const Point &_P1, const Point &_P2) {
return sqrt((_P1.x-_P2.x)*(_P1.x-_P2.x)+(_P1.y-_P2.y)*(_P1.y-_P2.y));
}


Line.cpp

#include "Line.h"
#include <stdlib.h>
#include <sstream>
#include <cmath>

Line::Line() {
a = b = c = 0;
}

Line::Line(double _A, double _B, double _C) {
a = _A;
b = _B;
c = _C;
}

void Line::reset(const double &_A, const double &_B, const double &_C) {
a = _A;
b = _B;
c = _C;
}

string Line::display() {
string str = "";
stringstream a_s, b_s, c_s;
a_s << a;
b_s << b;
c_s << c;
if (a != 0) str += a_s.str()+"x";
if (b < 0) str += b_s.str()+"y";
else if (a != 0 && b > 0) str += "+"+b_s.str()+"y";
else if (b > 0) str += b_s.str()+"y";
if (c < 0) str += c_s.str();
else if (c > 0) str += "+"+c_s.str();
str += "=0";
return str;
}

double dist_LP(const Line &_L, const Point &_P) {
return abs((_L.a*_P.x+_L.b*_P.y+_L.c)/sqrt(_L.a*_L.a+_L.b*_L.b));
}

double dist_LL(const Line &_L1, const Line &_L2) {
if (_L1.a/_L1.b != _L2.a/_L2.b) {
return -1;
}
return abs((_L1.c-_L2.c*_L1.a/_L2.a)/sqrt(_L1.a*_L1.a+_L1.b*_L1.b));
}


rethinking

1.三个友元函数是类的外部函数

2.距离公式,正负号.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐