Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 5308

Lasers bouncing off elliptical mirrors in C#

$
0
0

The NumberPhile Illumination Problem https://www.youtube.com/watch?v=xhj5er1k6GQ describes reflecting light beams off mirrored walls to see if all parts of an oddly shaped room can be illuminated.

In the last 2 posts (Lasers reflecting off an ellipse  and  Reflect laser beams off multiple mirrors) I showed some code to simulate laser beams reflecting off multiple mirrors in C++

Here is a solution in C# that includes the prior C++ functionality and also an ellipse very close to that described in the video.

The C# code for Reflection is 1600 lines, too big to put in a blog post. So it’s here:
https://github.com/calvinhsia/HwndHost/blob/nlasers/Reflect/Reflect.xaml.cs


I’ve been hosting the code in VSOnline using GIT. Moving the code to use a different backend was pretty simple. First I created an empty repo in GitHub.
Then:

git remote set-url origin https://github.com/calvinhsia/HwndHost.git
git push

 

Just Start Visual Studio, View->Team Explorer, Manage Connections, Clone https://github.com/calvinhsia/HwndHost.git
Then open the HWndHost.sln, right click on Reflect (or any other project in the solution) Set as Startup, then F5 to run the code

Which one is faster the C++ or the C# solution? The number of bounces per second is displayed in the status bar for each.

Issues:
1.    When a beam hits a corner, what should it do? right now the angle can be anything depending on how “close” to the corner it is. It’s the same problem as finding a tangent to a point. You can find a tangent to a curve, but what if it’s a point? Thus currently, the light beams can escape. Different starting window sizes make a difference in the calculations for the light beams.
2.    The code doesn’t remember whether a particular laser beam is identical to a prior laser beam. If so, then we know that spot has been illuminated and we can terminate the laser beam (if that’s possible). It’s still interesting to watch as the laser beams change color and traverse the shape.


In the same HWndHost solution  there are several other projects that do some fancy graphics stuff:

Bouncing balls on the screen (written in VB) You can use HWndHost to host a Win32 HWnd window inside a WPF element

Fish vs Sharks: Predator Prey simulation based on the same base class “HWndHost” as the Bouncing balls.

Logo Turtle Graphics in WPF where you have a very fast turtle with a pen, also based on the same base class.


Viewing all articles
Browse latest Browse all 5308

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>