C#

TCD.Mathematics

UPDATE: Source and samples for WinRT and WPF are now on GitHub: https://github.com/michaelosthege/TCD.Mathematics

This is just a quick post to introduce you to TCD.Mathematics, which is a NuGet package that adds several extension methods to the System.Windows.Media.Media3D namespace. If you’re unfamiliar with this namespace, it contains Point3D, Vector3D, Line3D and Plane3D classes which can be used for three-dimensional calculations.

EDIT: I just submitted an update to the NuGet package, wich adds support for WinRT. Please not that WinRT does not have neither Point3D, Vector3D, nor the System.Windows.Media.Media3D namespace. So in WinRT all classes, including substitutes for Point3D/Vector3D are located under TCD.Mathematics.

EDIT: Since version 1.2.0 TCD.Mathematics is a Portable Class Library that targets everything: Silverlight 4+, .NET4+, .NET for Windows Store, Windows Phone 7+, Xbox360 – make sure you’re running version 2.1+ of NuGet Package Manager!

The extension methods, which are added to the mentioned Point3D/Vector3D classes are:

  • Vector3D.AsPoint3D()
  • Point3D.AsVector3D()
  • Vector3D.Normalized()
  • Point3D.CenterOfClusterWithOtherPoint3Ds ()
  • Point3D.ProjectOnLine3D()
  • Point3D ProjectOnPlane3D()
  • Point3D.DistanceToPlane3D()

The new classes are Line3D and Plane3D. A Line3D can be intersected with a Plane3D (.IntersectWithPlane3D()) and both of them can be defined using different parameters. Line3D has a static method to calculate the ‘approach’ between two lines as well.

Due to the math behind, a variety of methods (and even constructors) can cause exceptions, which are defined in the TCD.Mathematics namespace. So please do your homework and don’t try to calculate illegal stuff, or try{}catch{} your calculations.

The IntelliSense descriptions are very extensive 😉

In case of questions, bugs or suggestions, or even if you like this package, please don’t hesitate to drop me a line =)

cheers^

5 comments

  1. ewernet

    I tryed to instal it on a WPF project in Visual Studio 2012 and it fails, it works only in windows 8?

    1. thecake

      That’s strange – it’s a portable class library that targets .NET4.0 and higher, Silverlight 4 and higher, Windows Phone 7 and higher, .NET for Windows Store and Xbox 360…
      That’s the WPF sample application I use to test it: http://dl.dropbox.com/u/7813771/Blog/TCD%20-%20Desktop/TCD.Mathematics.Sample.WPF.zip
      (The sample is .NET 4.5)

      I haven’t tested it on Windows 7, but there should be no difference. Does it give any error message?

      1. ewernet

        I tryed to test it on a WPF(4.5) application two. Here is te error.
        PM> Install-Package TCD.Mathematics
        Successfully installed ‘TCD.Mathematics 1.2.0’.
        Successfully uninstalled ‘TCD.Mathematics 1.2.0’.
        Install failed. Rolling back…
        Install-Package : Could not install package ‘TCD.Mathematics 1.2.0’. You are trying to install this package into a project that targets ‘.NETFramework,Version=v4.5’, but the package do
        es not contain any assembly references that are compatible with that framework. For more information, contact the package author.
        At line:1 char:16
        + Install-Package <<<< TCD.Mathematics
        + CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
        + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

        I still thinking that the problem is Windows7/Windows8 versions.

        1. thecake

          I’ve tested this on a virtual machine running Windows 7 and can confirm this.
          However I was able to get it to compile by using this project: http://dl.dropbox.com/u/7813771/Blog/TCD%20-%20Desktop/TCD.Mathematics.Sample.zip
          -> in this project, the NuGet package is already added to the solution..
          My guess is that the NuGet extension fucks up the installation.. I’ll try to modify the package to get it working, but in the meantime you’d either have to fall back to previous version (1.1.0) or somehow integrate with the project above.

          1. ewernet

            Thanks, if i can help in any other way, please, let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.