Bài tập C++ OOP: tạo lớp Point và PolyLine | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create a Polyline class in PolyLine.cpp file and a Point class in Point.cpp file like the following class diagram:

To make it easier to understand the above class diagram, you need to understand what PolyLine is. PolyLine is a set of straight lines created by connecting two consecutive points together. For example, a PolyLine with coordinates of points [(0, 1), (1, 2), (1, 0), (2, 3), (3, 1), (4, 2)] looks like as follows:

Explain the class diagram:

Point class

  • This class is used for storing information about a point in a two-dimensional coordinate system.
  • x and y attributes describe the abscissa and ordinate of a point.
  • Point(x: int, y: int) is the parameterized constructor used for initializing values to x and y.
  • setX(), getX(), setY(), getY() are setter and getter.

PolyLine class

  • points is an attribute that stores information about points in PolyLine, the data type of this attribute is List
  • PolyLine() is a parameterized constructor used for initializing an empty ArrayList for points attribute.
  • PolyLine(points: List<Point) is a parameterized constructor which is used for initializing values to ponits.
  • appendPoint(point: Point) and appendPoint(x: int, y: int) methods are used for adding a point to the end of points attribute.
  • getLength() method is used for getting the length of PolyLine. For example, if points = [(1, 1), (2, 3), (3, 0), (4, 2)] (see the illustration below),

    the getLength() method returns the following result 7.63441361516796.

A program to test PolyLine class:

#include <iostream>

using namespace std;

#include "Point.cpp"
#include "PolyLine.cpp"

int main() {
	PolyLine polyLine;
	polyLine.appendPoint(Point(1, 1));
	polyLine.appendPoint(Point(2, 3));
	polyLine.appendPoint(3, 0);
	polyLine.appendPoint(4, 2);
	cout << polyLine.getLength();
	return 0;
}

When the above code is compiled and executed, it produces the following result:

7.63441

Note: CodeLearn accepts numerical error less than 0.000001

Instruction

Code sample:

File Point.cpp

#pragma once

class Point {
private:
	int x;
	int y;

public:
	Point() {
		
	}

	Point(int x, int y) {
		this->x = x;
		this->y = y;
	}

	int getX() {
		return x;
	}

	void setX(int x) {
		this->x = x;
	}

	int getY() {
		return y;
	}

	void setY(int y) {
		this->y = y;
	}
};

File PolyLine.cpp

#pragma once

#include <vector>
#include <math.h>
#include "Point.cpp"

using namespace std;

class PolyLine {
private:
	vector<Point> points;

public:
	PolyLine() {
	}

	PolyLine(vector<Point> points) {
		this->points = points;
	}

	void appendPoint(Point point) {
		points.push_back(point);
	}

	void appendPoint(int x, int y) {
		points.push_back(Point(x, y));
	}

	double getLength() {
		double length = 0;
		for (int i = 0; i < points.size() - 1; i++) {
			int x1 = points[i].getX();
			int y1 = points[i].getY();
			int x2 = points[i + 1].getX();
			int y2 = points[i + 1].getY();
			double distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
			length += distance;
		}
		return length;
	}
};