Skip to content

Line.as_segments() and Line.as_points() functions #181

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

Closed
itzpr3d4t0r opened this issue Jan 12, 2023 · 0 comments · Fixed by #192
Closed

Line.as_segments() and Line.as_points() functions #181

itzpr3d4t0r opened this issue Jan 12, 2023 · 0 comments · Fixed by #192
Labels
submodule:line type:new_api Adds new functionalities to the project

Comments

@itzpr3d4t0r
Copy link
Member

itzpr3d4t0r commented Jan 12, 2023

The Line.as_segments() function takes in a parameter num_segments and returns a list of Line objects that are of equal length, with each segment being equal to the original line's length divided by num_segments.

For example, if we have a Line object line that has a length of 10 and we call line.as_segments(3), it will return a list of 3 Line objects with each line having a length of 3.33:

from geometry import Line

line = Line((0, 0), (10, 0))
segments = line.as_segments(3)

print(segments)

# Output:
# [Line((0, 0), (3.33, 0)), Line((3.33, 0), (6.66, 0)), Line((6.66, 0), (10, 0))]

The Line.as_points() function takes in a parameter num_pts and returns a list of points that are of equal distance from each other, starting from the a point to the b point of the Line object. The distance between each point is equal to the line length divided by num_pts.

For example, if we have a Line object line that has a length of 10 and we call line.as_points(3), it will return a list of 3 points with each point being 3.33 apart from each other:

from geometry import Line

line = Line((0, 0), (10, 0))
points = line.as_points(3)

print(points)  

# Output:
# [(0.0, 0.0), (2.5, 0.0), (5.0, 0.0), (7.5, 0.0), (10.0, 0.0)] or [(0.0, 0.0), (5.0, 0.0), (10.0, 0.0)]

In the case of Line.as_segments() you cannot pass a number of segments smaller than 1, as that will return a copy of the original.

In the case of Line.as_points() you cannot pass a number of points smaller than 0, as that will return the line's a and b points. About this we could debate to add a way to only get the points in between a and b with an additional parameter or what the function should return when 0 points are requested. Another thing is that we should decide whether the number of points refer to the total points it returns (meaning it will return a list of size num_pts, a and b included) or just the numbers in between (meaning it will return a list of size 2 + num_pts).

@itzpr3d4t0r itzpr3d4t0r converted this from a draft issue Jan 12, 2023
@itzpr3d4t0r itzpr3d4t0r added type:new_api Adds new functionalities to the project submodule:line labels Jan 12, 2023
@itzpr3d4t0r itzpr3d4t0r linked a pull request Feb 7, 2023 that will close this issue
@github-project-automation github-project-automation bot moved this from Planned next to Done in pygame.geometry Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
submodule:line type:new_api Adds new functionalities to the project
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant