Individual software engineering project work - the number of intersections of plane

  1. Teaching classes given at the beginning of the article and can be cloned Github project addresses. (1')
project content
This work belongs Spring 2020 Computer Software Engineering Institute (Roger Ren Jian)
This is a job requirement Individual project work
My teaching classes 006
The project's GitHub address IntersectProj
My goal for this operation is Personal development program to improve the quality of write high-performance program

PSP

  1. Before we start implementing the program, in the following form to record your PSP is estimated to be spent on the development of the various modules of the program period. (0.5 ')
  2. After you implement your program, your time on each module of the program actually spent in the following table PSP record. (0.5 ')
  • PSP
PSP2.1 Personal Software Process Stages Estimated time consuming (minutes) The actual time-consuming (minutes)
Planning plan
- Estimate - How much time is estimated that the task requires 10 10
Development Develop
- Analysis - needs analysis (including learning new technologies) 75 120
- Design Spec - Generate design documents 10 15
- Design Review - Design review (and colleagues reviewed the design documents) 5 10
- Coding Standard - code specification (to develop an appropriate specification for the current development) 5 2
- Design - specific design 30 30
- Coding - specific coding 60 180
- Code Review - Code Review 15 60
- Test - test (self-test, modify the code, submit modifications) 45 240
Reporting report
- Test Report - testing report 10 10
- Size Measurement - computational effort 5 5
- Postmortem & Process Improvement Plan - hindsight, and propose process improvement plan 10 15
total 280 697
  • Reflection
    • The actual coding time is about three times the plan
    • The main problem is that the first to write object-oriented programming with C ++, do not understand the various tools and tips VS platform, the scale of the problem is not the right judgments.
    • This also led to the initial idea behind a very big issue, after repeatedly changing ideas, modify the program
    • Wasted a lot of time, ultimate efficiency and correctness of the program are difficult to protect.

Idea

Problem-solving ideas

  1. Problem-solving ideas described. That is, after beginning to get the title, how to think, how to find information in the process. (3 ')
  • Problem-solving ideas divided into two parts
    1. Specific calculation method
    2. Calculate the optimal idea

Specific calculation method

  1. Seeking linear equation
  • The Line points, high school math recalled, with reference to Bowen after general formula using linear equations $ Ax + By + C = 0 $, and $ O (1) time complexity of the $ calculated linear equation
  1. Calculate the intersection
  • Intersection point calculation formula is also to be (1) derived in the time complexity $ O $
  • However, the new lines and calculate the intersection of the straight line to the preamble, is greatly influenced by the step of time complexity
  • It is easy to think of a violent solution:
    • Each new straight line n, the number of intersections is added $ n-1-N (number of parallel lines) -N (through the intersection of lines) $, while maintaining a parallel set of slope and the intersection of the existing collection
    • The time complexity of the algorithm is $ O (n ^ 2) $
  1. About the intersection of accuracy problems
  • Try a custom class to represent points

Code design

  1. Design and implementation process. Including how to design code organization, such as have several classes, several functions, how the relationship between them, whether key functions need to draw a flow chart? Unit testing is how design? (4 ')
  • The basic elements (class)
    • 点 Point
      • Constructor point Point (x, y)
      • Point Point relationship with the maintenance function (equal)
      • Straight intersection point belongs maintenance function (add, contains, size)
    • Line Line
      • Two straight constructor Line (point1, point2)
      • Analyzing the relationship between linear function (parallel, intersect, equal)
    • Linear collection lineSet
    • The intersection of the set interSet
    • Score calculation class Radio
      • Addition, subtraction, minimizing the numerator and denominator
  • Each time a new line is added flowchart
graph TD A [New straight configuration] -> B [intersect determination, and maintenance of intersection] B -> C [collection of intersecting lines obtained delSet] C -> D [rectilinear traversing the preamble of input other than delSet collection and maintenance of new cross-collection]

Performance Improvements

  1. Improving the performance of the program recording time spent on, you describe improvement ideas and show a performance analysis chart (VS generated automatically by a performance analysis tool of 2019), and show you consume the maximum program function. (3 ')
  • When programming, I faced two main problems, these two issues have a serious impact on the overall performance

    • Not familiar with C ++ code
    • No in-depth and accurate understanding of the issues when the idea
  • Especially the second question, led to the first serious run-time performance is low, eventually requiring multiple code refactoring,

  1. Radio-based fraction was originally designed, some of which strike the largest common multiple functions results in abnormally low performance program that runs only 1min straight lines 500+
  • Radio class wherein fraction occupied approximately 50% of CPU usage, and obtaining the greatest common divisor which is a function of the bulk
  • After the reference bit operation - the greatest common divisor efficient algorithm to optimize, but the final effect is mediocre, abandon the use of Radio employed to improve the accuracy class float

2Radio Effectiveness Analysis

  1. Program was originally designed with traversal, together with removal of parallel lines, the intersection of pruning, to improve program performance, but ultimately, when the initial design delSet design high CPU utilization
  • Traversing the intersection, but also look at the set of intersection of the straight line, causing the entire program effectiveness can only reach 1min600 + straight line, so deleting these two optimization
  • Reflection: Both methods can not improve performance, but I did not use better data structures and algorithms in the conception and preparation of the
  1. The final performance analysis chart
  • The largest program in consumption is a function of the insertion point of intersection to cross the main function of the collection insert operation, it makes me think I try to further optimize unordered_set, but unfortunately DDL imminent; followed by the consumption function is the main function of the former order traversal line group, which is to meet in our initial analysis

2 final efficacy analysis

Code review

  1. Code Description. Project demonstrated the key code and explain ideas and explanatory notes. (3 ')

Code Description

  • Top-down thinking of using to illustrate my code
graph TD A [intersection of the straight line and collective operation] -> B [straight intersection type] B -> C [ensure the accuracy of floating point numbers] B -> D [intersected by parallel computing]
  • main.cpp straight line in the intersection set operation
    • Linear traversing preamble set and adding a tail sequence may be employed such a linked list List Data Structure
    • Intersection set need to avoid duplication, but also to find a high speed, so the use of red and black tree data structure set (can also try to unordered_set hash index structure)
// 直线和交点集合
vector<Line> lineList;
set<Point> interSet;

// 遍历
for (i = 0; i < n; i++) {
		input >> c;
		if (c == 'L') {
			input >> x1 >> y1 >> x2 >> y2;
			Line l(x1, y1, x2, y2);
			
			for (auto iter = lineList.begin(); iter != lineList.end(); iter++) {
				Line lit = (Line)* iter;
				if (lit.isParallel(l)) {	
					continue;
				}
				// 交点计算和新增
				Point pInter = l.getIntersect(lit);
				interSet.insert(pInter);
			}
			lineList.push_back(l);
		}
	}
  • Graph.h & cpp class of points and lines
    • Which for set point ordering operator<& operator==heavy-duty, it is more important place
class Point
{
public:
	float x;
	float y;

	Point(float xNew, float yNew);
	bool equal(Point p);
	float getX();
	float getY();
	// 重载
	bool operator<(const Point& p) const {
		if (!EQFLOAT(x, p.x))
			return x < p.x;
		else
			return y < p.y;
	}
	bool operator==(const Point& p) const {
		return EQFLOAT(x, p.x) && EQFLOAT(y, p.y);
	}
	
private:
};
  • Line parameters, slope, if they contain a certain point, get the intersection, are important
class Line
{
public:

	Line(int x1, int y1, int x2, int y2);
	float getA();
	float getB();
	float getC();
	float getslope();
	bool isParallel(Line l);
	bool containsPoint(Point p);
	Point getIntersect(Line l);	
	bool equal(Line l);

private:
	// line: Ax + By + C = 0;
	float A;
  float B;
	float C;
	float slope;
};

// 计算直线参数极其斜率
Line::Line(int x1, int y1, int x2, int y2) {
	A = (float) y2 - y1;
	B = (float) x1 - x2;
	C = (float) x2 * y1 - x1 * y2;
	if (x1 - x2 == 0) {
		slope = FLT_MAX;
	}
	else {
		slope = (float)(y1 - y2) / (x1 - x2);
	}
}
  • In the judgment whether the intersection of two overlapping on the need to introduce precision floating-point number
#define EQS (1e-8)
#define EQFLOAT(a,b) (fabs((a) - (b)) < EQS)

bool Point::operator==(const Point& p) const {
		return EQFLOAT(x, p.x) && EQFLOAT(y, p.y);
	}
  • Whether the last straight line to obtain the intersection of the straight line parallel to judgment is the key
Point Line::getIntersect(Line l) {
	float a2 = l.getA();
	float b2 = l.getB();
	float c2 = l.getC();
	float x = (B * c2 - C * b2) / (A * b2 - B * a2);
	float y = (C * a2 - A * c2) / (A * b2 - B * a2);
	Point p(x, y);
	return p;
}

bool Line::containsPoint(Point p) {
	float res = A * p.getX() + B * p.getY() + C;
	return EQFLOAT(res, 0);
}

Testing and Debugging

  • unit test
    • Functional test coverage of the member functions of two classes Point and Line
		TEST_METHOD(TestMethodPoint1) {
			Point p(0.5, 3);
			Assert::AreEqual(p.getX()== 0.5, true);
			Assert::AreEqual(p.getY()==3, true);
			Point m(0.5, -3);
			Assert::AreNotEqual(p.equal(m), true);
		}

		TEST_METHOD(TestMethodLine1) {
			Line l1(0, 0, 1, 1);
			Line l2(0, 2, 1, 0);
			Line l3(0, -45, 45, 0);
			Line lr(1, 0, 5, 0);
			Line bt(1, 1, 1, 10);
			Assert::AreEqual(l2.getA()== -2, true);
			Assert::AreEqual(l2.getB()== -1, true);
			Assert::AreEqual(l2.getC()==2, true);
			Assert::AreEqual(l2.getslope()==-2, true);
			// parallel
			Assert::AreEqual(l1.isParallel(l3), true);
			Assert::AreEqual(lr.isParallel(bt), false);
			// containsPoint
			Point e(0.5,1);
			Point base(0,0);
			Assert::AreEqual(l2.containsPoint(e), true);
			Assert::AreNotEqual(l1.containsPoint(e), true);
			Assert::AreEqual(l1.containsPoint(base), true);
			// get intersect
			Point inter12((float)2/3,(float)2/3);
			Assert::AreEqual(l1.getIntersect(l2).equal(inter12), true);
			Point inter3lr(45, 0);
			Point inter3tb(1, -44);
			Assert::AreEqual(l3.getIntersect(lr).equal(inter3lr), true);
			// equal
			Assert::AreEqual(l1.equal(l2), false);
		}
  • Debugging aids
    • GeoGebra is a visual interface to sophisticated mathematical lightweight drawing tools, using it I found a bug aid program

2 graphical interface debug

  • Stress test coverage
    • Draws students randomly generated dot matrix code, generated a great deal of thousands of black-box sets the amount of data to aid test
    • But the problem is that the black box data overload, once to shoot inconsistency is difficult to find the problem.

Programming reflection

  • I almost wrote off the job for two days, due to the platform and are not familiar with C ++, and winter items are not too many software development tasks, so this time from the completion of work results and process point of view, are unsatisfactory. Software engineering as well as behind the heavy burden.

  • The following questions and record some areas for improvement

  • problem

    • Design documents which need to be refined to a fine-grained? What kind of work is needed to complete the code?
    • When writing code to change the design document is a preventable thing? How to avoid
  • Improve

    • Since the problem of programming ability, then I need to learn a variety of methods, while leaving more time for programming, in order to ensure the quality of work, such as this assignment, a day ahead of time if we can start writing, you should be improved a lot
    • C ++ need to be more familiar with the language, and VS IDE, developed after more than ensure efficiency

Guess you like

Origin www.cnblogs.com/yzy11235/p/12457866.html