Home » Android » android – Draw a smooth line

android – Draw a smooth line

Posted by: admin May 14, 2020 Leave a comment


I am developing a game using libgdx and i want to draw a smooth line using shape renderer.


I have tried Multi Sample anti aliasing from libgdx blog.
I have also went through Anti aliased filed shape in libgdx
but unfortunately these line is not in latest verson of libgdx.

How to&Answers:

Enable anti-alising in the configuration:

For Desktop:

    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.samples = 2;
    new LwjglApplication(new MyGdxGame(Helper.arrayList(arg)), config);

For Android:

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.numSamples = 2;
    initialize(new MyGdxGame(null), config);

Or you could create a white pixmap of 1×1 and use it to create a sprite and draw the line using that sprite, I personally prefer this method istead of ShapeRenderer (note that there is no rotation in this method):

 * draws a line on the given axis between given points
 * @param batch       spriteBatch
 * @param axis        axis of the line, vertical or horizontal
 * @param x          x position of the start of the line
 * @param y          y position of the start of the line
 * @param widthHeight width or height of the line according to the axis
 * @param thickness thickness of the line
 * @param color       color of the line, if the color is null, color will not be changed.
public static void line(SpriteBatch batch, Axis axis, float x, float y, float widthHeight, float thickness, Color color, float alpha) {
    if (color != null) sprite.setColor(color);
    if (axis == Axis.vertical) {
        sprite.setSize(thickness, widthHeight);
    } else if (axis == Axis.horizontal) {
        sprite.setSize(widthHeight, 1);

With some modifications to the previous method, you can come up with this method to draw a line with rotation.

public static void rotationLine(SpriteBatch batch, float x1, float y1, float x2, float y2, float thickness, Color color, float alpha) {
    // set color and alpha
    if (color != null) sprite.setColor(color);
    // set origin and rotation
    sprite.setRotation(getDegree(x2,y2, x1, y1));
    // set position and dimension
    sprite.setPosition(x1, y1);
    // draw
    // reset rotation

public static float getDegree(float x, float y, float originX, float originY) {
    float angle = (float) Math.toDegrees(Math.atan2(y - originY, x - originX));
    while (angle < 0 || angle > 360)
        if (angle < 0) angle += 360;
        else if (angle > 360) angle -= 360;
    return angle;

public static float distance(float x, float y, float x2, float y2) {
    return (float) Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2));