You're here: Snippet Directory » C/C++ (495)
Language:

Interpolation class

Language: English
Programming Language: C++
Published by: oscarp [not registered]
Last Update: 5/15/2006
Views: 150


Description

This pair of classes can be used to interpolate points between a given range. You simply have to call "interpole" method with adecuate parameters and it will return a linked list of points to interpolate the two given.

Code

1 *************************************************************************** 2 interpolator.h - description 3 ------------------- 4 begin : Mon Sep 10 2001 5 copyright : (C) 2001 by ?car P?ez 6 email : os_carp@terra.es 7 ***************************************************************************/ 8 9 /*************************************************************************** 10 * * 11 * This program is free software; you can redistribute it and/or modify * 12 * it under the terms of the GNU General Public License as published by * 13 * the Free Software Foundation; either version 2 of the License, or * 14 * (at your option) any later version. * 15 * * 16 ***************************************************************************/ 17 18 #ifndef INTERPOLATOR_H 19 #define INTERPOLATOR_H 20 21 22 /**This class is a point interpolator 23 *@author ?car P?ez 24 */ 25 26 #include "stdlib.h" 27 28 29 class PointItem 30 { 31 public: 32 PointItem(); 33 ~PointItem(); 34 public: 35 int x(void); 36 int y(void); 37 PointItem *next(void); 38 void setX(int x); 39 void setY(int y); 40 void setNext(PointItem *n); 41 private: 42 int xValue; 43 int yValue; 44 PointItem *nextPoint; 45 }; 46 47 class Interpolator 48 { 49 public: 50 Interpolator(); 51 ~Interpolator(); 52 53 public: 54 /** To interpolate between two points, simply call this 55 * method with the coordinates of both points. 56 * It returns the list of points needed to interpolate them. 57 * This method allocates memory, so after usage, you should 58 * call 'freePointList()' with the result of this method to free 59 * allocated memory. 60 * PointItem is a linked list where the last item has it's 'next' pointer 61 * set to null. 62 */ 63 PointItem *interpole(int x1, int y1, int x2, int y2); 64 65 /** This method frees all memory used by list. 66 */ 67 void freePointList(PointItem *list); 68 69 70 private: 71 PointItem *interpolateNormal(int x1,int x2, float m, float b); 72 PointItem *interpolateVertical(int x1, int y1,int y2); 73 PointItem *interpolateInverse(int y1,int y2, float m, float b); 74 PointItem *interpolateHorizontal(int x1, int y1, int x2); 75 }; 76 77 #endif 78 79 80 81 /************************************************************************** 82 *************************************************************************** 83 now the code (.cpp) 84 85 ****************************************************************************/ 86 87 /*************************************************************************** 88 interpolator.cpp - description 89 ------------------- 90 begin : Mon Sep 10 2001 91 copyright : (C) 2001 by ?car P?ez 92 email : os_carp@terra.es 93 ***************************************************************************/ 94 95 /*************************************************************************** 96 * * 97 * This program is free software; you can redistribute it and/or modify * 98 * it under the terms of the GNU General Public License as published by * 99 * the Free Software Foundation; either version 2 of the License, or * 100 * (at your option) any later version. * 101 * * 102 ***************************************************************************/ 103 104 #include "interpolator.h" 105 106 Interpolator::Interpolator() 107 { 108 } 109 110 /***************************************************************/ 111 112 Interpolator::~Interpolator() 113 { 114 } 115 116 /***************************************************************/ 117 118 PointItem *Interpolator::interpole(int x1, int y1, int x2, int y2) 119 { 120 //We simply use a linear interpolation y=m*a+b 121 int deltaX; 122 int deltaY; 123 PointItem *list; 124 float m; 125 float b; 126 127 deltaX = abs(x2-x1); 128 deltaY = abs(y2-y1); 129 if (deltaX>deltaY) 130 { 131 if (x2-x1!=0) 132 { 133 if (y2-y1!=0) 134 { 135 m = ((float)(y2-y1))/((float)(x2-x1)); 136 b = y1-m*x1; 137 list = interpolateNormal(x1,x2,m,b); 138 } 139 else 140 { 141 //It's horizontal 142 list = interpolateHorizontal(x1,y1,x2); 143 } 144 } 145 else 146 { 147 //It's a vertical line 148 list = interpolateVertical(x1,y1,y2); 149 } 150 } 151 else 152 { 153 if (y2-y1!=0) 154 { 155 if (x2-x1!=0) 156 { 157 m = ((float)(x2-x1))/((float)(y2-y1)); 158 b = x1-m*y1; 159 list = interpolateInverse(y1,y2,m,b); 160 } 161 else 162 { 163 //It's a vertical line 164 list = interpolateVertical(x1,y1,y2); 165 } 166 } 167 else 168 { 169 //It's horizontal 170 list = interpolateHorizontal(x1,y1,x2); 171 } 172 } 173 return(list); 174 } 175 176 /***************************************************************/ 177 178 PointItem *Interpolator::interpolateNormal(int x1, int x2, float m, float b) 179 { 180 int i; 181 int y; 182 PointItem *list; 183 PointItem *actual; 184 PointItem *previous; 185 186 previous = NULL; 187 list = NULL; 188 if (x1>x2) 189 { 190 i=x1; 191 x1 = x2; 192 x2 = i; 193 } 194 for (i=x1;i<=x2;i++) 195 { 196 actual = new PointItem(); 197 actual->setX(i); 198 y = (int)(m*i+b); 199 actual->setY(y); 200 if (previous!=NULL) 201 { 202 previous->setNext(actual); 203 } 204 previous = actual; 205 if (list==NULL) 206 { 207 list=actual; 208 } 209 } 210 return(list); 211 } 212 213 /***************************************************************/ 214 215 PointItem *Interpolator::interpolateVertical(int x1, int y1, int y2) 216 { 217 int i; 218 PointItem *list; 219 PointItem *actual; 220 PointItem *previous; 221 222 previous = NULL; 223 list = NULL; 224 if (y1>y2) 225 { 226 i = y1; 227 y1 = y2; 228 y2 = i; 229 } 230 for (i=y1;i<=y2;i++) 231 { 232 actual = new PointItem(); 233 actual->setX(x1); 234 actual->setY(i); 235 if (previous!=NULL) 236 { 237 previous->setNext(actual); 238 } 239 previous = actual; 240 if (list==NULL) 241 { 242 list=actual; 243 } 244 } 245 return(list); 246 } 247 248 /***************************************************************/ 249 250 PointItem *Interpolator::interpolateInverse(int y1,int y2, float m, float b) 251 { 252 int i; 253 int x; 254 PointItem *list; 255 PointItem *actual; 256 PointItem *previous; 257 258 previous = NULL; 259 list = NULL; 260 if (y1>y2) 261 { 262 x = y1; 263 y1=y2; 264 y2=x; 265 } 266 for (i=y1;i<=y2;i++) 267 { 268 actual = new PointItem(); 269 actual->setY(i); 270 x = (int)(m*i+b); 271 actual->setX(x); 272 if (previous!=NULL) 273 { 274 previous->setNext(actual); 275 } 276 previous = actual; 277 if (list==NULL) 278 { 279 list=actual; 280 } 281 } 282 return(list); 283 } 284 285 /***************************************************************/ 286 287 PointItem *Interpolator::interpolateHorizontal(int x1, int y1, int x2) 288 { 289 int i; 290 PointItem *list; 291 PointItem *actual; 292 PointItem *previous; 293 294 previous = NULL; 295 list = NULL; 296 if (x1>x2) 297 { 298 i = x1; 299 x1=x2; 300 x2=i; 301 } 302 for (i=x1;i<=x2;i++) 303 { 304 actual = new PointItem(); 305 actual->setX(i); 306 actual->setY(y1); 307 if (previous!=NULL) 308 { 309 previous->setNext(actual); 310 } 311 previous = actual; 312 if (list==NULL) 313 { 314 list=actual; 315 } 316 } 317 return(list); 318 } 319 320 /***************************************************************/ 321 322 void Interpolator::freePointList(PointItem *list) 323 { 324 PointItem *actual; 325 while (list!=NULL) 326 { 327 actual = list->next(); 328 delete(list); 329 list=actual; 330 } 331 } 332 333 334 /***************************************************************/ 335 /***************************************************************/ 336 /***************************************************************/ 337 /***************************************************************/ 338 PointItem::PointItem() 339 { 340 nextPoint = NULL; 341 } 342 343 /***************************************************************/ 344 345 PointItem::~PointItem() 346 { 347 } 348 349 /***************************************************************/ 350 351 int PointItem::x(void) 352 { 353 return(xValue); 354 } 355 356 /***************************************************************/ 357 358 int PointItem::y(void) 359 { 360 return(yValue); 361 } 362 363 /***************************************************************/ 364 365 PointItem *PointItem::next(void) 366 { 367 return(nextPoint); 368 } 369 370 /***************************************************************/ 371 372 void PointItem::setX(int x) 373 { 374 xValue = x; 375 } 376 377 /***************************************************************/ 378 379 void PointItem::setY(int y) 380 { 381 yValue = y; 382 } 383 384 /***************************************************************/ 385 386 void PointItem::setNext(PointItem *n) 387 { 388 nextPoint = n; 389 } 390 391 392 393 394

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS