I use the algorithm below to generate squares that are then rendered to make a circuit like this
http://img810.imageshack.us/img810/8530/uhohz.png
The problem, as can be seen in the image, is that sometimes the lines are too thin when they should always be the same width. My algorithm finds 4 vertices for the first, then the top 2 vertices of the next are the bottom 2 of the previous one. This creates connected lines, but does not seem to always work. How can i fix this?
This is my algorithm:
void OGLENGINEFUNCTIONS::GenerateLinePoly(const std::vector<std::vector<GLdouble>> &input, std::vector<GLfloat> &output, int width) { output.clear(); if(input.size() < 2) { return; } int temp; float dirlen; float perplen; POINTFLOAT start; POINTFLOAT end; POINTFLOAT dir; POINTFLOAT ndir; POINTFLOAT perp; POINTFLOAT nperp; POINTFLOAT perpoffset; POINTFLOAT diroffset; POINTFLOAT p0, p1, p2, p3; for(unsigned int i = 0; i < input.size() - 1; ++i) { start.x = static_cast<float>(input[i][0]); start.y = static_cast<float>(input[i][1]); end.x = static_cast<float>(input[i + 1][0]); end.y = static_cast<float>(input[i + 1][1]); dir.x = end.x - start.x; dir.y = end.y - start.y; dirlen = sqrt((dir.x * dir.x) + (dir.y * dir.y)); ndir.x = static_cast<float>(dir.x * 1.0 / dirlen); ndir.y = static_cast<float>(dir.y * 1.0 / dirlen); perp.x = dir.y; perp.y = -dir.x; perplen = sqrt((perp.x * perp.x) + (perp.y * perp.y)); nperp.x = static_cast<float>(perp.x * 1.0 / perplen); nperp.y = static_cast<float>(perp.y * 1.0 / perplen); perpoffset.x = static_cast<float>(nperp.x * width * 0.5); perpoffset.y = static_cast<float>(nperp.y * width * 0.5); diroffset.x = static_cast<float>(ndir.x * 0 * 0.5); diroffset.y = static_cast<float>(ndir.y * 0 * 0.5);
thanks
Edit:
POINTFLOAT multiply(const POINTFLOAT &a, float b) { POINTFLOAT result; result.x = ax * b; result.y = ay * b; return result; } POINTFLOAT normalize(const POINTFLOAT &a) { return multiply(a, 1.0f/sqrt(ax*a.x+ay*ay)); } POINTFLOAT slerp2d( const POINTFLOAT v0, const POINTFLOAT v1, float t ) { float dot = (v0.x * v1.x + v1.y * v1.y); if( dot < -1.0f ) dot = -1.0f; if( dot > 1.0f ) dot = 1.0f; float theta_0 = acos( dot ); float theta = theta_0 * t; POINTFLOAT v2; v2.x = -v0.y; v2.y = v0.x; POINTFLOAT result; result.x = v0.x * cos(theta) + v2.x * sin(theta); result.y = v0.y * cos(theta) + v2.y * sin(theta); return result; } void OGLENGINEFUNCTIONS::GenerateLinePoly(const std::vector<std::vector<GLdouble> > &input, std::vector<GLfloat> &output, int width) { output.clear(); if(input.size() < 2) { return; } float w = width / 2.0f;
c ++ c algorithm graphics opengl
jmasterx
source share