Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turn a line into a rectangle

I have a method that draws a line between two points. This works pretty well, but now I want to make this line into a rectangle.

How can I get the points on the left and right side of each of the line points to make it into a rectangle that I can draw?

It is almost as though I need to somehow figure out how to get perpendicular lines programatically....

like image 220
Mel Avatar asked Feb 04 '23 07:02

Mel


1 Answers

I'm guessing you basically want fat lines? Lets assume the line is specified by two points (x0, y0) and (x1, y1) we then have:

float dx = x1 - x0; //delta x
float dy = y1 - y0; //delta y
float linelength = sqrtf(dx * dx + dy * dy);
dx /= linelength;
dy /= linelength;
//Ok, (dx, dy) is now a unit vector pointing in the direction of the line
//A perpendicular vector is given by (-dy, dx)
const float thickness = 5.0f; //Some number
const float px = 0.5f * thickness * (-dy); //perpendicular vector with lenght thickness * 0.5
const float py = 0.5f * thickness * dx;
glBegin(GL_QUADS);
glVertex2f(x0 + px, y0 + py);
glVertex2f(x1 + px, y1 + py);
glVertex2f(x1 - px, y1 - py);
glVertex2f(x0 - px, y0 - py);
glEnd();

Since you're using OpenGL ES I guess you'll have to convert the immediate mode rendering (glBegin, glEnd, etc) to glDrawElements. You'll also have to convert the quad into two triangles.

One final thing, I'm a little tired and uncertain if the resulting quad is counterclockwise or clockwise so turn of backface culling when you try this out (glDisable(GL_CULL)).

like image 117
Andreas Brinck Avatar answered Feb 08 '23 16:02

Andreas Brinck