macroquad icon indicating copy to clipboard operation
macroquad copied to clipboard

`draw_rectangle_lines` with thickness `1.0` only draws half of the rectangle

Open walter76 opened this issue 1 year ago • 6 comments

Hi,

I have no idea if this is on purpose, but with macroquad v0.4.5, when I use draw_rectangle_lines with a thickness of 1.0 I only get half of the rectangle drawn. It works fine with a thickness of 2.0.

Code to reproduce:

use macroquad::prelude::*;

#[macroquad::main("Macroquad PoCs")]
async fn main() {
    loop {
        clear_background(LIGHTGRAY);

        draw_rectangle_lines(100., 100., 200., 200., 1., BLUE);

        next_frame().await
    }
}

Sample image:

macroquad_half_rectangle

Is this a bug or does this have to do with some other setting?

/walter

walter76 avatar Mar 03 '24 09:03 walter76

Works in windows image Fails in Linux image

gordug avatar Mar 27 '24 11:03 gordug

I'm working on a pixel art game and this has been an absolute nightmare. I've tried messing around with different thicknesses and I can never get a perfect rectangle. The problem also extends to drawing simple lines as well, if you try to draw a rectangle out of lines very often the corners will be missing.

I really get the impression that the cause of this bug is weirdness with floating point numbers, maybe it would be appropriate to have a second set of drawing functions that take in integer coordinates?

After quite a bit of experimentation, I've come up with this which seems to render an entire rectangle with corners quite consistently. The main thing is to round all coordinates and then add a small epsilon to the coordinate of the top corner. Here's what I've come up with. It's really dirty, but it gets the job done. Your mileage may vary and this might require some adjustment depending on what you're doing.

let x = x.round();
let y = y.round();
let w = w.round();
let h = h.round();
const EPSILON: f32 = 0.5;
draw_rectangle_lines(x, y, w, h, 1.0, WHITE);
draw_line(x + EPSILON, y + EPSILON, x + w, y, 1.0, WHITE);
draw_line(x + w, y + EPSILON, x + w, y + h, 1.0, WHITE);
draw_line(x + w, y + h, x, y + h, 1.0, WHITE);
draw_line(x + EPSILON, y + h, x, y, 1.0, WHITE);

~~Since this seems to be a Linux-specific issue (haven't tested on Windows yet), you could wrap this in a #cfg[(target_os = "linux")] and then have the normal draw_rectangle_lines under a #cfg[(target_os = "windows")]. Hope this helps anyone who is having trouble with this same issue.~~ Nope, it happens on Windows as well for me.

ElnuDev avatar Apr 04 '24 18:04 ElnuDev

idk if this is a good solution but I was able to fix this by changing line 55 in src/shapes.rs from let t = thickness / 2.;, to let t = f32::max(thickness / 2., 1.); just clamping the value of t to 1.0.

ozymandiaslone avatar Apr 06 '24 01:04 ozymandiaslone

The problem is that this line let t = thickness / 2.; is just wrong. If you remove it, the rectangle with width 1 is drawn as expected: image

It gets even clearer when considering this rectangle:

use macroquad::prelude::*;
#[macroquad::main("Rectangles")]
async fn main() {
    loop {
        clear_background(LIGHTGRAY);
        draw_rectangle_lines(100., 100., 200., 200., 100., BLUE);
        next_frame().await;
    }
}

This rect should be filled entirely, but isn't: image

The draw_rectangle_lines_ex function does not have this faulty behaviour:

use macroquad::prelude::*;
#[macroquad::main("Rectangles")]
async fn main() {
    loop {
        clear_background(LIGHTGRAY);
        draw_rectangle_lines_ex(
            100.,
            100.,
            200.,
            200.,
            1.,
            DrawRectangleParams {
                color: BLUE,
                ..Default::default()
            },
        );
        next_frame().await;
    }
}

correctly produces image

cyrgani avatar Jul 15 '24 11:07 cyrgani

Also facing the same issue. Any progress on reviewing the PR?

gavrilikhin-d avatar Jan 03 '25 14:01 gavrilikhin-d