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

Geometry2D.merge_polygons will always return polygons with counter-clock-wise #92146

Closed
RickyYCheng opened this issue May 20, 2024 · 2 comments
Closed

Comments

@RickyYCheng
Copy link

RickyYCheng commented May 20, 2024

Tested versions

Godot v4.3.dev6.mono

System information

Windows 10.0.22631 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3050 Laptop GPU (NVIDIA; 31.0.15.2698) - AMD Ryzen 7 5800U with Radeon Graphics (16 Threads)

Issue description

This code merges two unit (clock-wise / counter-clock-wise) sqaure polygons, but returns the same result.
The result polygons are counter-clock-wise.

According to the document, it should returns a cw polygon if it is a boundary and a ccw one if a hole.

extends Node

func _ready() -> void:
	var poly1 = [Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)]
	var poly2 = [Vector2(1, 0), Vector2(1, 1), Vector2(2, 1), Vector2(2, 0)]
	print(Geometry2D.is_polygon_clockwise(poly1))
	print(Geometry2D.is_polygon_clockwise(poly2))
	var merged = Geometry2D.merge_polygons(poly1, poly2)
	poly1.reverse()
	poly2.reverse()
	print(poly1)
	print(poly2)
	var reversed_merged = Geometry2D.merge_polygons(poly1, poly2)
	print(merged)
	print(reversed_merged)
	for poly in merged:
		print(Geometry2D.is_polygon_clockwise(poly))
	for poly in reversed_merged:
		print(Geometry2D.is_polygon_clockwise(poly))
	pass

logs:

true
true
[(1, 0), (1, 1), (0, 1), (0, 0)]
[(2, 0), (2, 1), (1, 1), (1, 0)]
[[(2, 0), (2, 1), (0, 1), (0, 0)]]
[[(2, 0), (2, 1), (0, 1), (0, 0)]]
false
false

Steps to reproduce

Create a node, copy and apply the script, and run.

Minimal reproduction project (MRP)

N/A

@lawnjelly
Copy link
Member

lawnjelly commented May 20, 2024

EDIT: My mistake, this issue isn't directly to do with the clockwise confusion, just examining it in a testbed.

I'm not absolutely sure I understand what you are describing:

In both cases, it seems to be correctly merging two polygons to form one, there are no holes, so the polygon is returned in the same orientation in each case (counterclockwise it seems).

The doc states:

The operation may result in an outer polygon (boundary) and multiple inner polygons (holes) produced which could be distinguished by calling is_polygon_clockwise().

It doesn't actually specify which is which (if I am reading it correctly), so maybe a counter clockwise result is expected.

The doc could arguably be clarified here though to say e.g. counterclockwise for the boundary, and clockwise for the hole. Although this could be confusing due to #92154, so it is understandable why this might not have been mentioned.

@RickyYCheng
Copy link
Author

RickyYCheng commented May 24, 2024

In both cases, it seems to be correctly merging two polygons to form one, there are no holes, so the polygon is returned in the same orientation in each case (counterclockwise it seems).

Yes this is the spirit.

In my case, I input some cw polygons and expected a cw one. And I don't know why the union of two Clock-Wise polygons will produce a counter-clock-wise polygon. The document just ignore this point, so my solution is to reserve the output array.

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

No branches or pull requests

3 participants