summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/Z-Uebung/Teil2/shape.cpp
blob: 87c75fea8a79ede77bdde8b726f326b62d6949b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// shape.cpp

// Implementation of classes Shape and Polyline. 



#include "shape.h"



// class Shape 

// all methods inline



// class Polyline

Polyline::Polyline( Point arr[], int n )

{

   arrPoints = NULL;

   nLines = 0;

   if( n > 0 ) {

      anchor = arr[ 0 ];  // anchor

      if( n > 1 ) {

        nLines = n - 1;

        // save points relatively to anchor

        arrPoints = new Point[ nLines ];

        for( int i = 0; i < n-1; ++i )

           arrPoints[ i ] = arr[ i + 1 ] - anchor;  

      }

   }

}



Polyline::Polyline( const Polyline& src )  // copy constructor

{

   nLines = src.nLines;

   anchor = src.anchor;

   arrPoints = NULL;

   

   if( src.nLines > 0) {

      arrPoints = new Point[ nLines ];

      for( int i = 0; i < nLines; ++i )

         arrPoints[ i ] = src.arrPoints[ i ];  

   }

}



Polyline& Polyline::operator=( const Polyline& src )  // assignment

{

   if( this != &src) {

      delete [] arrPoints;

      nLines = src.nLines;

      anchor = src.anchor;

      arrPoints = NULL;

      if( src.nLines > 0 ) {

         arrPoints = new Point[ nLines ];

         for( int i = 0; i < nLines; ++i )

            arrPoints[ i ] = src.arrPoints[ i ];  

      }

   }

   return *this;

}



double Polyline::getLength() const

{

   double len = 0.0;

   if( nLines > 0 ) {

      Point begin( 0, 0 );

      for( int i = 0; i < nLines; ++i ) {

         len += begin.distance( arrPoints[ i ] );

         begin = arrPoints[ i ];

      }

   }

   return len;

}



// append one more point:

Polyline& Polyline::operator+=( const Point& p )

{

   Point* ptr = new Point[ nLines + 1 ];  // new array

   for( int i = 0 ; i < nLines; ++i )  // copy points into new array

      ptr[ i ] = arrPoints[ i ];

   ptr[ nLines ] = p - anchor;  // add new point

   ++nLines;

   delete [] arrPoints;  // free old array

   arrPoints = ptr;

   return *this;

}



// append a second line:

Polyline& Polyline::operator+=( const Polyline& src )

{

   if( src.nLines > 0 ) {

      Point last = Point( 0, 0 );  // rel. coordinates of last point

      if( nLines > 0 )

         last = arrPoints[ nLines - 1 ];

      Point* ptr = new Point[ nLines + src.nLines ];  // new array

      int i;

      for( i = 0; i < nLines; ++i )  // copy points into new array

         ptr[ i ] = arrPoints[ i ];   

      for( i = 0; i < src.nLines; ++i )  // append points of src

         ptr[ nLines + i ] = last + src.arrPoints[ i ];



      nLines += src.nLines;

      delete [] arrPoints;  // free old array

      arrPoints = ptr;

   }

   return *this;

}



void Polyline::scale( double scalingFactor )

{

   for( int i=0; i < nLines; ++i )

      arrPoints[ i ] *= scalingFactor;

}



string Polyline::toString() const  // points of line

{

   string str = anchor.toString();

   for( int i = 0; i < nLines; ++i )

      str += "  ",  str += ( anchor + arrPoints[ i ] ).toString();

   return str;

}