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

end_shape() is closing shapes without 'CLOSE' #167

Closed
marcrleonard opened this issue May 19, 2020 · 6 comments · Fixed by #189
Closed

end_shape() is closing shapes without 'CLOSE' #167

marcrleonard opened this issue May 19, 2020 · 6 comments · Fixed by #189
Labels

Comments

@marcrleonard
Copy link

Consider/run this code:

from p5 import *

window_w = 1500
window_h = 1000
center_x = window_w/2
center_y = window_h/2


def delta_coords(degrees, distance):
	angle_radians = degrees * math.pi / 180
	x = math.cos(angle_radians) * distance
	y = math.sin(angle_radians) * distance
	return x, y

def setup():
	size(window_w, window_h)
	background(255)


def draw():
	background(255)
	for n in range(1):
		start_angle = 0
		end_angle = 500
		start_radius = 350

		curr_raidus = start_radius

		no_fill()
		begin_shape()
		for d in range(start_angle, end_angle):

			final_distance = curr_raidus
			d_x, d_y = delta_coords(d, final_distance)

			x = center_x + d_x
			y = center_y + d_y

			vertex(x, y)

			curr_raidus += .02

		end_shape()

	no_loop()

run()



This creates a very gentle spiral. It appears that once the end angle > 360, the shape closes. This should not happen unless end_shape('CLOSE') is specified.

System information:

  • p5 release - 0.6.0
  • Python version - 3.7
  • Operating system - OSX
@arihantparsoya
Copy link
Member

I believe this has something to do with the Delaunay triangulation algorithm of the triangle library. See: https://www.cs.cmu.edu/~quake/triangle.delaunay.html

It would be better to not use begin_shape because the shape you are trying to render. Because it is not convex. P5.js on the other hand renders the shape perfectly because it is not triangulating the shape, instead they are using the HTML canvas API (https://www.w3schools.com/tags/ref_canvas.asp) for drawing all 2D shapes.

@marcrleonard
Copy link
Author

Yup. Kinda had a feeling it had something to do with the triangle lib. As a matter of fact, I got some error messages from that lib under certain circumstances (I think when there were too many segments, or something).

If I shouldn't use begin_shape() what do you recommend doing? Not necessarily for this specific spiral case, but for other convex shapes?

@arihantparsoya
Copy link
Member

For this particular case, rendering multiple lines to form a spiral would also work.

Not necessarily for this specific spiral case, but for other convex shapes?

I don't have a definite answer. But, begin_shape() would work best if only the outermost vertex() of the geometry are passed (to make sure the shape is convex). Adding vertices inside the geometry could mess up with the triangulation.

@ziyaointl
Copy link
Member

One option of fixing this could be to render the border and the mesh separately - we'll still use the result of triangulation for drawing the mesh but we treat the vertices as in line mode when drawing the border.

@parsoyaarihant Should I give this approach a try? Seems quicker than incorporating PyOpenGL. (In this case I'll still incorporate PyOpenGL but the purpose of which would not include fixing this bug).

@arihantparsoya
Copy link
Member

One option of fixing this could be to render the border and the mesh separately - we'll still use the result of triangulation for drawing the mesh but we treat the vertices as in line mode when drawing the border.

Okay, I think inherently the triangulation approach will have some limitations compared to p5.js because they use path APIs for curves. As @marcrleonard mentioned, the triangle library gave some errors while triangulation.

(In this case I'll still incorporate PyOpenGL but the purpose of which would not include fixing this bug)

I didnt get this part.

@ziyaointl
Copy link
Member

@parsoyaarihant If I fix this bug by rendering the lines and the meshs separately, then incorporating PyOpenGL wouldn't be needed to fix this bug. However, it may still be worth incoporating PyOpenGL for using it in 3D mode and behavior consistancy between the Processing Language dialects? There could be a performance benefit too, but given triangulation isn't the bottleneck in scenes I tested in #173, this benefit is limited.

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

Successfully merging a pull request may close this issue.

3 participants