Files Classes Functions Hierarchy
00001 00002 #include <cassert> 00003 using namespace std; 00004 00005 #include <point.h> 00006 00007 #include <ruler.h> 00008 00009 #define PI 3.14159265359 00010 00011 void ruler::addAngleRuler 00012 ( 00013 doublec radius, 00014 doublec angle0, 00015 doublec angle1, 00016 doublec ticklenmajor, 00017 doublec ticklenmiddle, 00018 doublec ticklenminor 00019 ) 00020 { 00021 assert(angle0 >= 0.0); 00022 assert(angle0 < angle1); 00023 00024 gobjContainer * c = new gobjContainer(); 00025 00026 c->push( new gobjglBegin(GL_LINES) ); 00027 00028 doublec degtorad = PI / 180.0; 00029 double t; 00030 00031 for (double s=angle0; s<=angle1; ) 00032 { 00033 t = s * degtorad; 00034 //point2<double> const w(radius*cos(t),radus*sin(t)); //Save on trig calculations. 00035 point2<double> const nm(-cos(t),-sin(t)); // The normal 00036 point2<double> const w(-nm.x*radius,-nm.y*radius); // Point on curve. 00037 00038 c->push( new gobjglVertex2f(w) ); 00039 c->push( new gobjglVertex2f(w + nm * ticklenmajor) ); 00040 s += 10.0; 00041 } 00042 00043 for (double s=angle0; s<=angle1; ) 00044 { 00045 t = s * degtorad; 00046 point2<double> const nm(-cos(t),-sin(t)); // The normal 00047 point2<double> const w(-nm.x*radius,-nm.y*radius); // Point on curve. 00048 00049 c->push( new gobjglVertex2f(w) ); 00050 c->push( new gobjglVertex2f(w + nm * ticklenmiddle) ); 00051 s += 5.0; 00052 } 00053 00054 for (double s=angle0; s<=angle1; ) 00055 { 00056 t = s * degtorad; 00057 point2<double> const nm(-cos(t),-sin(t)); // The normal 00058 point2<double> const w(-nm.x*radius,-nm.y*radius); // Point on curve. 00059 00060 c->push( new gobjglVertex2f(w) ); 00061 c->push( new gobjglVertex2f(w + nm * ticklenminor) ); 00062 s += 1.0; 00063 } 00064 00065 00066 c->push(new gobjglEnd()); 00067 00068 push(c); 00069 } 00070 00071 00072 void ruler::addStraightRuler 00073 ( 00074 doublec len, 00075 point2<double> const & pos0, 00076 point2<double> const & pos1, 00077 doublec ticklenmajor, 00078 doublec ticklenmiddle, 00079 doublec ticklenminor 00080 ) 00081 { 00082 uintc n = (uintc)(floor(len)); 00083 00084 //doublec dt = 1.0 / ((double)n - 1.0); 00085 doublec dt = 1.0 / len; 00086 00087 point2<double> const p2(pos1-pos0); 00088 00089 point2<double> tmp(-p2.y,p2.x); 00090 tmp.normalize(); 00091 point2<double> const nm(tmp); 00092 00093 gobjContainer * c = new gobjContainer(); 00094 00095 c->push( new gobjglBegin(GL_LINES) ); 00096 00097 for (uint i=0; i<n; ++i) 00098 { 00099 point2<double> w(pos0 + p2*(dt*i)); 00100 00101 // Write the major ticks. 00102 c->push( new gobjglVertex2f(w) ); 00103 c->push( new gobjglVertex2f(w + nm * ticklenmajor) ); 00104 00105 // Write the middle ticks. 00106 point2<double> w2(pos0 + p2*(dt*(i+0.5))); 00107 c->push( new gobjglVertex2f(w2) ); 00108 c->push( new gobjglVertex2f(w2 + nm * ticklenmiddle) ); 00109 00110 // Write the minor ticks 00111 for (uint k=1; k<=4; ++k) 00112 { 00113 point2<double> w3(pos0 + p2*(dt*(i+k*0.1))); 00114 c->push( new gobjglVertex2f(w3) ); 00115 c->push( new gobjglVertex2f(w3 + nm * ticklenminor) ); 00116 } 00117 for (uint k=1; k<=4; ++k) 00118 { 00119 point2<double> w3(pos0 + p2*(dt*(i+0.5+k*0.1))); 00120 c->push( new gobjglVertex2f(w3) ); 00121 c->push( new gobjglVertex2f(w3 + nm * ticklenminor) ); 00122 } 00123 } 00124 00125 // Write the last major tick. 00126 if (len>1.0) 00127 { 00128 point2<double> w(pos0 + p2*(dt*n)); 00129 c->push( new gobjglVertex2f(w) ); 00130 c->push( new gobjglVertex2f(w + nm * ticklenmajor) ); 00131 } 00132 00133 // Write the last middle tick 00134 if ((len-n)>=0.5) 00135 { 00136 point2<double> w(pos0 + p2*(dt*(n+0.5))); 00137 c->push( new gobjglVertex2f(w) ); 00138 c->push( new gobjglVertex2f(w + nm * ticklenmiddle) ); 00139 } 00140 00141 // The following suggests an easier way of writing this routine. 00142 // Instead of separating into cases, just write all the minor ticks 00143 // at once, then the middle and major ticks, overwriting the previous ones. 00144 // While it writes unnecessary lines its logic and code is much less. 00145 00146 if ((len-n)>0.0) 00147 { 00148 for (double s=0.0, s1=len-n; s<=s1; ) 00149 { 00150 point2<double> w(pos0 + p2*(dt*(n+s))); 00151 c->push( new gobjglVertex2f(w) ); 00152 c->push( new gobjglVertex2f(w + nm * ticklenminor) ); 00153 s += 0.1; 00154 } 00155 } 00156 00157 c->push(new gobjglEnd()); 00158 00159 push(c); 00160 } 00161 00162 00163 00164 00165
1.5.8