1 #ifndef _global_h
2 #	include "global.h"
3 #endif
4 
5 #ifndef _vec2list_h
6 #	include "vec2list.h"
7 #endif
8 #ifndef _mat2_h
9 #	include "mat2.h"
10 #endif
11 
Vec2List(int len_in)12 Vec2List::Vec2List( int len_in ) {
13 	alloc_len = len_in;
14 	len = 0;
15 	v = new Vec2[alloc_len];
16 }
17 
Vec2List(const Vec2List & vl)18 Vec2List::Vec2List( const Vec2List &vl ) {
19 	v = 0;
20 	*this = vl;
21 }
22 
Vec2List(const Vec2List & vl,const Mat2 & m)23 Vec2List::Vec2List( const Vec2List &vl, const Mat2 &m ) {
24 	alloc_len = len = vl.Len();
25 	v = new Vec2[alloc_len];
26 	for (int i=0;i<len;i++) {
27 		v[i] = m*vl.v[i];
28 	}
29 }
30 
GetExtent(Vec2 * tl,Vec2 * br)31 void Vec2List::GetExtent( Vec2 *tl, Vec2 *br ) const {
32 	if (len) {
33 		Real	min_x(v[0].X());
34 		Real	max_x(v[0].X());
35 		Real	min_y(v[0].Y());
36 		Real	max_y(v[0].Y());
37 
38 		for (int i=1;i<len;i++) {
39 			if (v[i].X()<min_x)		min_x=v[i].X();
40 			if (v[i].X()>max_x)		max_x=v[i].X();
41 			if (v[i].Y()<min_y)		min_y=v[i].Y();
42 			if (v[i].Y()>max_y)		max_y=v[i].Y();
43 		}
44 		*tl = Vec2(min_x,min_y);
45 		*br = Vec2(max_x,max_y);
46 	}
47 	else {
48 		*tl = Vec2Zero;
49 		*br = Vec2Zero;
50 	}
51 }
52 
SetAt(int id,const Vec2 & z)53 const Vec2List& Vec2List::SetAt(int id,const Vec2 &z) {
54 	if (id>=0&&id<len) {
55 		v[id] = z;
56 	}
57 	return *this;
58 }
59 
AddAt(int id,const Vec2 & z)60 const Vec2List& Vec2List::AddAt(int id,const Vec2 &z) {
61 int i;
62 	if (len>=alloc_len) {
63 		alloc_len += 4;
64 		Vec2	*new_v = new Vec2[alloc_len];
65 		for (i=0;i<len;i++)		new_v[i] = v[i];
66 		delete v;
67 		v = new_v;
68 	}
69 	for (i=len-1;i>=id;i--)	v[i+1]=v[i];		// shift to the end
70 	len++;
71 	v[id] = z;
72 	return *this;
73 }
74 
Del(int id)75 const Vec2List& Vec2List::Del(int id) {
76 int i;
77 
78 	for (i=id;i<len-1;i++)	v[i]=v[i+1];		// shift to front
79 	len--;
80 	return *this;
81 }
82 
83 // deletes range of the polyline, include <from> but not include <to>
DelRange(int from,int to,int * start)84 const Vec2List& Vec2List::DelRange( int from, int to, int *start ) {
85 int i;
86 
87 	if (from<to) {
88 		for (i=to+1;i<len;i++)	v[from+i-to]=v[i];
89 		*start=from;
90 		len-=to-from;
91 		return *this;
92 	}
93 	else if (from>to) {
94 		// cut to end
95 		for (i=to+1;i<=from;i++)	v[i-to-1]=v[i];
96 		*start=from-to-1;
97 		len=from-to;
98 		return *this;
99 	}
100 	else {	/* from == to */
101 		*start=from;
102 		return *this;
103 	}
104 }
105 
106 const Vec2List& Vec2List::operator=(const Vec2List &vl) {
107 	if (v)	delete v;
108 	alloc_len = len = vl.Len();
109 	v = new Vec2[alloc_len];
110 	for (int i=0;i<len;i++) {
111 		v[i] = vl.v[i];
112 	}
113 	return *this;
114 }
115 
116 const Vec2List& Vec2List::operator|=(const Vec2& z)
117 {
118 	if (len>=alloc_len) {
119 		alloc_len += 4;
120 		Vec2	*new_v = new Vec2[alloc_len];
121 		for (int i=0;i<len;i++)		new_v[i] = v[i];
122 		delete v;
123 		v = new_v;
124 	}
125 	v[len++] = z;
126 	return *this;
127 }
128 
129 const Vec2List& Vec2List::operator+=(const Vec2& z2)
130 {
131 	for (int i=0;i<len;i++)		v[i]+=z2;
132 	return *this;
133 }
134 
135 const Vec2List& Vec2List::operator-=(const Vec2& z2)
136 {
137 	for (int i=0;i<len;i++)		v[i]-=z2;
138 	return *this;
139 }
140 
141 const Vec2List& Vec2List::operator*=(const Real& val)
142 {
143 	for (int i=0;i<len;i++)		v[i]*=val;
144 	return *this;
145 }
146 
147 const Vec2List& Vec2List::operator/=(const Real& val)
148 {
149 	for (int i=0;i<len;i++)		v[i]/=val;
150 	return *this;
151 }
152 
153 
154 const Vec2List& Vec2List::operator*=(const Mat2& m)
155 {
156 	for (int i=0;i<len;i++)		v[i]=m*v[i];
157 	return *this;
158 }
159 
160 int operator==(const Vec2List& z1, const Vec2List& z2)
161 {
162 int erg;
163 	if (z1.Len()!=z2.Len())		return 0;
164 	for (int i=0;i<z1.Len();i++) {
165 		erg = (z1.v[i]==z2.v[i]);
166 		if (!erg)			return erg;
167 	}
168 	return 1;
169 }
170 
TurnAngleRad(const Real & angle)171 void Vec2List::TurnAngleRad( const Real &angle )
172 {
173 	for (int i=0;i<len;i++) {
174 		v[i]=v[i].TurnAngleRad(angle);
175 	}
176 }
TurnRight()177 void Vec2List::TurnRight()
178 {
179 	for (int i=0;i<len;i++) {
180 		v[i]=v[i].TurnRight();
181 	}
182 }
TurnLeft()183 void Vec2List::TurnLeft()
184 {
185 	for (int i=0;i<len;i++) {
186 		v[i]=v[i].TurnLeft();
187 	}
188 }
189