Skip to content

Instantly share code, notes, and snippets.

@JakeCoxon
Created June 18, 2012 15:46
Show Gist options
  • Save JakeCoxon/2949013 to your computer and use it in GitHub Desktop.
Save JakeCoxon/2949013 to your computer and use it in GitHub Desktop.
Drawing straight lines
// This can be used for a basic line instead of making a texture yourself
Pixmap pixmap = new Pixmap(1, 1, Format.RGB565);
pixmap.setColor(1f, 1f, 1f, 1f);
pixmap.drawPixel(0, 0);
pixelTexture = new Texture(pixmap);
pixelTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
public void drawLine(SpriteBatch batch, float x1, float y1, float x2, float y2, float width) {
drawLine(batch, pixelTexture, x1, y1, x2, y2, width);
}
public void drawLine(SpriteBatch batch, TextureRegion region, float x1, float y1, float x2, float y2, float width) {
float dy = y2 - y1;
float dx = x2 - x1;
float angle = (float) Math.toDegrees(Math.atan2(dy, dx));
float length = (float) Math.sqrt(dx * dx + dy * dy);
if (region == null) throw new RuntimeException("Needs a line texture");
batch.draw(region, x1, y1-width/2, 0f, width/2, length, width, 1f, 1f, angle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment