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 dashed line #2337

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

oddbookworm
Copy link
Member

@oddbookworm oddbookworm commented Jul 23, 2023

TODO:

  • write tests
  • fix failures due to conversion from double to int implicitly

@oddbookworm oddbookworm requested a review from a team as a code owner July 23, 2023 21:22
@oddbookworm oddbookworm changed the title Draw dotted line Draw dashed line Jul 23, 2023
@yunline yunline added New API This pull request may need extra debate as it adds a new class or function to pygame draw pygame.draw labels Jul 25, 2023
Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is progressing nicely. Still needs tests as the To do list indicates.

I've also noticed that the segments in a line can be drawn with different top/bottom angles to one another. Like so:

image

Which is not a particularly terrible problem.

I guess you could extend it to take a list of lengths which would make up a repeating pattern of segment lengths (counting the spaces as segments). See:

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash

for inspiration.

src_c/draw.c Outdated Show resolved Hide resolved
@MyreMylar MyreMylar marked this pull request as draft November 26, 2023 17:34
@oddbookworm
Copy link
Member Author

Will revive this soon, I promise

@oddbookworm
Copy link
Member Author

I've also noticed that the segments in a line can be drawn with different top/bottom angles to one another. Like so:

I think this is a problem with draw_line_width in draw.c. Not sure I can fix it without messing with that function, and I'm not sure that's the best idea

@oddbookworm oddbookworm marked this pull request as ready for review December 24, 2023 02:41
@damusss
Copy link
Member

damusss commented Jun 13, 2024

I will leave a comment so the discussion in #general isn't lost.
Problems I've found so far:

  • if delay is greater than the line length, a line will be drawn past the end point. I suggest 2 approaches:
    • Don't draw anything
    • Loop the delay with the % operator ( I prefer this one )
  • if start and end makes up to a perfectly vertical line, nothing is drawn

const double total_length = sqrt(dx * dx + dy * dy);
if (total_length == 0) {
return pgRect_New4(start_x, start_y, 0, 0);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About my latest comment, to add the second approach of the first problem, this snipped seems to do the trick:

if (delay_gap > total_length) {
    delay_gap = delay_gap % (int)total_length;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
draw pygame.draw New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants