Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BrokenLine.h"
- #include <cmath>
- #include <iostream>
- BrokenLine::BrokenLine(const BrokenLine &obj) : length(obj.length), cap(obj.cap) {
- Point *a = new Point[cap];
- for (int i = 0; i < cap; i++)
- a[i] = obj.points[i];
- delete [] points;
- points = a;
- }
- double BrokenLine::dist(Point x, Point y) {
- return sqrt(pow(y.x - x.x, 2) + pow(y.y - x.y, 2));
- }
- int BrokenLine::size() { // получение количества точек
- return length;
- }
- Point& BrokenLine::getPoint(int i) {
- return points[i];
- }
- void BrokenLine::add(Point x) {
- if (cap == length) {
- cap *= 2;
- Point *a = new Point[cap];
- for (int i = 0; i < length; i++)
- a[i] = points[i];
- a[length++] = x;
- delete [] points;
- points = a;
- }
- else
- points[length++] = x;
- }
- void BrokenLine::split(double dis) { //разбиение отрезков пополам
- int j = 0;
- if (cap < length * 2)
- cap *= 2;
- Point *buff = new Point[cap];
- if (size() != 0)
- buff[j++] = points[0];
- //buff[0] = points[0];
- j = 1;
- for (int i = 1; i < length; i++) {
- if (dist(points[i], points[i - 1]) > dis)
- buff[j++] = Point((points[i].x + points[i - 1].x)/2, (points[i].y + points[i - 1].y)/2);
- buff[j++] = points[i];
- }
- if (dist(points[0], points[size() - 1]) > dis)
- buff[j++] = Point((points[0].x + points[size() - 1].x)/2, (points[0].y + points[size() - 1].y)/2);
- delete [] points;
- points = buff;
- length = j;
- }
- BrokenLine::~BrokenLine() {
- delete [] points;
- }
- BrokenLine& BrokenLine::operator= (const BrokenLine &obj) {
- if (this != &obj) {
- Point *new_a = new Point[obj.cap];
- for (int i = 0; i < obj.cap; i++)
- new_a[i] = obj.points[i];
- delete [] points;
- cap = obj.cap;
- length = obj.length;
- points = new_a;
- }
- return *this;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement