[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Draw a set of colored convex hulls for a set of points in Python

[A colored set of convex hulls for a set of points]

The post Find multiple convex hulls for a set of points in Python shows how to extract a series of convex hulls from a set of points. This example modifies that one to fill the hull polygons with different shades of red.

Most of the program is the same as the previous one. Most of the difference is in the following draw method.

def draw(self): '''Draw the points, hull, etc.''' # Delete any previous drawing objects. self.canvas.delete(tk.ALL) # Calculate colors. dc = 255 / (len(self.hulls) - 1) # Draw the hulls. r = 255 g = 0 b = 0 outline = '' for hull in self.hulls: self.canvas.create_polygon(hull, outline=outline, fill=rgb_to_hex(r, g, b)) g += dc b += dc # Draw the points. for point in self.points: self.draw_point(point, 'blue')

This code clears the drawing canvas. It then calculates the amount by which it should increase the color's green and blue components to make them shade from 0 to 255.

The code then loops through the hulls. It fills each hull with the next color and then adds dc to the green and blue components. This code calls the rgb_to_hex method described shortly.

After it has filled the hulls, the program loops through the points and calls draw_point (also described shortly) to draw them.

The following code shows the rgb_to_hex function.

def rgb_to_hex(r, g, b): '''Convert RGB values into a hex string that tkinter can understand.''' r = int(r) g = int(g) b = int(b) return f'#{r:02x}{g:02x}{b:02x}'

The tkinter canvas widget doesn't understand red, green, and blue color components, so the program must convert them into a hexadecimal string. The rgb_to_hex method does that. First it converts the color components into integers in case they are floats (which they are in this example). It then uses an f-string to format them as two-digit hexadecimal values and returns the result. For example, this function converts the RGB values for magenta (255, 0, 255) into the hexadecimal string #ff00ff.

The following code shows the revised draw_point method.

def draw_point(self, point, color): RADIUS = 5 RADIUS = 1 self.canvas.create_oval( point[0] - RADIUS, point[1] - RADIUS, point[0] + RADIUS, point[1] + RADIUS, fill=color)

This is the same as the previous version except I changed RADIUS to 1 so the points are just dots and don't obscure the hull colors. You could remove the calls to this method completely if you like.

Download the example to experiment with it (for example, you could change the color scheme) and to see additional details.

© 2024 Rocky Mountain Computer Consulting, Inc. All rights reserved.