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

Polygon and convexity? #245

Closed
MacTuitui opened this issue Jan 21, 2019 · 7 comments · Fixed by #375
Closed

Polygon and convexity? #245

MacTuitui opened this issue Jan 21, 2019 · 7 comments · Fixed by #375

Comments

@MacTuitui
Copy link
Contributor

I'm trying to draw a polygon with the following code:

let mut points = Vec::new();
let s = 5;
for k in 0..s {
    let frac = (k as f32)/(s as f32);
    let angle = frac*TAU;
    let r = (frac-0.2).abs() * 300.0;
    points.push(pt2(r*angle.cos(), r*angle.sin()));
}
draw.polygon()
    .points(points)
    .rgba(1.0, 1.0, 1.0, 0.4);

screen shot 2019-01-21 at 14 31 08

But it looks like the order of the vertices is changed somehow. I'm lost as to why (convexity? triangulation?).

@JoshuaBatty
Copy link
Member

Thanks @MacTuitui . Can you provide a rough drawing of what shape you were intending?

@MacTuitui
Copy link
Contributor Author

That would be the "darker" one.

@mitchmindtree
Copy link
Member

Ah yep, the current polygon API doesn't allow for drawing concave shapes unfortunately! I'll open up an issue about adding support for concave polygons as it should totally be doable with the same API, we'll just need to do the triangulation differently behind the scenes.

Maybe in the meantime this could be better achieved using draw.mesh() for now?

@MacTuitui
Copy link
Contributor Author

MacTuitui commented Jan 21, 2019

But it kind of does actually:

    let mut points = Vec::new();
    let s = 5;
    for k in 0..s {
        let frac = (k as f32)/(s as f32);
        let angle = frac*TAU+0.01;
        let r = (frac-0.6).abs() * 300.0;
        points.push(pt2(r*angle.cos(), r*angle.sin()));
    }
    draw.polygon()
        .points(points)
        .rgba(1.0, 1.0, 1.0, 0.4);

screen shot 2019-01-21 at 14 38 21

That's why I'm confused...

@mitchmindtree
Copy link
Member

Yeah so the reason why it works sometimes is that the polygon triangulation is done by joining every following pair of vertices with the first vertex, so for example if the concave section occurs in line with the first point, the triangulated polygon will have a section that overlaps back over the top of the concave section when it joins the following points.

In case you're interested in where we need to do the actual fix, here is the polygon triangulation iterator. It's very simple at the moment, I'm sure it wouldn't be too tricky to fix but I don't know the concave support approach off the top of my head.

@MacTuitui
Copy link
Contributor Author

It's pretty hard to be honest!
FWIW, it looks like processing defaults to the GLU tessellation.

@mitchmindtree
Copy link
Member

Ahh thanks for the heads up! I'll do some investigating in the future if no one beats me to it. I can image it would get especially tricky when you have overlapping shapes like stars.

It looks like there's a nice writeup here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants