Author Topic: return a 3dvector(xyz)?  (Read 3278 times)

0 Members and 1 Guest are viewing this topic.

sybold

  • Newt
  • Posts: 62
return a 3dvector(xyz)?
« on: September 05, 2014, 04:07:45 AM »
i have 2 angles, horz. and vert.
how do i return the 3dvector(xyz)

base is 0,0,0

i'm breaking my head on it with .net

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: return a 3dvector(xyz)?
« Reply #1 on: September 05, 2014, 05:35:43 AM »
Have a play with this.
You could use it as is by passing 0,0,0 as point an 1.0 as distance,
   and convert the point to a Vector3D passing the X Y Z of the returned point
   using  Vector3d(double x, double y, double z);

... or
fiddle with the math to only accept the 2 angles and return the Vector3D.

Code - C#: [Select]
  1.  
  2.         /// <summary>
  3.         /// Return the Polar3D point calculated from angles in each plane
  4.         /// Kdub Services
  5.         /// </summary>
  6.         /// <param name="point"></param>
  7.         /// <param name="xyAngle"></param>
  8.         /// <param name="xzAngle"></param>
  9.         /// <param name="distance"></param>
  10.         /// <returns>Point3d</returns>
  11.         public static Point3d Polar3D(
  12.             Point3d point,
  13.             double xyAngle,
  14.             double xzAngle,
  15.             double distance)
  16.         {
  17.             double sinXy = Math.Sin(xyAngle),
  18.                    cosXy = Math.Cos(xyAngle),
  19.                    sinXz = Math.Sin(xzAngle),
  20.                    cosXz = Math.Cos(xzAngle);
  21.  
  22.             return new Point3d(
  23.                 point.X + distance*cosXy*cosXz,
  24.                 point.Y + distance*sinXy*cosXz,
  25.                 point.Z + distance*sinXz);
  26.         }
  27.  
« Last Edit: September 05, 2014, 05:39:47 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: return a 3dvector(xyz)?
« Reply #2 on: September 05, 2014, 09:44:05 AM »
Something to play with :

Code - C#: [Select]
  1. // (C) Copyright 2014 by KdubServices  
  2. // Proof of concept ; theSwamp KdubServices 2015/09/05
  3. // http://www.theswamp.org/index.php?topic=47778.msg527920#msg527920
  4.  
  5. using System;
  6. using Autodesk.AutoCAD.Runtime;
  7. using Autodesk.AutoCAD.ApplicationServices;
  8. using Autodesk.AutoCAD.Geometry;
  9.  
  10. [assembly: CommandClass(typeof(Polar3DTest.MyCommands))]
  11.  
  12. namespace Polar3DTest
  13. {
  14.     public class MyCommands
  15.     {
  16.         public static double Deg2Rad(double degrees) { return degrees *  (Math.PI / 180.0); }
  17.         public static double Rad2Deg(double radians) { return radians * (180.0 / Math.PI); }
  18.  
  19.         [CommandMethod("Test02", CommandFlags.Modal)]
  20.         public void Test02()
  21.         {
  22.             var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  23.  
  24.             Vector3d v3D = GetVector3D(Deg2Rad(15.0), Deg2Rad(45.0));
  25.             ed.WriteMessage(
  26.                             string.Format(
  27.                                           "\nVector3D 15:45 :\nX:{0}\nY:{1}\nZ:{2}",
  28.                                           v3D.X,
  29.                                           v3D.Y,
  30.                                           v3D.Z)
  31.                 );
  32.         }
  33.         /// <summary>
  34.         /// Return the Vector3d from angles in XY and XZ planes
  35.         ///  
  36.         /// </summary>
  37.         /// <param name="xyAngle"></param>
  38.         /// <param name="xzAngle"></param>
  39.         /// <returns></returns>
  40.         public static Vector3d GetVector3D(
  41.             double xyAngle,
  42.             double xzAngle
  43.             )
  44.         {
  45.             double sinXy = Math.Sin(xyAngle),
  46.                    cosXy = Math.Cos(xyAngle),
  47.                    sinXz = Math.Sin(xzAngle),
  48.                    cosXz = Math.Cos(xzAngle);
  49.             return new Vector3d(
  50.                 cosXy * cosXz,
  51.                 sinXy * cosXz,
  52.                 sinXz);
  53.         }
  54.     }
  55. }
  56.  

