Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include "BrokenLine.h"
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. BrokenLine::BrokenLine(const BrokenLine &obj) : length(obj.length), cap(obj.cap) {
  6. Point *a = new Point[cap];
  7. for (int i = 0; i < cap; i++)
  8. a[i] = obj.points[i];
  9. delete [] points;
  10. points = a;
  11. }
  12.  
  13. double BrokenLine::dist(Point x, Point y) {
  14. return sqrt(pow(y.x - x.x, 2) + pow(y.y - x.y, 2));
  15. }
  16.  
  17. int BrokenLine::size() { // получение количества точек
  18. return length;
  19. }
  20.  
  21. Point& BrokenLine::getPoint(int i) {
  22. return points[i];
  23. }
  24.  
  25. void BrokenLine::add(Point x) {
  26. if (cap == length) {
  27. cap *= 2;
  28. Point *a = new Point[cap];
  29. for (int i = 0; i < length; i++)
  30. a[i] = points[i];
  31. a[length++] = x;
  32. delete [] points;
  33. points = a;
  34. }
  35. else
  36. points[length++] = x;
  37. }
  38.  
  39. void BrokenLine::split(double dis) { //разбиение отрезков пополам
  40. int j = 0;
  41. if (cap < length * 2)
  42. cap *= 2;
  43. Point *buff = new Point[cap];
  44. if (size() != 0)
  45. buff[j++] = points[0];
  46. //buff[0] = points[0];
  47. j = 1;
  48. for (int i = 1; i < length; i++) {
  49. if (dist(points[i], points[i - 1]) > dis)
  50. buff[j++] = Point((points[i].x + points[i - 1].x)/2, (points[i].y + points[i - 1].y)/2);
  51. buff[j++] = points[i];
  52. }
  53. if (dist(points[0], points[size() - 1]) > dis)
  54. buff[j++] = Point((points[0].x + points[size() - 1].x)/2, (points[0].y + points[size() - 1].y)/2);
  55. delete [] points;
  56. points = buff;
  57. length = j;
  58. }
  59.  
  60. BrokenLine::~BrokenLine() {
  61. delete [] points;
  62. }
  63.  
  64. BrokenLine& BrokenLine::operator= (const BrokenLine &obj) {
  65. if (this != &obj) {
  66. Point *new_a = new Point[obj.cap];
  67. for (int i = 0; i < obj.cap; i++)
  68. new_a[i] = obj.points[i];
  69. delete [] points;
  70. cap = obj.cap;
  71. length = obj.length;
  72. points = new_a;
  73. }
  74. return *this;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement