Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draw_rectangle_lines with thickness 1.0 only draws half of the rectangle #704

Open
walter76 opened this issue Mar 3, 2024 · 4 comments · May be fixed by #761
Open

draw_rectangle_lines with thickness 1.0 only draws half of the rectangle #704

walter76 opened this issue Mar 3, 2024 · 4 comments · May be fixed by #761
Labels
bug Something isn't working

Comments

@walter76
Copy link

walter76 commented Mar 3, 2024

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

@not-fl3 not-fl3 added the bug Something isn't working label Mar 3, 2024
@gordug
Copy link

gordug commented Mar 27, 2024

Works in windows
image
Fails in Linux
image

@ElnuDev
Copy link

ElnuDev commented Apr 4, 2024

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.

@ozymandiaslone
Copy link
Contributor

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.

@cyrgani
Copy link
Contributor

cyrgani commented Jul 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants