gdwhe.blogg.se

Pygame draw dashed line
Pygame draw dashed line






pygame draw dashed line

Firstly, in getLine function, it converts the points it receives as arguments from float to int. I can see a couple of things which might be causing problems. When i tried to rotate the mesh, it causes heavy lag, so it is clearly of no use in rendering bigger models(by bigger models, i mean a cube. I tried implementing the code shown above to fill in a mesh (made of only 2 triangles). My goal is to fill in triangles that make up a 3d mesh. It uses bresenham's line algorithm and scan line algorithm to draw lines and fill triangle respectively. # RETURNS ALL THE PIXELS THAT NEEDS TO BE FILLED TO FORM A LINE # x-y-value-pairs of where dashes start (and on next, will end)ĭash_knots = np.array(, end_pos, dash_amount) for i in range(2)]).I wrote the following script to fill a Triangle. # get amount of pieces that line will be split up in (half of it are amount of dashes) Length = np.linalg.norm(end_pos - start_pos) # get euclidian distance between start_pos and end_pos import pygame as pgĭef draw_line_dashed(surface, color, start_pos, end_pos, width = 1, dash_length = 10, exclude_corners = True): You could make this function way shorter, by using all of NumPy's features.

pygame draw dashed line

Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.

pygame draw dashed line

We are looking for answers that provide insightful observations about the code in the question. (surf, color, start.get(), end.get(), width) Start = origin + (slope * index * dash_length)Įnd = origin + (slope * (index + 1) * dash_length) # get back values in original tuple formatįor index in range(0, length/dash_length, 2): Return int(math.sqrt(self.x**2 + self.y**2)) Return Point((self.x/scalar, self.y/scalar)) Return Point((self.x*scalar, self.y*scalar)) Return Point((self.x - other.x, self.y - other.y)) Return Point((self.x + other.x, self.y + other.y)) Instead of storing all the values you're trying to draw, it might be more efficient to just get the slope of the drawn line, and compute dashes at draw time.Ĭonsidering that there's a lot of mirrored code here, consider using a point class to handle both the x and the y values at the same time! class Point: Line a = abs(x2 - x1) to line dy = dl * b / c calculates the amount x and y change for dl (dash_length).īecause dx and dy are floats, I had to use numpy.arange (built-in range() doesn't allow floating-point). This function takes two coordinates and draws a colored dashed line from the first to the second coordinate. Last_coords = list(zip(xcoords, ycoords))įor (x1, y1), (x2, y2) in zip(next_coords, last_coords): Next_coords = list(zip(xcoords, ycoords)) Def draw_dashed_line(surf, color, start_pos, end_pos, width=1, dash_length=10):








Pygame draw dashed line