Bresenham.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A class to realise a uniform n:m - relationship using the
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <iostream>
00031 #include "Bresenham.h"
00032 
00033 #ifdef CHECK_MEMORY_LEAKS
00034 #include <foreign/nvwa/debug_new.h>
00035 #endif // CHECK_MEMORY_LEAKS
00036 
00037 
00038 // ===========================================================================
00039 // method definitions
00040 // ===========================================================================
00041 void
00042 Bresenham::compute(BresenhamCallBack *callBack, SUMOReal val1, SUMOReal val2) {
00043     // case1: both numbers are equal
00044     if (val1==val2) {
00045         for (SUMOReal step=0; step<val1; step++) {
00046             callBack->execute(step, step);
00047         }
00048         return;
00049     }
00050     // case2: the first value is higher
00051     if (val1>val2) {
00052         SUMOReal pos = 0;
00053         SUMOReal prop = val2 / val1;
00054         SUMOReal cnt = prop / 2;
00055         for (SUMOReal i=0; i<val1; i++) {
00056             callBack->execute(i, pos);
00057             cnt += prop;
00058             if (cnt>=1.0) {
00059                 pos++;
00060                 cnt -= 1.0;
00061             }
00062         }
00063         return;
00064     }
00065     // case3: the first value is smaller than the second
00066     if (val1<val2) {
00067         SUMOReal pos = 0;
00068         SUMOReal prop = val1 / val2;
00069         SUMOReal cnt = prop / 2;
00070         for (SUMOReal i=0; i<val2; i++) {
00071             callBack->execute(pos, i);
00072             cnt += prop;
00073             if (cnt>=1.0) {
00074                 pos++;
00075                 cnt -= 1.0;
00076             }
00077         }
00078         return;
00079     }
00080 }
00081 
00082 
00083 
00084 /****************************************************************************/
00085 

Generated on Wed May 5 00:06:28 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6