fossunited / joy

Joy is a tiny creative coding library in Python.
MIT License
192 stars 19 forks source link

points_on_circle function #14

Open anandology opened 3 years ago

anandology commented 3 years ago

It would be interesting to have a function that returns points equidistant on a circle.

points = points_on_circle(n=5, r=100)
shape = polygon(points)

These points can be used to create very intersting shapes by connecting to each other point etc. This will allow the students to achive all these shapes without worrying about sine and cosine.

I'm not particularly happy with the name points_on_circle. Another option is crange, just like range, but returns points on a circle.

pentagon = polygon(crange(n=5))  # r would default to 100

It feels quite useful, but at the same time it also feels like this function doesn't really belong to the joy library and doesn't go well with all other functions.

@amitkaps what do you think?

anandology commented 3 years ago

I could do:

c = circle()
points = c.get_points(n=5)

Looks like it is extensible.

e = ellipse()
points = e.get_points(n=5)
r = rectangle(w=200, h=100)
points = e.get_points(n=8) # any multiple of 4 will work

This approach an be extended to a line or polygon as well.

But I have not introduced methods yet. Not sure, if it is a good idea to do it this way.

amitkaps commented 3 years ago

It will be very useful. You should look at these two function in SVG which do the basic stuff.

It should work on any path & shape - so I would suggest to name it as a general function that work on Path and Shapes. e.g. get_point_on_path and get_point_on_shape

For inspiration & examples, also look at Coordinator Library - https://github.com/spotify/coordinator which extends it beyond paths to shape. Write up about the library @ https://engineering.atspotify.com/2018/03/02/introducing-coordinator-a-new-open-source-project-made-at-spotify-to-inject-some-whimsy-into-data-visualizations/

anandology commented 3 years ago

It will be very useful. You should look at these two function in SVG which do the basic stuff.

* [SVGGeometryElement.getPointAtLength()](https://developer.mozilla.org/en-US/docs/Web/API/SVGGeometryElement/getPointAtLength)

* [SVGGeometryElement.getTotalLength()](https://developer.mozilla.org/en-US/docs/Web/API/SVGGeometryElement/getTotalLength)

It should work on any path & shape - so I would suggest to name it as a general function that work on Path and Shapes. e.g. get_point_on_path and get_point_on_shape

Rather than having to get individual points by specifying length, I think it would be better have a function that returns a list of n points at equal distance on the shape/path.

Also, instead of having a function specific to path or shape, I suggested a method get_points that works on every primitive shape.

One option could be to have a global function get_points(shape, n), but I'm not convinced to add this to the global namespace.

anandology commented 3 years ago

For inspiration & examples, also look at Coordinator Library - https://github.com/spotify/coordinator which extends it beyond paths to shape. Write up about the library @ https://engineering.atspotify.com/2018/03/02/introducing-coordinator-a-new-open-source-project-made-at-spotify-to-inject-some-whimsy-into-data-visualizations/

Thats looks quite interested!

amitkaps commented 3 years ago

Path and Shapes are different - similar to Stroke and Fill (for Colors). So Points on a Path and Points in a Shape have different meaning.

Right now the library does differentiate between the two - so may be later.

Also, worth thinking about what happens when you draw multiple shapes? Or is this going to be limited to primitive shapes (line, rectangle, circle) only.

anandology commented 3 years ago

Path and Shapes are different - similar to Stroke and Fill (for Colors). So Points on a Path and Points in a Shape have different meaning.

Agreed. But I'm trying to see if it is possible to unify that.

Right now the library does differentiate between the two - so may be later.

Thanks for the caution. I'll read up a bit and see if I need to model it differently.

Also, worth thinking about what happens when you draw multiple shapes? Or is this going to be limited to primitive shapes (line, rectangle, circle) only.

It is only for primitive shapes right now. I can't think of a way to generalize this.

However, I would love to find a way to identify a bounding box for any shape. It could be just the center of the shape and width and height.