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 fill problems for complex polygons #653

Open
bizehao opened this issue Feb 9, 2023 · 2 comments
Open

Draw fill problems for complex polygons #653

bizehao opened this issue Feb 9, 2023 · 2 comments

Comments

@bizehao
Copy link

bizehao commented Feb 9, 2023

I used the following code to draw a fill five-pointed star, and I don't know why the fill color is protruding

                nvgBeginPath(vg);
		struct Point {
			float x;
			float y;
		};
		Point _points[5] = {};
		float r = 100;
		for (int i = 0; i < 5; i++) {
			float du = 72 * (i + 1) * M_PI / 180;
			_points[i] = Point{ r * std::cos(du) + 200, r * std::sin(du) + 200 };
		}

		nvgMoveTo(vg, _points[0].x, _points[0].y);
		nvgLineTo(vg, _points[2].x, _points[2].y);
		nvgLineTo(vg, _points[4].x, _points[4].y);
		nvgLineTo(vg, _points[1].x, _points[1].y);
		nvgLineTo(vg, _points[3].x, _points[3].y);

		nvgClosePath(vg);
		//nvgPathWinding(vg, NVG_CW); //it is fill Normal after adding this
		nvgFillColor(vg, nvgRGBA(169, 192, 50, 255));
		nvgFill(vg);

		nvgStrokeWidth(vg, 3);
		nvgStrokeColor(vg, nvgRGBA(169, 86, 250, 255));
		nvgStroke(vg);

image

When I added nvgPathWinding(vg, NVG_CW); After this, he is filling up normally and why?

image

I checked the docs and said that nanovg's fill mode is even-odd filling rule , so I don't understand Thank you for this library Very good to use Hope to get an answer.

@ColdPaleLight
Copy link

nanovg has some flaws when calculating whether a polygon is convex. The reason for this issue is that nanovg considers the star to be a convex polygon, so it uses the drawing method of convex polygons instead of stencil-then-cover, resulting in incorrect drawing results.

The key to fixing the issue is to improve the algorithm of convex polygons. For example, add some new ways to determine whether it is convex, like skia:
https://github.com/google/skia/blob/chrome/m120/src/core/SkPath.cpp#L2152-L2191

@bizehao
Copy link
Author

bizehao commented Jan 27, 2024

nanovg has some flaws when calculating whether a polygon is convex. The reason for this issue is that nanovg considers the star to be a convex polygon, so it uses the drawing method of convex polygons instead of stencil-then-cover, resulting in incorrect drawing results.

The key to fixing the issue is to improve the algorithm of convex polygons. For example, add some new ways to determine whether it is convex, like skia:
https://github.com/google/skia/blob/chrome/m120/src/core/SkPath.cpp#L2152-L2191

Thank you for your answer, I tried it and added it to nanovg, thank you very much!

@bizehao bizehao closed this as completed Jan 27, 2024
@bizehao bizehao reopened this Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants