Drawing a line between two points is a perfect example of a function which most graphics libraries implement at a single scale: 1 pixel width. Here’s how to implement drawing a line with a specific weight (thickness) in JOGL:
Image may be NSFW.
Clik here to view.The line (shown by the dotted line rectangle) will be drawn between (x1,y1) and (x2,y2) with a weight of 2*f:
public void line(double x1, double y1, double x2, double y2, double weight) {
gl.glColor3d(0, 0, 0);
if (y1 == y2) {
gl.glBegin(GL.GL_QUADS);
y1 -= weight / 2;
y2 = y1 + weight;
gl.glVertex2d(x1, y1);
gl.glVertex2d(x1, y2);
gl.glVertex2d(x2, y2);
gl.glVertex2d(x2, y1);
gl.glEnd();
} else if (x1 == x2) {
gl.glBegin(GL.GL_QUADS);
x1 -= weight / 2;
x2 = x1 + weight;
gl.glVertex2d(x1, y1);
gl.glVertex2d(x1, y2);
gl.glVertex2d(x2, y2);
gl.glVertex2d(x2, y1);
gl.glEnd();
} else {
double a = x2 - x1;
double b = y2 - y1;
double c = Math.sqrt(a * a + b * b);
double ratio = weight/2/c;
double d = a * ratio;
double e = b * ratio;
double xa = x1 + e;
double ya = y1 - d;
double xb = x1 - e;
double yb = y1 + d;
double xc = x2 - e;
double yc = y2 + d;
double xd = x2 + e;
double yd = y2 - d;
gl.glBegin(GL.GL_QUADS);
gl.glVertex2d(xa, ya);
gl.glVertex2d(xb, yb);
gl.glVertex2d(xc, yc);
gl.glVertex2d(xd, yd);
gl.glEnd();
}
}
This function is implemented in IGF in this file and tested in this file, which draws the following image using both an OpenGL and Java implementation:
Image may be NSFW.
Clik here to view.