(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.距离公式,正负号.
相关文章推荐
- 计算几何(point&line)红书模板
- org.apache.lucene.queryParser.ParseException: Encountered "<EOF>" at line 1, column 0.
- 【九度】题目1548:平面上的点 && 【LeetCode】Max Points on a Line
- C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )
- ArcGIS API for JavaScript 实现point multipoint line polyline cirle等graphics的绘制
- command line warning #10006: ignoring unknown option '/NODEFAULTLIB'错误解决(XPDF编译时出现的问题)
- Coins in a Line I & II
- loongson & MySQL 如何处理 /etc/lsb-base-logging.sh: line 72: RUNLEVEL: unbound variable
- 错误:line 1: syntax error: "(" unexpected
- Pointcut is not well-formed: expecting 'name pattern' at character position
- OJ:line point 6
- Stsadm command-line tool (Office SharePoint Server)
- SharePoint’s Popularity Trends & Popularity and Search Reports
- SAXParseException; lineNumber: 21; columnNumber: 7; 元素类型为 "set" 的内容必须匹配 "(met
- 黑马程序员_<<字符流的缓冲技术和装饰类(BufferedWriter,BufferedReader,LineNumberReader)>>
- LeetCode Max Points on a Line & Sort List
- Android NDK: Host 'awk' tool is outdated. Please define HOST_AWK to point to Gawk or Nawk !
- Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
- error: unrecognized command line option "-fconserve-stack"
- MySQL server version for the right syntax to use near 'type=InnoDB' at line 1