[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 ray traced self-interlocking shape in Python

[A ray traced self-interlocking shape]

One of the tests of software is to see if you can modify it later. I was pleased that it only took about half an hour to modify the code from my book Build Your Own Ray Tracer With Python to draw this shape, and most of that time was figuring out where the tubes' end points should be.

To really understand the program, you should read the book. It starts simple and helps you build your own ray tracer until it can handle shadows, highlights, reflections, transparency, and more.

Here's the code that actually creates the set of tubes.

# Draw the tubes. radius = 0.4 material = RtMaterial( ambient_color=RtColor.GREEN, diffuse_color=RtColor.GREEN, specular_intensity=0.4, specular_power=30, reflectivity=0.1) points = [ Vector3(2, -2, 1), Vector3(2, -2, -1), Vector3(2, 1, -1), Vector3(-2, 1, -1), Vector3(-2, 1, 2), Vector3(-2, 0, 2), Vector3(1, 0, 2), Vector3(1, 0, -2), Vector3(1, 2, -2), Vector3(-1, 2, -2), Vector3(-1, 2, 1), Vector3(-1, -2, 1), ] for i in range(len(points)): j = (i + 1) % len(points) self.scene.objects.append( RtCylinder.from_endpoints( 'cylinder', material, points[i], points[j], radius)) self.scene.objects.append( RtSphere('sphere', material, points[i], radius))

This code builds a list of points. It then loops through them, connecting each point with the next one to build the tubes. It also placse a sphere at each jpoint so the tubes meet smoothly.

Download the example to see all of the details and read the book to understand how it works.

 

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