Code - Text: [Select]
  1. Command:
  2. Command: NETLOAD
  3. Command: TEST02
  4. Vector3D 15:45 :
  5. X:0.683012701892219
  6. Y:0.183012701892219
  7. Z:0.707106781186547
  8.  

edit kdub :> fixed error in Deg2Rad()
« Last Edit: September 09, 2014, 02:57:12 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: return a 3dvector(xyz)?
« Reply #3 on: September 05, 2014, 11:04:54 AM »
I was testing the result with my calculator (HP35S) and had a moment until I realised that I was testing manually with decimal degrees instead of radians.
Easy to test because the vector length is expected to be 1.

Can be shown by :
note the angles differ from original sample
Code - C#: [Select]
  1.         [CommandMethod("Test03", CommandFlags.Modal)]
  2.         public void Test03()
  3.         {
  4.             var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  5.  
  6.             Vector3d v3D = GetVector3D(Deg2Rad(15.0), Deg2Rad(30.0));
  7.             ed.WriteMessage(
  8.                             string.Format(
  9.                                           "\nVector3D 15:30 :\nX:{0}\nY:{1}\nZ:{2}\nLength:{3}",
  10.                                           v3D.X,
  11.                                           v3D.Y,
  12.                                           v3D.Z,
  13.                                           v3D.Length)
  14.                 );
  15.         }
  16.  
  17.  

Code: [Select]
Command: TEST03
Vector3D 15:30 :
X:0.836516303737808
Y:0.224143868042013
Z:0.5
Length:1


« Last Edit: September 09, 2014, 02:41:30 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

sybold

  • Newt
  • Posts: 62
Re: return a 3dvector(xyz)?
« Reply #4 on: September 05, 2014, 02:27:45 PM »
that's great!

was totally missing the distance factor, had most of the rest.
thanks for helping out

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: return a 3dvector(xyz)?
« Reply #5 on: September 05, 2014, 05:33:35 PM »
You're welcome.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

sybold

  • Newt
  • Posts: 62
Re: return a 3dvector(xyz)?
« Reply #6 on: September 09, 2014, 01:42:58 AM »
just a note, deg2rad should be

Code: [Select]
public static double Deg2Rad(double degrees)
{
return degrees * Math.PI / 180.0;
}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: return a 3dvector(xyz)?
« Reply #7 on: September 09, 2014, 02:35:30 AM »
Sorry, I thought I'd repaired that.
... will fix now.

You can also use an extension method

Code - C#: [Select]
  1. /// <summary>
  2. /// Convert to Radians.
  3. /// </summary>
  4. /// <param name="deg">The value to convert to radians</param>
  5. /// <returns>The value in radians</returns>
  6. public static class NumericExtensions
  7. {
  8.     public static double ToRadians(this double deg)
  9.     {
  10.         return (Math.PI / 180.0) * deg;
  11.     }
  12. }
  13.  
  14.  

Code - C#: [Select]
  1.     Vector3d v3D = GetVector3D(15.0.ToRadians(), 30.0.ToRadians());
« Last Edit: September 10, 2014, 05:06:19 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Bryco

  • Water Moccasin
  • Posts: 1883
Re: return a 3dvector(xyz)?
« Reply #8 on: September 10, 2014, 10:11:32 AM »
return degrees * Math.PI / 180.0; vs return degrees * Math.PI / 180;
I have been caught badly before by the lack of a .0
but a quick test shows int/int=int
double/int=double  int/double=double so 180 will be fine
Util.Debug.Print(3.0 / 5); =0.6
            Util.Debug.Print(3 / 5);=0
            Util.Debug.Print((double) 3 / 5);=0.6
            Util.Debug.Print((double)(3 / 5));=0
            Util.Debug.Print(3 / 5.0);=0.6